【问题标题】:Shiny: reactive context error in ggplot interactivity using reactiveValuesShiny:使用 reactiveValues 的 ggplot 交互性中的反应性上下文错误
【发布时间】:2015-11-21 23:40:25
【问题描述】:

我的计划是导入一个名为rawdata 的大型原始数据集,然后使用源脚本.R 文件对数据集进行一些修改。处理后的数据作为由 tidyr::gather 创建的名为 heap 的多个数据帧的列表传回 server.R。

接下来,我想显示heap[[2]]heap[[10]] 的ggplot。

使用 Shiny 网站上的示例绘图交互,我希望能够从图中清除明显异常值的点。但是,我遇到了一个无法调试的讨厌的反应式上下文错误。我相当肯定它涉及第 77 到 80 行:

vals.temp <- reactiveValues(
if(!is.null(heap())) {
  keeprows = rep(TRUE, nrow(heap()[[2]]))
})

在 Shiny 网站上提供的示例中,它使用 mtcars 数据集,但我的数据集是基于用户输入的响应式数据集。我怀疑某处有断线。

文件链接: Dropbox

非常感谢您的专业知识!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以声明一个空列表vals &lt;- reactiveValues(),然后您可以向其中添加一个元素some &lt;- reactive({ ... vals$keeprows &lt;- nrow...})

    在下面的示例中,plotOutput('figure') 现在是交互式的。我使用了闪亮画廊中提到的示例。

    服务器脚本的关键部分:

    output$AddCustom <- renderUI(
        AddCustomInputs()
      )
    
    
      # Based on data() variables column, populate a selectInput called "subset" with variable data
      output$selector <- renderUI({
        selectInput('subset', 'Graph Parameter', choices = levels(heap()[[1]]$variable))
      })
    
      # Changes from here: ----------------------------------------------------------------------
    
      vals <- reactiveValues() # keeprows = rep(TRUE, 100000 )
    
      # Subset the data() set by the value selected in the "subset" selectInput and save it as df()
      df <- reactive({
    
        vals$keeprows <- rep(TRUE, nrow(heap()[[1]][heap()[[1]]$variable %in% input$subset, ]))
    
        return(heap()[[1]][heap()[[1]]$variable %in% input$subset, ])
      })
    
    
      observeEvent(input$exclude_toggle, {
        res <- brushedPoints( df(), input$figure_brush, allRows = TRUE)
    
        vals$keeprows <- xor(vals$keeprows, res$selected_)
      })
    
      observeEvent(input$exclude_reset, {
        vals$keeprows <- rep(TRUE, nrow( df() ))
      })
    
    
      # create a plot based on df()
      output$figure <- renderPlot({
    
        keep <- df()[ vals$keeprows, , drop = FALSE ] 
    
        if(!is.null(df())) {
          plot <- isolate(
            ggplot(data = na.omit(keep), aes(x = NST_DATI, y = value)) + 
              geom_line() + 
              labs(x = "Date", y = input$subset, title = paste(input$subset, "vs. Date"))
          )
          return(plot)
        }
      })
      output$table <- renderDataTable(
        if(!is.null(rawdata())) {
          return(rawdata())
        }
      )
    })
    

    用户界面脚本:

    shinyUI(fluidPage(
        titlePanel("Data Fix-it"),
        sidebarLayout(
          sidebarPanel(
            fileInput('rawfile', 'Input *.CSV'),
            helpText("Files downloaded from ADRS Console must be saved as *.csv for import."),
            h4("Other Parameters"),
            tags$body("Only the 'Big 7' parameters will be retained, unless specified."),
            br(),
            checkboxInput('AddCustomCheck', 'Add custom parameters'),
            uiOutput('AddCustom'),
            hr(),
            numericInput('sequnceminutes', 'Water Quality Interval (mins)', value = 60),
            actionButton('groomgo', 'Groom Data'),
            textOutput('linesaltered'),
            hr(),
            downloadButton('downloadcsv', 'Download *.csv file')
          ),
          mainPanel(
            tabsetPanel(
              tabPanel("Plot",
                       uiOutput('selector'),
                       plotOutput('figure', brush = brushOpts(id = "figure_brush")),
    
                       actionButton("exclude_toggle", "Toggle points"),
                       actionButton("exclude_reset", "Reset")
              ),
              tabPanel("Table",
                       dataTableOutput('heaptable')),
              tabPanel("Report",
                       actionButton('MakePlotsgo', 'Make Plots'),
                       plotOutput('heaptemp'),
                       plotOutput('heapph'),
                       plotOutput('heapcond'),
                       plotOutput('heaptds'),
                       plotOutput('heapdomgl'),
                       plotOutput('heapdosat'),
                       plotOutput('heapturb'),
                       plotOutput('heapflow'),
                       plotOutput('heapcustom1'),
                       plotOutput('heapcustom2'),
                       plotOutput('heapcustom3')
              )
            )
          )
        )))
    

    我希望这是你想要的:)

    【讨论】:

    • 非常感谢您的超越。
    猜你喜欢
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多