【发布时间】:2017-03-21 15:40:59
【问题描述】:
我正在尝试使用在 data reactive() 下创建的变量。以下是我的代码。这是一个现成的例子
用户界面
library(shiny)
shinyUI(fluidPage(
titlePanel("Old Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("var",
label = "Choose a Group to Display",
choices = c("4", "6","8"),
selected = "4")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("Plot1"),
plotOutput("Plot2")
)
)
))
服务器
library(shiny)
library(datasets)
library(ggplot2)
cars=mtcars
shinyServer(function(input, output) {
data_rec =reactive({
d=cars[cars$cyl==input$var,]
d1=d[d$am==0,]
list(d=d,d1=d1)
})
output$Plot1 <- renderPlot({
data2=data_rec()
ggplot(data2$d,aes(x=gear,y=wt))+geom_boxplot() })
output$Plot2 <- renderPlot({
ggplot(data2$d1,aes(x=gear,y=wt))+geom_boxplot() })
})
我只能为另一个创建 1 个图表,我收到一个错误:找不到对象“data2”。帮助我在数据反应中定义变量,然后使用它们进行绘图。
【问题讨论】:
-
如果您找到一些其他词来命名您的“数据”,将会有所帮助。它变得相当混乱。
-
反应式不共享上下文。您在
Plot1renderPlot 块中定义的data2未在Plot2块中定义。 -
我已经消除了这种困惑,请帮助您的第二条评论
标签: html r shiny reactive-programming