The path collected by fileInput

4,494 views
Skip to first unread message

Aaron Quinlan

unread,
Nov 16, 2012, 9:03:19 AM11/16/12
to shiny-...@googlegroups.com
Hi all,

I have tried to follow the example use of fileInput in this example [1] to attempt to open a local sqlite database (using Chrome).  It seems that after selecting a file with the fileInput widget, the full path of the selected file is not stored.  Instead, only the name of the file is stored.  The evidence for this is that I the following error message after selecting "test.db" from the browser UI:

> runApp("/Users/arq5x/shiny/")

Listening on port 8100
     name     size type
1 test.db 16048128     
                                                                                 datapath
1 /var/folders/7r/c3r2sn4j3dvg4c62jr1dq8_w0000gn/T//RtmpAk41Pt/e23a2c42561fcb0d044d05f5/0
Error in path.expand(dbname) : invalid 'path' argument
Error in path.expand(dbname) : invalid 'path' argument

Am I missing something obvious?  I know the server code can work since it works fine when I hard-code a full path to a file in the dbConnect statement instead of using input$db

Code below:

ui.R
===
library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(

  headerPanel("Gemini"),
  
  sidebarPanel(
                
    fileInput("db", "gemini database", multiple=FALSE),
    checkboxInput("lof", "Loss of function")
  ),

  mainPanel(
    tableOutput("view")
  )
))


server.R
=======
library(shiny)
library(RSQLite)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
    output$view <- reactiveTable(function() {
        if (! is.null(input$db)) {
            print(input$db)
            con <- dbConnect(dbDriver("SQLite"), 
                         dbname = input$db)
            query = paste("select ", "chrom, start, end", 
                          " from variants limit 10")
            df = dbGetQuery(con, query)
            df
        }
    })
})

Ryan Hope

unread,
Nov 16, 2012, 9:37:17 AM11/16/12
to shiny-...@googlegroups.com
I don't believe there is any way to get there real path to the file. At least, that is what I have found so far. This makes it really hard to make R load large files directly.

Joe Cheng

unread,
Nov 16, 2012, 11:41:01 AM11/16/12
to shiny-...@googlegroups.com
Are you looking to allow the user to browse the server's entire filesystem? Or are we just talking about one folder? If the latter, then you could consider using a selectInput with list.files populating the choices (and verify, when you're reading the choice, that it's definitely a file in the directory you expect and not malicious input with ../../ etc.). If the former, there are ways I could think of enabling this but you'd really need to think through the security implications of exposing your server's filesystem.

If this is only for local use then putting a call to file.choose() behind a button is probably the way to go. Someone on this forum submitted a code sample for an action button, I'll see if I can get it or something similar in shiny-incubator.


--
 
 

Aaron Quinlan

unread,
Nov 16, 2012, 12:55:56 PM11/16/12
to shiny-...@googlegroups.com
Thanks for your comments, Joe.  I was thinking of the former in full recognition of the security implications, given that the envisioned application will really just be running on individual machines.  If you have any suggestions or examples, it would be great.  Given the utility of R for data analysis, I imagine it would be useful for other users to conduct ad hoc analyses of different files using the same analysis engine in shiny.

Cheers,  
- Aaron





--
 
 

Thomas Bristow

unread,
Nov 17, 2012, 1:27:22 AM11/17/12
to shiny-...@googlegroups.com
I would like to plot data from a text file collected by fileInput. Can you tell me how to do that? 
I am pretty new to this.

thanks 
Tom
Message has been deleted

Mikhail Semeniuk

unread,
Nov 19, 2012, 3:01:47 AM11/19/12
to shiny-...@googlegroups.com

Hey,

I think i figured out a way to do this. In my example, i'm grabbing a file from it's /tmp location and moving it to a more permanent location for further processing:

shinyServer(function(input, output) {
  output$filetable <- reactiveTable(function() {
    if (is.null(input$files)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    
    df <- input$files
    path <- df$datapath 
    file.copy(path, "~/web/tcprbp/")
    
    input$files
    
  })
})

Best,
M

Ryan Hope

unread,
Nov 19, 2012, 11:04:05 AM11/19/12
to shiny-...@googlegroups.com
Launching file.choose from a button does not work. We really need a way to access the filesystem path. I agree with the OP that any Shiny app I make is going to be run locally thus the security risk is minimal.

Joe Cheng

unread,
Nov 19, 2012, 5:35:20 PM11/19/12
to shiny-...@googlegroups.com
Can you tell me what doesn't work about file.choose from a button? I just added an action button into the shiny-incubator package (use library(devtools); install_github('shiny-incubator', 'rstudio') to install) and it seemed to work fine with file.choose for me.

Two things to note: If you cancel out of file.choose, it raises an error; if that's not the behavior you want then you can do result <- try(file.choose()) and then use is(result, 'try-error') to see if the user cancelled or not.

Secondly, if you don't want the file.choose to appear right away, but only when the button is clicked, you can check if the button value is 0 and don't do anything in that case.


--
 
 

Felix Wittmann

unread,
May 8, 2013, 7:15:25 AM5/8/13
to shiny-...@googlegroups.com
Hi Joe,

it seems to work fine for shiny (local) but not for shiny server. I know the answer will be security, however this is also a question of local deployment, since shiny server has some advantages in local deployment, too.

Felix Wittmann

unread,
May 8, 2013, 7:47:02 AM5/8/13
to shiny-...@googlegroups.com
in the log the failure is recorded like this

Warning in strsplit(msg, "\n") :
  input string 1 is invalid in this locale
Error in zipArchive(file) : file ��U
                                    ��p
                                       ,#Y
                                          t�G
                                             ��U
                                                `�� does not exist

Joe Cheng

unread,
May 8, 2013, 10:25:01 PM5/8/13
to shiny-...@googlegroups.com
The problem is file.choose() only launches a dialog when running in an interactive session of R, which is not the case when R is running under Shiny Server. You can see this yourself if you run R non-interactively from your command line:

R -e "file.choose()"

I'm not sure how you can work around this--maybe someone else has ideas?


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Joe Cheng

unread,
May 8, 2013, 10:26:26 PM5/8/13
to shiny-...@googlegroups.com
I'm not sure what the strsplit/zipArchive error is, that may not be related.
Reply all
Reply to author
Forward
0 new messages