[英]R: Shiny Using variables under data Reactive
I am trying to use variables created under data reactive(). 我正在尝试使用在数据react()下创建的变量。 Following is my code.
以下是我的代码。 It's a ready to use example
这是一个随时可以使用的示例
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() })
})
I am only able to create 1 chart for the other I am receiving an Error: object 'data2' not found. 我只能为另一个创建1个图表,但收到错误:找不到对象“ data2”。 Help me with defining variables inside data reactive and then using them to plot.
帮助我在数据反应式内部定义变量,然后使用它们进行绘图。
I think there is some confusion as to how variables are scoped here. 我认为这里对变量的范围有一些困惑。
data2
is not defined in the output$plot2
code block, and it shares no definitions with those defined in the output$plot1
code block. data2
没有在output$plot2
代码块中定义,并且它与output$plot1
代码块中定义的定义没有共享。
I think this does what you want, although I would have used a reactiveValues
for data_rec
. 我想这你想要做什么,但我会用一个
reactiveValues
的data_rec
。
library(shiny)
library(datasets)
library(ggplot2)
u <- 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")
)
)
))
cars=mtcars
s <- shinyServer(function(input, output) {
data_rec =reactive({
req(input$var)
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({
data2=data_rec()
ggplot(data2$d1,aes(x=gear,y=wt))+geom_boxplot()
})
})
shinyApp(u,s)
yielding: 产生:
You forgot to create your data in output$Plot2
. 您忘记在
output$Plot2
创建数据。
It should look like this: 它看起来应该像这样:
output$Plot2 <- renderPlot({
data2 <- data1()
data2$d1 %>%
ggplot +
aes(x = gear,y = wt) +
geom_boxplot()
})
You could also put the reactive parts directly in the renderPlot functions and avoid having lists to work with ggplot... 您还可以将反应部件直接放入renderPlot函数中,并避免让列表与ggplot一起使用...
The server part would look like this: 服务器部分如下所示:
server <- shinyServer(function(input, output) {
output$Plot1 <- renderPlot({
data %>%
filter(cyl == input$var) %>%
ggplot +
aes(x = gear, y = wt) +
geom_boxplot()
})
output$Plot2 <- renderPlot({
data %>%
filter(cyl == input$var & am == 0) %>%
ggplot +
aes(x = gear,y = wt) +
geom_boxplot()
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.