闪亮 - 使用 eventReactive 根据输入更新数据
shiny - update data based on input using eventReactive
刚接触shiny
,尝试在更新数据时整理eventReactive
。在我的例子中,我希望 data.frame
dat.base
不被重新计算,除非输入 (textInput
a2
) 被更新。与 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)
你应该把 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)
})
}
刚接触shiny
,尝试在更新数据时整理eventReactive
。在我的例子中,我希望 data.frame
dat.base
不被重新计算,除非输入 (textInput
a2
) 被更新。与 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)
你应该把 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)
})
}