Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 闪亮应用程序-多用途的反应式数据框&;延迟活动_R_Shiny - Fatal编程技术网

R 闪亮应用程序-多用途的反应式数据框&;延迟活动

R 闪亮应用程序-多用途的反应式数据框&;延迟活动,r,shiny,R,Shiny,我正在创建一个带有多个KPI值框的闪亮仪表板。我不会粘贴整个服务器/代码(>500行代码),但每个框都使用renderValueBox调用,我创建要使用的数据框“kpidat”(我对每个框都这样做),并使用if语句确定框的颜色和符号。请参见下面的示例代码: output$incbox <- renderValueBox({ area_prev<-as.character(input$dynamic2) prev_region<-input$select_B

我正在创建一个带有多个KPI值框的闪亮仪表板。我不会粘贴整个服务器/代码(>500行代码),但每个框都使用renderValueBox调用,我创建要使用的数据框“kpidat”(我对每个框都这样做),并使用if语句确定框的颜色和符号。请参见下面的示例代码:

output$incbox <- renderValueBox({

    area_prev<-as.character(input$dynamic2)
    prev_region<-input$select_B
    kpidat<-sept[sept$area==area_prev & sept$region==prev_region,]

    if(kpidat$`New TB incidents`[2]>kpidat$`New TB incidents`[1]){a<-"arrow-up";b<-"red"}
    if(kpidat$`New TB incidents`[2]<kpidat$`New TB incidents`[1]){a<-"arrow-down";b<-"green"}
    if(kpidat$`New TB incidents`[2]==kpidat$`New TB incidents`[1]){a<-"";b<-"yellow"}

    valueBox(
        paste0(kpidat$`New TB incidents`[2], "Incidents"), "Number of disease incidents.", 

        icon = icon(a),
        color = b
    )
})

output$incbox几个建议:


  • 您可以将
    kpidat
    定义为一个反应对象,它将在
    input$select\B
    发生更改时更新。要在其他输出中使用它,请使用
    kpidat()
    调用它。即使您在多个其他输出中使用它,
    kpida中的表达式也非常好。谢谢你的帮助!我现在已经按照建议创建了一次被动函数来创建kpidat。我肯定也会研究trycatch,因为我以前从未听说过。非常感谢你的提示!
    
    kpidat <- reactive({
      return(sept[sept$area==as.character(input$dynamic2) & sept$region==input$select_B,])
    })
    
    output$incbox <- renderValueBox({
      tryCatch({
        ## This will return if it can execute successfully
        local_kpidat <- kpidat()
    
        ## Only calculate these once
        foo <- local_kpidat$`New TB incidents`[2]
        bar <- local_kpidat$`New TB incidents`[1]
    
        if(foo >  bar){a<-"arrow-up";b<-"red"}
        if(foo <  bar){a<-"arrow-down";b<-"green"}
        if(foo == bar){a<-"";b<-"yellow"}
    
        valueBox(value = paste0(kpidat$`New TB incidents`[2], "Incidents"),
                 subtitle = "Number of disease incidents.",
                 icon = icon(a),
                 color = b)
    
      }, error = function(e) {
        ## This, an empty value box, will be returned if any errors occur
        valueBox(value = "","")
      })
    })
    
    setDT(sept) ## conver sept to a data.table
    setkey(sept,area,region,date) ## setkeys on sept to order it properly, I assume there is some kind of date value here?
    
    ## Calculate delta for all cases at once
    sept[, Delta := `New TB incidents` - shift(`New TB incidents`, n = 1L, type = "lag")) , keyby = .(area,region)]
    
    ## Add a and be as columns for sept
    sept[delta > 0,  `:=` (a = "arrow-up", b = "red"   )]
    sept[delta < 0,  `:=` (a = "arrow-up", b = "green" )]
    sept[delta == 0, `:=` (a = "",         b = "yellow")]
    
    ## setk key on area and region for faster subsets
    setkey(sept,area,region)
    
    ## New version of reactive kpidat()
    
    kpidat <- reactive({
      ## Return only the last (most recent) using data.table keys on area and region
      return( sept[.(as.character(input$dynamic2),input$select_B)][.N] )
    })
    
    output$incbox <- renderValueBox({
      tryCatch({
        ## This will return if it can execute successfully
        local_kpidat <- kpidat()
    
        valueBox(value = paste0(local_kpidat[,`New TB incidents`], "Incidents"),
                 subtitle = "Number of disease incidents.",
                 icon = local_kpidat[,a],
                 color = local_kpidat[,b])
    
      }, error = function(e) {
        ## This, an empty value box, will be returned if any errors occur
        valueBox(value = "","")
      })
    })