Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Created November 10, 2012 08:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcheng5/4050398 to your computer and use it in GitHub Desktop.
Save jcheng5/4050398 to your computer and use it in GitHub Desktop.
Shiny file upload demo
shinyServer(function(input, output) {
output$filetable <- reactiveTable(function() {
if (is.null(input$files)) {
# User has not uploaded a file yet
return(NULL)
}
input$files
})
})
shinyUI(pageWithSidebar(
headerPanel("File input test"),
sidebarPanel(
fileInput("files", "File data", multiple=TRUE)
),
mainPanel(
tableOutput("filetable")
)
))
@yannrichet
Copy link

Oups, seems not to work out-of-the-box (:

> library(shiny)
Le chargement a nécessité le package : websockets
Le chargement a nécessité le package : caTools
Le chargement a nécessité le package : bitops
Le chargement a nécessité le package : digest
> runGist('4050398')

Listening on port 8100
Error in is.null(data) || is.na(data) : type 'y' incorrect dans 'x || y'
Error in is.null(data) || is.na(data) : type 'y' incorrect dans 'x || y'

And then, when trying to select a file:

Error in UseMethod("close") : 
  pas de méthode pour 'close' applicable pour un objet de classe "uninitializedField"

More info on my R/shiny configuration:

> packageDescription('shiny')
Package: shiny
Type: Package
Title: Web Application Framework for R
Version: 0.1.9
Date: 2012-11-06
Author: RStudio, Inc.
Maintainer: Joe Cheng <joe@rstudio.org>
Description: Shiny makes it incredibly easy to build interactive web
        applications with R. Automatic "reactive" binding between
        inputs and outputs and extensive pre-built widgets make it
        possible to build beautiful, responsive, and powerful
        applications with minimal effort.
License: GPL-3
Depends: R (>= 2.14.1), websockets (>= 1.1.5)
Imports: stats, tools, utils, datasets, methods, caTools, RJSONIO,
        xtable, digest
Suggests: markdown
URL: https://github.com/rstudio/shiny,
        http://rstudio.github.com/shiny/tutorial
BugReports: https://github.com/rstudio/shiny/issues
Collate: 'map.R' 'random.R' 'timer.R' 'tags.R' 'cache.R' 'react.R'
        .....
Packaged: 2012-11-06 21:55:32 UTC; jcheng
Built: R 2.15.1; ; 2012-11-13 12:35:16 UTC; unix

-- File: /home/richet/opt/R/x86_64-pc-linux-gnu-library/2.15/shiny/Meta/package.rds 

Sorry for error messages in french...

@jcheng5
Copy link
Author

jcheng5 commented Nov 14, 2012

I fixed the first error. For the second error, can you tell me what browser you're using? Can you try it with the latest version of Chrome or Firefox to see if that makes a difference?

@yannrichet
Copy link

Hi Joe, I used latest Chrome on Linux 64
Can you tell me where to find your fix ?

... and thank you for this amazing project .

@tommo42
Copy link

tommo42 commented Nov 16, 2012

I am using the shiny server to test the file upload demo. I got as far as selecting a file. After choosing .txt or .csv there is an error message saying the Rwebsocket unexpectedly closed.
Maybe a dumb question, but are only certain file types accepted?

@jcheng5
Copy link
Author

jcheng5 commented Nov 20, 2012

Yann, the fix should be in the latest Shiny package.

install.packages('shiny', repos=c('http://rstudio.org/_packages', 'http://cran.rstudio.com'))

@jcheng5
Copy link
Author

jcheng5 commented Nov 20, 2012

tommo42, I think there may be an incompatibility between the file upload code and shiny server--I will have to look into that. BTW, I don't seem to get notifications on comments posted here so if you would like to follow up please e-mail me at joe AT rstudio.org or on the shiny-discuss google group. Thanks!

@yannrichet
Copy link

Thanks, it works.
Could not even reproduce the second error...
I also tried to reproduce the bug mentioned by tommo42 without success.
Seems everything is ok now.

@bestdan
Copy link

bestdan commented Dec 9, 2012

Hi Joe,
I've just tried this gist on Glimmer, and get the websocket failing issue as well. If I include the line:
'''R
install.packages('shiny', repos=c('http://rstudio.org/_packages', 'http://cran.rstudio.com'))
'''
in start of the ui.R and server.R files, I get the following error:
'''R
Listening on port 50719
Execution halted'''
Any reason why Glimmer wouldn't be up to date?

Much thanks!
Dan

@bestdan
Copy link

bestdan commented Dec 9, 2012

////edited for markdown
Hi Joe,
I've just tried this gist on Glimmer, and get the websocket failing issue as well. If I include the line:
install.packages('shiny', repos=c('http://rstudio.org/_packages', 'http://cran.rstudio.com'))
in start of the ui.R and server.R files, I get the following error:
Listening on port 50719 Execution halted
Any reason why Glimmer wouldn't be up to date?

Much thanks!
Dan

@Nomen-Nescio
Copy link

I am adding a bit of information to this page, since it took me 20 minutes to figure out which is stupid I know, it probably will take someone else it as well.

Most people, like me, will actually want to use the file. When you upload, you do not upload the file but a group of strings, this is what you see when you use the code above. Using the 'datapath' column you are able to use the normal ways to get your data, e.g. read.csv()

Seeing the post linked below made me realize this, using the line:

inFile <- input$files

you get the uploaded data (note this does not contain the file yet) in a data.frame. If you use

dataset<-read.csv(inFile$datapath, header=T, stringsAsFactors =F)

you actually get the data you want in the variable 'dataset'. The variable inFile$datapath contains the path to the file. Now you have the data, play around with it! Using the normal code for the UI you should see your table.

Might be redundant, but since this is one of the first hits on google, I guess it won't hurt.

Links:
https://groups.google.com/forum/?fromgroups=#!topic/shiny-discuss/rQMyQR7gOQk

@ApsaraRadhakrishnan
Copy link

All's great, but where and how does the file gets stored when it is uploaded ?

Thank you,
Apsara

@jamiemkass
Copy link

I'm running my app locally and can't seem to do a multi-select for files when I specify "multiple=TRUE". I'm on Mac OS Yosemite. If it's local, don't see how my web browser figures into this. Has anyone else had this problem? I tried shift-clicking, command-clicking, command-A, and nothing works.

@lycone
Copy link

lycone commented Nov 12, 2016

What about if i don't wanna return all the file informations but just the filename. Like for example '/User/Documents/.../filename' the output would look like 'filename'

@Kumar015
Copy link

Dear,

I am new to R-shiny, Can you please help me "how to create a graph using the file which user loaded manually through fileinput function.?
Basically i would like to access the variable from the file loaded by user and create a graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment