1

I want to create a web app using flexdashboard so that people can upload their CSV and see the visualization.

Right now, I am using a R markdown with flexdashboard template. Here is the file upload code.

{r, echo = FALSE}
fileInput("file1", "Choose CSV File",
                    multiple = TRUE,
                   accept = c("text/csv",
                            "text/comma-separated-values,text/plain",
                            ".csv"))

My question is: in this .rmd notebook, how can I access users' uploaded file and read it as dataframe?

Rieder
  • 1,125
  • 1
  • 11
  • 18

1 Answers1

1

The simplest way I know of is to do something like:

```{r, echo=FALSE}
library(shiny)

fileInput("file1", "Choose CSV File",
          multiple = TRUE,
          accept = c("text/csv",
                     "text/comma-separated-values,text/plain",
                     ".csv")
)

values <- reactiveValues(df_data = NULL)

observeEvent(input$file1, {
  values$df_data <- read.csv(input$file1$datapath)
})


renderTable(values$df_data)

```

You can really do whatever you'd like in that observeEvent expression (i.e. data validation, filter, transform, etc.).

lmeninato
  • 462
  • 4
  • 12
  • 1
    Oh thank you so much! So basically, ```values$df_data``` is our variables or dataframe. – Rieder Apr 20 '22 at 21:51
  • Exactly! Any time someone updates that input file, that reactive value gets updated, and thus any reactive expression/observer that depends on `values$df_data` will get updated and re-rendered as needed (like the `renderTable(values$df_data)` in this example. – lmeninato Apr 20 '22 at 21:54