11

Following the last example on RMarkdown's Parametrized Reports page, I am trying to use the Shiny interface to select my input file with the following code inside the YAML header:

params:  
  data:  
    input: file  
    label: 'Input dataset:'  
    value: myData.csv  

The Shiny interface shows up and I get to browse for a file, but when I try to access it further down in the R code via read.csv(file=params$data, header=TRUE), I get the following message:

Error in file(file, "rt") : cannot open the connection

How can I get to read my file?

Note: I have seen a thread where users pass the file path in a function at the time of rendering the RMarkdown document, but this is not what I am trying to do. I would just like to be able to select it from the Shiny interface.

EDIT
After playing a bit more, I think the issue is that the temporary file created when reading the file I select via the shiny interface and passed as params$data doesn't exist anymore when I try to access it.
Indeed, file.exists(params$data) returns FALSE.

So I guess my question now becomes: How do I get to read this temporary file before it is erased ?

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
YeO
  • 938
  • 1
  • 9
  • 17
  • Quite sure this is a bug in the system. I reported this to the RStudio crew, but no answer yet. – Joris Meys Mar 06 '17 at 10:00
  • Have you tried accessing the file as you would had you been using a Shinyapp? Usually, the object created by fileInput is not the path to the file. The actual path would be, in your case, `params$data$datapath` or maybe `params$datapath`. – Ricardo Fernandes Campos Mar 12 '18 at 18:31

1 Answers1

2

It doesn't look like this can be reproduced anymore.

Just for the sake of explaining how this is done:

  1. Create a new RMarkdown document
  2. Add the following yaml block to the top:

    params:  
      data:  
        input: file  
        label: 'Input dataset:'  
        value: myData.csv  
    
  3. Add the following R chunk to the document:

    ```{r data}
    cat(params$data)
    c <- read.csv(params$data)
    print(c)
    ```
    
  4. Select under the "Knit" dropdown the "Knit with Parameters" option

  5. Attach any valid CSV and click "Knit" in the Shiny parameters panel
Scott Jackson
  • 194
  • 3
  • 7
  • Is it possible to access the original file path? I am working on a report where I need to log the file location and timestamp. – Oskar Hansson Jun 03 '20 at 09:35