3

Instead of specifying separate fileInput variables, I'd like to use reactiveValues to store uploaded CSV dataframes, manipulate them in some way, and then store them for accession later. My design is to name each dataframe by its filename and append to the reactiveValue rvTL. My questions are,

  • How can I access individual dataframes under the list I created using reactiveValuesToList(rvTL)?
  • Next step, how to create a selectInput menu to access the individual dataframes uploaded by fileInput

To learn this concept, I am piggybacking off the answer from Dean Attali and made rvTL the same as his values variable. R shiny: How to get an reactive data frame updated each time pressing an actionButton without creating a new reactive data frame?

I've gone over many example codes on reactiveValues, yet still at an incomplete understanding. Most examples are using some sort variation on reactiveValuesToList(input) R Shiny: Keep/retain values of reactive inputs after modifying selection, I'm really not seeing the logic here. Any help/suggestions would be appreciated!

library(shiny)
runApp(shinyApp(
  ui=(fluidPage(
    titlePanel("amend data frame"),

    mainPanel(
      fileInput("file", "Upload file", multiple=T),
      tabsetPanel(type="tabs",
          tabPanel("tab1",        
                    numericInput("Delete", "Delete row:", 1, step = 1),
                    actionButton("Go", "Delete!"),
                    verbatimTextOutput("df_data_files"),
                    verbatimTextOutput("values"),
                    verbatimTextOutput("rvTL"),
                   tableOutput("rvTL_out")
          ),
          tabPanel("tab2", 
                  tableOutput("df_data_out")
          )
    )))),

  server = (function(input, output) {
    values <- reactiveValues(df_data = NULL) ##reactiveValues
    rvTL <- reactiveValues(rvTL = NULL)

    observeEvent(input$file, {
      values$df_data <- read.csv(input$file$datapath)
      rvTL[[input$file$name]] <- c(isolate(rvTL), read.csv(input$file$datapath))
    })

    observeEvent(input$Go, {
      temp <- values$df_data[-input$Delete, ]
      values$df_data <- temp
    })

    output$df_data_files <- renderPrint(input$file$name)
    output$values <- renderPrint(names(values))
    output$rvTL <- renderPrint(names(reactiveValuesToList(rvTL))[1] )
    output$rvTL_out <- renderTable(reactiveValuesToList(rvTL)[[1]])
    output$df_data_out <- renderTable(values$df_data)

    })
  ))
Community
  • 1
  • 1
seraphim711
  • 137
  • 2
  • 11

1 Answers1

4

It really is as straightforward as you thought. You were close too, just fell into some syntax traps. I made the following changes:

  • that c(isolate(.. call was messing things up, I got rid of it. It was leading to those "Warning: Error in as.data.frame.default: cannot coerce class "c("ReactiveValues", "R6")" to a data.frame" errors.
  • Also you were reusing the rvTL name too often which is confusing and can lead to conflicts, so I renamed a couple of them.
  • I also added a loaded file name list (lfnamelist) to keep track of what was loaded. I could have used names(rvTL$dflist) for this but it didn't occur to me at the time - and I also this is a useful example of how to organize related reactive values into one declaration.
  • And then I added rendered selectInput so you can inspect what is saved in the reactiveValue list.

So here is the adjusted code:

library(shiny)
runApp(shinyApp(
  ui=(fluidPage(
    titlePanel("amend data frame"),

    mainPanel(
      fileInput("file", "Upload file", multiple=T),
      tabsetPanel(type="tabs",
                  tabPanel("rvTL tab",        
                           numericInput("Delete", "Delete row:", 1, step = 1),
                           uiOutput("filesloaded"),
                           actionButton("Go", "Delete!"),
                           verbatimTextOutput("df_data_files"),
                           verbatimTextOutput("values"),
                           verbatimTextOutput("rvTL_names"),
                           tableOutput("rvTL_out")
                  ),
                  tabPanel("values tab", 
                           tableOutput("df_data_out")
                  )
      )))),

  server = (function(input, output) {
    values <- reactiveValues(df_data = NULL) ##reactiveValues
    rvTL <- reactiveValues(dflist=NULL,lfnamelist=NULL)

    observeEvent(input$file, {
      req(input$file)
      values$df_data <- read.csv(input$file$datapath)
      rvTL$dflist[[input$file$name]] <-read.csv(input$file$datapath)
      rvTL$lfnamelist <- c( rvTL$lfnamelist, input$file$name )
    })

    observeEvent(input$Go, {
      temp <- values$df_data[-input$Delete, ]
      values$df_data <- temp
    })

    output$df_data_files <- renderPrint(input$file$name)
    output$values <- renderPrint(names(values))
    output$rvTL_names <- renderPrint(names(rvTL$dflist))
    output$rvTL_out <- renderTable(rvTL$dflist[[input$lftoshow]])
    output$df_data_out <- renderTable(values$df_data)
    output$filesloaded <- renderUI(selectInput("lftoshow","File to show",choices=rvTL$lfnamelist))

  })
))

And here is a screen shot:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Thank you again for such clear explanation! I see now I have to initialize the `reactiveValues` as lists (`rvTL$dflist`) in order to hold other things. This is exactly what I was going for! I guess I was a bit fixated on using `reactiveValuesToList` without understanding it. Cheers! – seraphim711 Mar 30 '17 at 14:13