简体   繁体   中英

How to create a plot in shiny combining x number of CSV inputs?

I'm trying to create a dashboard that accepts any number of CSV files, combines them together (probably using bind_rows), wrangles the code, and then creates a plot with data from each file being represented as a separate geom_line.

So far I have found code that allows the generation of the UI, but I'm stuck as to how to get the data from those inputs into a reactive tibble that I can then plot.

library(shiny)
library(purrr)

ui <- fluidPage(
  numericInput("n", "Number of files", value = 5, min = 1),
  uiOutput("file"),
)

server <- function(input, output, session) {
  file_names <- reactive(paste0("file", seq_len(input$n)))
  
  output$file <- renderUI({
    map(file_names(), ~ fileInput(.x, NULL,
                                  accept = c('text/csv', 'text/comma-separated-values', '.csv')))
 })   
   
 }

shinyApp(ui = ui, server = server)

Any help appreciated.

library(shiny)
library(purrr)
library(readr)
library(ggplot2)
library(dplyr)
# create some dummy data
map(1:2, ~ tibble(x = 1:10, y = x + .x) |> write_csv(paste0("file", .x, ".csv"))) |>
    invisible()

ui <- fluidPage(
    numericInput("n", "Number of files", value = 2, min = 1),
    uiOutput("file"),
    plotOutput("plot", width = "20%")
)

server <- function(input, output, session) {
    file_names <- reactive(replicate(input$n, paste0(sample(c(0:9, letters), 9), collapse = '')))
    
    output$file <- renderUI({
        map(file_names(), ~ fileInput(.x, NULL,
                                      accept = c('text/csv', 'text/comma-separated-values', '.csv')))
    })   
    
    df <- reactive({
        map(isolate(file_names()), ~ req(input[[.x]]$datapath))
        
        map(seq_along(isolate(file_names())), 
            ~ input[[file_names()[.x]]]$datapath |>
                read_csv() |>
                mutate(class = as.factor(.x))
        ) |>
            bind_rows()
    })
    
    output$plot <- renderPlot({
        req(df())
        ggplot(df()) +
            geom_line(aes(x, y, color = class))
    })
}

shinyApp(ui = ui, server = server)
  1. One problem with creating dynamic number of file input is that even if you change the n , once you have uploaded some files in the previous round, in the next round (after you change n ), previous file upload information is still there. This is because you didn't change the input ID for the next round. To avoid so, first we need to make sure file upload IDs are unique each time you change n . This is done by replicate(... sample(... . This is a very simple implementation of unique IDs. To use a more robust method, read UUID .
  2. We use req to make sure all file inputs have files uploaded
  3. Read individual files into memory and give them a unique class column value so we can plot them as different groups on plot.
  4. bind all rows to a tibble.
  5. plot them all. use class as a color marker to distinguish data from different files.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM