How to update the file associated with the `fileInput` variable in R Shiny without user interaction?

I am working on an application in R where users need to select a file from their computer using the RShiny button fileInput. I want to change this so that a related variable can be assigned (for example, a file can be loaded) automatically by the program, without clicking on the user button and without selecting the file.

The problem that I am facing is that it fileInputhas 4 fields, among which I only know 3. For example, when I load a file hello.csvin a variable inFilethrough the usual 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

Although I could guess that the second and third know the file, I have no idea how to assign the field datapath...

I tried declaring it inFileas a global variable NULLand then assigning different fields one at a time, but I was stuck with this last one. Is there another way to do it, like a function that mimics the behavior of a user who presses a file input button and selects a specified file?

Many thanks.

+4
source share
2 answers

Here is how I understood it:


, read.csv(...) data.frame. , , . , ( fileInput), , . , , .

, , , .


@brittenb , , -. , .

- , fileInput .

+1

, , , Shiny . R. :

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)

Shiny , inFile$datapath is NULL . , inFile , , .

, .

Update

, reactiveValues , . , / input$inFile$datapath values$file_path, .

+5

All Articles