【问题标题】:R: Shiny Using variables under data ReactiveR: Shiny 在数据反应下使用变量
【发布时间】: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”。帮助我在数据反应中定义变量,然后使用它们进行绘图。

【问题讨论】:

  • 如果您找到一些其他词来命名您的“数据”,将会有所帮助。它变得相当混乱。
  • 反应式不共享上下文。您在Plot1 renderPlot 块中定义的data2 未在Plot2 块中定义。
  • 我已经消除了这种困惑,请帮助您的第二条评论

标签: html r shiny reactive-programming


【解决方案1】:

我认为这里变量的范围存在一些混淆。 data2 没有在output$plot2 代码块中定义,它与output$plot1 代码块中定义的定义没有任何共同之处。

我认为这可以满足您的需求,尽管我会使用 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)

屈服:

【讨论】:

  • 一个快速的问题,我可以只在列表中列出 1 或变量,并且它依赖于变量。它会起作用吗?因此,例如 data = reactive({ a= 2^2, b=a^2 list(b=b) }) 然后仅将 b 用于绘图
【解决方案2】:

您忘记在 output$Plot2 中创建数据。

应该是这样的:

output$Plot2 <- renderPlot({ 
                data2 <- data1()
                data2$d1 %>% 
                  ggplot + 
                  aes(x = gear,y = wt) + 
                  geom_boxplot() 
})  

您也可以将反应部分直接放在 renderPlot 函数中,避免让列表与 ggplot 一起使用...

服务器部分如下所示:

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()
          })  
})

【讨论】:

  • 不错的解决方案。我认为reactiveValues 会产生更灵活的结构。
猜你喜欢
  • 2013-06-29
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 2021-01-07
  • 2022-01-02
  • 2020-12-28
相关资源
最近更新 更多