【问题标题】:R Shiny data not available "Error: object not found"R闪亮的数据不可用“错误:找不到对象”
【发布时间】:2018-06-29 10:47:10
【问题描述】:

在 Shiny 应用程序中绘制条形图时,生成的数据帧仅可用于第一个绘图,而不能用于第二个绘图(“错误:找不到对象 df2”)。将生成数据帧的代码复制并粘贴到第二个 renderPlot 部分可以解决问题,但这是多余的,并且会降低应用程序的速度。有没有更优雅的方式让数据在多个 renderPlot 部分中可用?我尝试使用 Shinys reactive() 函数但没有成功。

这是一个最小的例子:

library(shiny)
library(ggplot2)

# Define UI for application that draws a histogram
ui <- fluidPage(

  titlePanel("Utility"),

  sidebarLayout(
    sidebarPanel(
    sliderInput("var1",
                 "N",
                 min = 1,
                 max = 100,
                 value = 20)),
    mainPanel(
      plotOutput("barplot1"),
      plotOutput("barplot2"))))



# Define server logic required to draw a barplot
server <- function(input, output) {

  output$barplot1 <- renderPlot({

  df <- as.data.frame(matrix(c(
                      "A", "1", rnorm(1, input$var1, 1),
                      "A", "2", rnorm(1, input$var1, 1),
                      "B", "1", rnorm(1, input$var1, 1),
                      "B", "2", rnorm(1, input$var1, 1)),
                      nrow = 4, ncol=3, byrow = TRUE))
  df$V3 <- as.numeric(as.character(df$V3))

  df2 <- as.data.frame(matrix(c(
                       "A", "1", rnorm(1, input$var1, df[1,3]),
                       "A", "2", rnorm(1, input$var1, df[1,3]),
                       "B", "1", rnorm(1, input$var1, df[1,3]),
                       "B", "2", rnorm(1, input$var1, df[1,3])),
                       nrow = 4, ncol=3, byrow = TRUE))
  df2$V3 <- as.numeric(as.character(df$V3))

  ggplot(df, aes(x=V1, y=V3, fill=V2)) +
  geom_bar(stat="identity") 

  })

  output$barplot2 <- renderPlot({

  ggplot(df2, aes(x=V1, y=V3, fill=V2)) +
  geom_bar(stat="identity") 

  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 将 df2 放入 output$barplot2 会导致以下错误消息:“错误:'closure' 类型的对象不是子集”。我想这与 df ist 动态创建的事实有关?

标签: r shiny


【解决方案1】:

是的,您永远不应该复制您的数据。要使您的数据框可用于所有函数,只需使用reactive 动态生成它(因为它取决于input$var1)。调用反应变量x 时,需要使用x() 而不是x。否则,你会得到你得到的错误:Error: object of type 'closure' is not subsettable"。因此,在您的情况下,使df 具有反应性需要您改用df()

server <- function(input, output) {

  df <- reactive(
    data.frame(V1 = c("A", "A", "B", "B"),
               V2 = c("1", "2", "1", "2"),
               V3 = rnorm(1, input$var1, 5)        
               )
  )

  output$barplot1 <- renderPlot({
    ggplot(df(), aes(x=V1, y=V3, fill=V2)) +
      geom_bar(stat="identity") 
  })

  output$barplot2 <- renderPlot({
    ggplot(df(), aes(x=V1, y=V3, fill=V2)) +
      geom_bar(stat="identity") 
  })
}

【讨论】:

  • 谢谢!两个答案都解决了我的问题,但我接受了这个,因为它更短。谢谢你们!
【解决方案2】:

这个正在他们自己的反应式外部创建data.frame

library(shiny)
library(ggplot2)

# Define UI for application that draws a histogram
ui <- fluidPage(

  titlePanel("Utility"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("var1",
                  "N",
                  min = 1,
                  max = 100,
                  value = 20)),
    mainPanel(
      plotOutput("barplot1"),
      plotOutput("barplot2")
      )
    )
  )



# Define server logic required to draw a barplot
server <- function(input, output) {


  df <- reactive({


    df <-  as.data.frame(matrix(c(
      "A", "1", rnorm(1, input$var1, 1),
      "A", "2", rnorm(1, input$var1, 1),
      "B", "1", rnorm(1, input$var1, 1),
      "B", "2", rnorm(1, input$var1, 1)),
      nrow = 4, ncol=3, byrow = TRUE))
    df$V3 <- as.numeric(as.character(df$V3))

    df

  })

  df2 <- reactive({  

  df2 <- as.data.frame(matrix(c(
    "A", "1", rnorm(1, input$var1, df()[1,3]),
    "A", "2", rnorm(1, input$var1, df()[1,3]),
    "B", "1", rnorm(1, input$var1, df()[1,3]),
    "B", "2", rnorm(1, input$var1, df()[1,3])
    ),
    nrow = 4, ncol=3, byrow = TRUE))
   df2$V3 <- as.numeric(as.character(df()$V3))

  df2
  })

  output$barplot1 <- renderPlot({


    ggplot( df() , aes(x=V1, y=V3, fill=V2)) +
      geom_bar(stat="identity")

  })

  output$barplot2 <- renderPlot({


    ggplot(df2(), aes(x=V1, y=V3, fill=V2)) +
      geom_bar(stat="identity")

  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多