Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the file related to a `fileInput` variable in R Shiny without user interaction?

I'm working on an app in R where the users need to choose a file from their computer, with a RShiny fileInput button. I want to modify this, so that the associated variable can be assigned (i.e. a file can be loaded) automatically by the programm, without having the user click on the button and choose the file.

The problem I'm facing is that a fileInput has 4 fields, amongst which I only can know 3. For instance, when I load the file hello.csv in the variable inFile through the normal procedure, here is what I get :

inFile$name = hello.csv
inFile$size = 8320
inFile$type = text/csv
inFile$datapath = C:\\Users\\MyName\\AppData\\Local\\Temp\\Rtmpkh8Zcb/7d5f0ff0111d440c7a66b656/0

Though I could have guessed the second and the third one knowing the file, I have no idea how the datapath field is assigned...

I've tried to declare inFile as a NULL global variable, then to assign one by one the different fields, but I'm stuck with this last one. Is there an other way to do, like a function that mimics the behaviour of a user who clicks on the file input button and choose a specified file ?

Thank you very much.

like image 336
AdrienW Avatar asked Apr 05 '16 11:04

AdrienW


2 Answers

If all you're looking to do is load a file initially, you don't have to rely on Shiny functions to do that. You can just rely on R functions. Set up your app like this:

ui <- shinyUI(
  fileInput("inFile", label="Choose a file", multiple=F)
)

server <- shinyServer(function(input, output, session) {
  values <- reactiveValues()

  dat <- reactive({
    if (is.null(inFile$datapath)) {
      dat <- read.csv("path/to/your.csv")
      values$file_name = "your.csv"
      values$file_type = "csv"
      values$file_size = file.size("path/to/your.csv")
      values$file_path = "path/to/your.csv"
    } else {
      dat <- read.csv(inFile$datapath)
      values$file_name = inFile$name
      values$file_size = inFile$size
      values$file_type = inFile$type
      values$file_path = inFile$datapath
    }
  })
})

shinyApp(ui=ui, server=server)

In the above code, the Shiny app will start and see that inFile$datapath is NULL and will load a predefined file of your choosing. It won't run again until inFile changes, at which point it will load the file that the user pointed to.

Hope that helps.

Update

I changed the code above to use reactiveValues to store the pieces of information that need to be used throughout the app. If you just set those and then do a find/replace for input$inFile$datapath and replace it values$file_path, your code should work just fine.

like image 59
tblznbits Avatar answered Oct 27 '22 00:10

tblznbits


Here is how I figured it out :


I edited the original code, so that all the read.csv(...) are replaced with calls to a data.frame global variable. I also added a small button that you need to click on before you continue. This button saves what you just loaded in the Database (if you chose a file with the fileInput) and assigns the right values to the global variables that will be needed for the following operations. If you chose no file at all, it will directly assign the variables from the data found in the Database.

So I did not find a proper solution to the problem, but this is a workaround that will do the job in my case.


@brittenb I couldn't get your reactive solution to work as I wanted to, that's why I ended up doing this another way. Thanks for having taken the time to think about it though.

I'm still open to suggestions on how to update the file in a fileInput without user interaction.

like image 41
AdrienW Avatar answered Oct 26 '22 23:10

AdrienW