【问题标题】:renderPlot producing error "object of type 'closure' is not subsettable"renderPlot 产生错误“'closure' 类型的对象不是子集”
【发布时间】:2016-03-05 09:11:06
【问题描述】:

我目前正在尝试在由 4 个滑块输入设置的值的数据帧上使用 renderplot(),但我一直遇到同样的错误。

library(shiny)

# Define server logic for random distribution application

shinyServer(function(input, output) {

sliderValues <- reactive ({
#compose data frame
  ws<- as.numeric  (c(0:input$sws))
  df<-data.frame(
      WindSpeed = as.numeric  (c(ws)
      ),
      CBH = as.numeric (c(input$sCBH)
                          ),
      FFMC = as.numeric (c(input$sFFMC)
                          ),
      DC = as.numeric  (c(input$sDC)
                         ),
      PCFI = as.numeric  (c((exp(-66.62+(-0.993*input$sCBH)+(0.568*ws)+(0.671*input$sFFMC)+(0.018*input$sDC)))/(1+(exp(-66.62+(-0.993*input$sCBH)+(0.568*ws)+(0.671*input$sFFMC)+(0.018*input$sDC)))))
                        )

      )



})

#Show the values using an HTML table
output$summary <- renderPlot ({
  plot(df$WindSpeed, df$PCFI)
})  
output$values <- renderTable({
    sliderValues()

  })

})

我不确定为什么 R 会为此踢出错误,但我是 Shiny 包的新手,我认为它与在创建变量时定义变量有关?我的最终目标似乎并不难......但我一直坚持让代码生成图表很长一段时间。

library(shiny)

shinyUI
  (fluidPage(
    titlePanel("Wildfire Behaviour Model"),

    #Sidebar with sliders that demonstrate various available 
    #options
    sidebarLayout(
        sidebarPanel(
          #Simple integer interval
          sliderInput ("sws", "10m Wind Speed (km/hr):",
                   min=0,
                   max=50,
                   value=15),
          sliderInput ("sCBH", "Crown Base Height (m):",
                   min=0,
                   max=25,
                   value=5),
          sliderInput ("sFFMC", "Fine Fuel Moisutre Code:",
                   min = 77,
                   max=98,
                   value = 88,
                   step=1.0),
          sliderInput("sDC", "Drought Code:",
                  min=0,
                  max= 1000,
                  value = 200)
          ),

        #Show a table summarizing the values entered
        mainPanel(
            plotOutput("summary"),
            tableOutput("values")
            )
        )
    ))

感谢您在该主题上提供的任何帮助,并感谢您花时间阅读我的问题。凯文

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您的数据框 (df) 不存在于 reactive 表达式之外

    sliderValues <- reactive ({
    #compose data frame
      ws<- as.numeric  (c(0:input$sws))
      df<-data.frame(
          WindSpeed = as.numeric  (c(ws)
          ),
          CBH = as.numeric (c(input$sCBH)
                              ),
          FFMC = as.numeric (c(input$sFFMC)
                              ),
          DC = as.numeric  (c(input$sDC)
                             ),
          PCFI = as.numeric  (c((exp(-66.62+(-0.993*input$sCBH)+(0.568*ws)+(0.671*input$sFFMC)+(0.018*input$sDC)))/(1+(exp(-66.62+(-0.993*input$sCBH)+(0.568*ws)+(0.671*input$sFFMC)+(0.018*input$sDC)))))
                            )
    
          )
    

    此代码块实际上将sliderValues 设置为您的数据框。但它是反应式的,因此当您更改输入时它会更新。

    在反应式表达式中有一个“已理解”return 语句将最后一个条目返回到sliderValues()

    您需要的更改是如何访问绘图函数中的data.frame

    #Show the values using an HTML table
    output$summary <- renderPlot ({
      plot(sliderValues()$WindSpeed, sliderValues()$PCFI)
    })  
    

    因为sliderValues() 本质上您的数据框。

    我更喜欢构建我的反应式数据帧的方式更像

    df_sliderValues() <- reactive({
    
        ## code to generate my data frame
        df <- data.frame(a = c(...),
                         b = c(...))
        return(df)
    })
    
    output$plot <- renderPlot({
        plot(df_sliderValues()$a, df_sliderValues()$b).
    })
    

    【讨论】:

    • 天啊@symbolix,你做到了。我认为它与反应功能有关,但我无法缩小范围。我非常感谢您的帮助!现在,当我今晚上床睡觉时,这将不会挂在我的头上。
    • @AlphaKevy 没问题。我发现 reactive() 构造起初有点令人困惑,因为它与标准 R 函数相差了一步。但是,当您了解它时,它在shiny 环境中完全有意义。
    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2014-08-01
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多