Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the data from a file passed as parameters in a RMarkdown document?

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 ?

like image 229
YeO Avatar asked Dec 23 '15 13:12

YeO


People also ask

How do I use parameters in R Markdown?

Parameters in an R Markdown document are very simple to use. In the yaml header (the section at the top of the markdown document), you just have to add a couple new lines for your parameters to hardcode them in. Once they're coded in, they will be available in the params object for use in the rest of the analysis.

How do I read an Excel file in R Markdown?

You can click in the upper left menu File > Import Dataset > From Excel and select the file to import it. Then you can copy the code that appears in the R console with the code required for import the data in xlsx and then copy it in a R Markdown code chunk.

How do I open a RMD file?

Rmd file contains runtime: shiny in its YAML header, the RStudio IDE will display a “Run Document” button at the top of the scripts pane. The “Run Document” button is a shortcut for the rmarkdown::render command. It let's you quickly render your . Rmd file into an interactive document hosted locally on your computer.


1 Answers

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
like image 50
Scott Jackson Avatar answered Oct 17 '22 18:10

Scott Jackson