访问reactiveValuesToList 中的reactiveValues [英] accessing reactiveValues in a reactiveValuesToList

查看:36
本文介绍了访问reactiveValuesToList 中的reactiveValues的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是指定单独的 fileInput 变量,我想使用 reactiveValues 来存储上传的 CSV 数据帧,以某种方式操作它们,然后存储它们以供以后访问.我的设计是按文件名命名每个数据帧,并附加到reactiveValue rvTL.我的问题是,

  • 如何访问我使用 reactiveValuesToList(rvTL) 创建的列表下的单个数据框?
  • 下一步,如何创建一个selectInput菜单来访问fileInput
  • 上传的各个数据帧

为了学习这个概念,我借鉴了 Dean Attali 的答案,并让 rvTL 与他的 values 变量相同.

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)

    })
  ))

解决方案

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:

这篇关于访问reactiveValuesToList 中的reactiveValues的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆