user6550364
user6550364

Reputation:

R Flexdashboard: Calling uploaded data via fileInput from other components

I have more experience with Shiny and Shiny dashboards so maybe that's why I'm having trouble with this. I can approximate a MWE with the below hypothetical:

Say I want to replicate the ggplotly example but I want to combine it with file upload functionality with Shiny. Meaning, I will have nine flexdashboard components (one for data upload, 4+4 for the ggplotly figures). If I set up the file upload in component#1, I seem to be able to call it reactively e.g. (from the file upload example above)

server <- function(input, output) {

output$contents <- renderTable({

req(input$file1)

df <- read.csv(input$file1$datapath,
         header = input$header,
         sep = input$sep,
         quote = input$quote)

return(head(df))

})

}

This works within its own component/code chunk in RMarkdown; once a user uploads a .csv file, it displays the first five rows.

However, I'm failing to call the uploaded data from any other component, as it is embedded as a Shiny module in its own component. I tried assigning the uploaded data to an object and then calling it reactively (e.g. selectedData()) but that greys out the component. Also tried setting an observeEvent to no avail. I feel I must be missing something obvious. In the ggplotly example, the data is saved in the global chunk - that would serve my needs, but seems like it only works in you are getting the data from a package/library.

tl;dr What's the best way of calling user-uploaded data from other flexdashboard components?

Upvotes: 1

Views: 2272

Answers (2)

Manu
Manu

Reputation: 1120

This is an old question but this code will let you upload files on Flexdashboard:

    ---
    title: "Untitled"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    ```

    ```{r}
    dataset <- eventReactive(input$file1,{
      dataset <- read.csv(input$file1$datapath)
    })
    ```

    # Column {.sidebar}

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

    # Data 

    ## Row

    ### Table 1 - Show the data recently acquired

    ```{r}
    renderTable({
      dataset <- dataset()
      dataset
    })
    ```

The main issue was to create a reactive function, since you don't know about the dataset until you upload the file. The reactive value is input$file1

I wish the person who asked this question was available, anyway it was trial an error. No document on the web points to flexdashboard for uploading files.

Upvotes: 0

Jai
Jai

Reputation: 321

One way to do this would be to use a sidebar (although same code can be used without). Modify fileInput to reflect file options.

Try this:

# File Upload {.sidebar}

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

Remember to add runtime: shiny to the YAML header

Upvotes: 1

Related Questions