Elegantly and interactively obtain a local file path

1,450 views
Skip to first unread message

djo...@aucklanduni.ac.nz

unread,
Jan 26, 2016, 8:04:51 PM1/26/16
to Shiny - Web Framework for R

Hi all. I have trying to solve this simple problem.

I would like to interactively obtain a local file path to pass to a reactive expression. 
I don't want to use fileInput since it uploads (i.e. copies) the file. I haven't found a way to suppress this behaviour. I'm sure there is a way to get the file path from fileInput
but by that point, the damage is done, since I don't want to make a copy of the file at all since it's (a) large and (b) already there.

I have rtfm'd extensively, and tried to use shinyFiles, which seemed to be the perfect solution but I can't get it to work since it seems to stop reactive expressions from being reactive (sorry if terminology is wrong here).

I can get it all to work OK with the following code, but it's just not that elegant. This switches to the browser window, pops up a file.choose() dialog in R (so you have to switch back to R) with no ability to provide information on what file you want, populates the textInput with the correct file path, then correctly displays the (in this case) tab separated csv file as a table in the browser window mainPanel. It's just not that elegant, requires that you switch back and forth between RStudio and your browser at weird times, and can't provide any text about what file is required.

The alternative is cutting and pasting the file path into a textInput UI element, which also works fine, but isn't elegant.

Has anyone used 
shinyFiles and gotten it to work for something like this? Is there an alternative? Am I missing something obvious?


server.R
shinyServer(function(input, output) {
  filedata
<- reactive({
    inpath
<- input$my_file_path
    read
.csv(inpath)
 
})
 
  output$filetable
<- renderTable({
    filedata
()
 
})
})


ui.R

shinyUI(pageWithSidebar(
  headerPanel
("CSV Viewer"),
  sidebarPanel
(
    textInput
("my_file_path", label = "Full path to my file", value = file.choose())
 
),
  mainPanel
(
    tableOutput
("filetable")
 
)
))

Joe Cheng

unread,
Jan 26, 2016, 8:15:45 PM1/26/16
to djo...@aucklanduni.ac.nz, Shiny - Web Framework for R
I haven't used shinyFiles but it or something like it is necessary here. What exactly do you mean by "seems to stop reactive expressions from being reactive"?

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/74c44f50-4ea1-4a51-99ca-90a3aba7a9c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

djo...@aucklanduni.ac.nz

unread,
Jan 26, 2016, 8:42:31 PM1/26/16
to Shiny - Web Framework for R, djo...@aucklanduni.ac.nz

Hi, thanks… appreciate your time. Sorry for the repeat emailspam... I'm new to Google groups


I wish I could put it in a better way… In short, I can get a shinyFiles dialog box up to ask for a file (and a folder for that matter) and correctly display the folder path in the UI (as per the shinyFilesExample()) but a downstream reactive expression stops working. It’s a reactive expression to connect to an SQLite database (which is the giant file) and grab some gene expression data based on another textInput box, and plot it. I have this working fine without shinyFiles but the solution is really inelegant (see here:


https://github.com/NZGL/shiny_cuffdiff


As well, if I simply have a submit button in the downstream code (I.e. submitButton) then the shinyFiles “select a file” dialog box suddenly stops being able to navigate up and down folders (it still shows the folders, you just can’t click on them to see what’s inside and you can’t select files). I can see some issues that seem similar on the shinyFiles issues page but I’m really not enough of an expert to understand the issue. Also I’m reluctant to hassle the developer of shinyFiles since (a) he recently had a concussion and (b) he’s  finishing his PhD (I’m not sure which is worse). So I’m kind of stuck with half-solutions for now. 

djo...@aucklanduni.ac.nz

unread,
Jan 26, 2016, 8:44:12 PM1/26/16
to Shiny - Web Framework for R, djo...@aucklanduni.ac.nz
I will shortly post some examples of the shinyFiles code which will illustrate the point.
Reply all
Reply to author
Forward
0 new messages