【问题标题】:Calling reactive objects into render output functions将反应对象调用到渲染输出函数中
【发布时间】:2021-07-12 14:40:22
【问题描述】:

我正在尝试在Shiny 中创建仪表板。我正在从用户那里获取输入(数据框 - df1)并进行一些预测并显示预测结果以及空气质量参数图。 下面是ui 代码块-

########## User interface  ############
ui <- dashboardPage(
  dashboardHeader(title = "Air Quality Index"),
  dashboardSidebar(
    selectizeInput(
      "City", "Select the City:", 
      choices = list("Mumbai", "Delhi", "Bengaluru") 
    ),
    numericInput("PM2.5", "PM2.5 Levels:", min = 0, max = 1000, value = 50),
    numericInput("PM10", "PM10 Levels:", min = 0, max = 1000, value = 50),
    numericInput("NO", "NO Levels:", min = 0, max = 1000, value = 50),
    numericInput("CO", "CO Levels:", min = 0, max = 1000, value = 50),
    numericInput("NO2", "NO2 Levels:", min = 0, max = 1000, value = 50),
    numericInput("O3", "O3 Levels:", min = 0, max = 1000, value = 50),
    actionButton("submitbutton", "Calculate AQI", class = "btn btn-primary")
  ),

dashboardBody(
  fluidRow(
    valueBoxOutput("AQI"),
    valueBoxOutput("high_risk_params")
    
  ),
  fluidRow(
    box(title = "Polluting Contents in Air:", solidHeader = TRUE,
        width = 12, collapsible = TRUE,
        plotlyOutput("param_plot"))
  )
)) 

server 的代码是 -

########### Server logic ############
  server <- function(input, output, session) {
    # Input Data
    datasetInput <-  reactive({
      df1 <- data.frame(
        Names=  c("City"= input$City, 
                  "PM2.5"= input$PM2.5,      
                  "PM10"= input$PM10,       
                  "NO"= input$NO,         
                  "NO2"= input$NO2,        
                  "CO"= input$CO,         
                  "O3"= input$O3        
        ),
        stringsAsFactors = FALSE) 
      
      n <- rownames(df1)
      input <- data.table::transpose(df1)
      colnames(input) <- n
      input[,2:7] <- as.numeric(input[,2:7])
      predicted <- data.frame(Predicted.AQI=predict(rf_model_new,input))
      print(predicted)
      
      
    })
      output$param_plot <- renderPlotly({
        
        df1 <- data.table::transpose(df1, keep.names = "rn")
        df1 <- df1[-1,]
        print(df1)
          plot <- ggplot(df1) + 
          geom_col(aes(x = rn, y = as.numeric(V1), fill = as.numeric(V1))) +
          labs(x = "Air Paramteres", y = "Value") + 
          theme_gray() + 
          ylim(0, NA) +
          geom_hline(yintercept = 50) +
          scale_fill_gradient(low = "green",
                              high = "red", 
                              limits = c(0, 300),
                              na.value = "darkred",
                              name = "Value") +
          theme(panel.background = element_rect(fill = "mintcream"), 
                legend.position = "none") 
        plot <- ggplotly(plot)
        plot
      })
      
      output$AQI <- renderValueBox({
        valueBox(paste0( "AQI:  ",round(predicted,0)),
                " ", icon = icon("cloudscale"), color = "blue", width = 10)
      })
      
      output$high_risk_params <- renderValueBox({
        
        risk_df <- df1 %>%
          filter(df1[,2:7]> 100)
      if(nrow(risk_df)>0){
        valueBox("Over Safe Limits", HTML(paste0(risk_df$rn, sep= "<br>")), 
                 icon = icon("exclamation-triangle"), color = "red")
      }
        else{
          valueBox("No Hazard", icon = icon("exclamation-triangle"), color = "green")
        }
      })
    
    
    
}

现在显示的错误是 - Warning: Error in data.table::transpose: object 'df1' not found &
Warning: Error in paste0: object 'predicted' not found &
Warning: Error in filter: object 'df1' not found

附上错误截图。
任何帮助深表感谢。谢谢!
https://i.stack.imgur.com/4ncQq.png

【问题讨论】:

  • 不清楚你在用datasetInput做什么。您似乎没有在任何地方使用该反应值。但是由于您仅在 datasetInput 反应对象内创建 df1 变量,因此这是唯一可用的地方。该变量永远不会离开该范围。也许看看这些关于反应性的视频,看看闪亮的用途:rstudio.com/resources/shiny-dev-con-2016
  • 谢谢@MrFlick 我会检查的

标签: r shiny


【解决方案1】:

尝试在 reactive({}) 函数外部声明 df1 var 并使其在整个服务器上成为全局变量,然后在 renderPlotly({}) 函数内部使用它

【讨论】:

  • 我不确定这是否可行。 df1 取决于用户输入,需要被动更改。而是创建一个返回此数据框的反应式表达式。
  • 我试图在我的机器上运行这段代码,我发现很多与变量声明有关的错误不仅df1还有很多其他变量,我认为代码的逻辑应该从头开始重新实现以修复所有这些错误
猜你喜欢
  • 1970-01-01
  • 2022-10-06
  • 2017-12-27
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 2021-09-26
  • 2017-07-14
相关资源
最近更新 更多