【问题标题】:shiny - update data based on input using eventReactiveshiny - 使用 eventReactive 根据输入更新数据
【发布时间】:2020-11-26 19:19:00
【问题描述】:

shiny 很陌生,在更新数据时尝试整理eventReactive。就我而言,我希望不重新计算data.framedat.base,除非更新输入(textInputa2)。与 stoch_data 对象相反,在更新两个输入中的任何一个时都应重新计算该对象。我似乎无法弄清楚如何创建dat.base(每当我将dat.base 包装在eventReactive 中时,我都会收到object 'dat.base' not found 错误。

我做错了什么?感谢您的任何指导...

小例子:

library(plyr)
library(dplyr)
library(tidyr)
library(ggplot2)

library(shiny)
library(shinydashboard)
    
sidebar <- dashboardSidebar(
    textInput(inputId = "a1", label = "First", 
                value = "10"),
                                        
    textInput(inputId = "a2", label = "Second", 
                value = "20")
)


server <- function(input, output) {
    stoch_data <- reactive({
        a1 <- as.numeric(input$a1)
        a2 <- as.numeric(input$a2)
        stoch_output <- data.frame(a1 = a1, a2 = a2)        
        }) 

    output$plot2 <- renderPlot({
        a1 <- 10
        a2 <- as.numeric(input$a2)
        
        eventReactive(input$a2, {
        dat.base <- data.frame(a1, a2) %>% 
                                                mutate(Source = "Baseline input parameters")
                                        }, ignoreNULL=FALSE)

        dat <- stoch_data() %>%
                mutate(Source = "User-provided input parameters") %>%
                rbind(dat.base) 
                
        ggplot(dat) +
            geom_point(aes(x = a1, y = a2)) +
            facet_wrap(~ Source)
        }) 
} 

body <- dashboardBody(
    mainPanel(plotOutput("plot2"), width = 350 ))

ui <- dashboardPage(sidebar = sidebar,
    body = body,
    header = dashboardHeader()
        )
                    
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny reactive


    【解决方案1】:

    您应该将eventReactive() 放在output$plot2 之外。试试这个

    server <- function(input, output) {
      stoch_data <- reactive({
        a1 <- as.numeric(input$a1)
        a2 <- as.numeric(input$a2)
        stoch_output <- data.frame(a1 = a1, a2 = a2)        
      }) 
      
      dat.base <- eventReactive(input$a2, {
        a1 <- 10
        a2 <- as.numeric(input$a2)
        dat.base <- data.frame(a1, a2) %>% 
          mutate(Source = "Baseline input parameters")
      }, ignoreNULL=FALSE)
      
      output$plot2 <- renderPlot({
        
        dat <- stoch_data() %>%
          mutate(Source = "User-provided input parameters") %>%
          rbind(dat.base()) 
        
        ggplot(dat) +
          geom_point(aes(x = a1, y = a2)) +
          facet_wrap(~ Source)
      }) 
    } 
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 2017-05-17
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多