【发布时间】: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)
【问题讨论】: