无法在Shiny应用程序中的Flexdashboard库中渲染仪表

时间:2018-08-20 14:05:02

标签: r shiny

我正在尝试创建一个闪亮的应用 a)提示用户上传包含数字数据的文件, b)读取文件并将数据点分配给不同的变量, c)从捕获的变量中计算新变量 d)使用计算出的变量显示“量规”

代码成功执行,但仪表图未正确显示。也没有错误或警告消息。相反,我收到以下消息:

“向asJSON(keep_vec_names = TRUE)的输入是命名向量。在jsonlite的未来版本中,将不支持此选项,命名向量将转换为数组而不是对象。如果要JSON对象输出,请改用命名列表。请参见?toJSON。”

而不是仪表的输出,我在中间得到了一个很小的规格,如所附图像所示。

整个代码相当长,因此仅提供相关的代码片段。

如果能帮助解决此问题,我们将非常感谢。

library(shiny)
library(flexdashboard)

ui <- fluidPage(

             tabPanel("Sensitivity Analysis",
                      sidebarLayout(
                        sidebarPanel(
                          uiOutput("Sensitivity_Analysis")
                        ),
                        mainPanel(
                          gaugeOutput("sensitivity", width = "600px", height = "600px")
                        )
                      )
             ),


server <- function (input, output)
{

  output$input_financials=renderUI({
    fluidRow(fileInput("file1", "Choose CSV File",multiple = FALSE,accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),
             actionButton("process","Process"))})


data_input=reactiveValues()

  observeEvent(input$process,{
    file_input <- input$file1
    if (is.null(file_input)) {
      return(NULL)}

  ## File is read and all the inputs are assigned to variables
  ....


  ## Output for Gauge begins
  output$sensitivity <- flexdashboard::renderGauge({


    gauge_limit <-  data_input$wc_value

    data_input$cash_rel_dpo <- ## Formula for cash_del_dpo
    data_input$cash_rel_dro <- ## Formula for cash_del_dro
    data_input$cash_rel_dio <- ## Formula for cash_del_dio
    data_input$wc_predicted_value <- (data_input$wc_predicted_value - data_input$cash_rel_dpo - data_input$cash_rel_dro - data_input$cash_rel_dio)
    gauge(data_input$wc_predicted_value, min = 0, max = gauge_limit, 
          gaugeSectors(success = c(0, 10000), 
                       warning = c(10001, 50000),
                       danger = c(50001, 1000000000))
    )
  })

shinyApp(ui = ui, server = server)

执行代码后生成的输出的屏幕截图

1 个答案:

答案 0 :(得分:0)

billboarder包中有一个类似的量规,请尝试以下示例:

library(shiny)
library(billboarder)

ui <- fluidPage(
  tabPanel(
    title = "Sensitivity Analysis",
    sidebarLayout(
      sidebarPanel(
        fileInput("file1", "Choose CSV File",
                  multiple = FALSE,
                  accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),
        actionButton(inputId = "process", label = "Process (click here to refresh the gauge)")
      ),
      mainPanel(
        billboarderOutput("sensitivity", width = "400px", height = "400px")
      )
    )
  )
)


server <- function (input, output) {

  data_input <- reactiveValues(x = 0)

  observeEvent(input$process, {
    data_input$x <- sample.int(1e5, size = 1)
  }, ignoreInit = TRUE)


  ## Output for Gauge begins
  output$sensitivity <- renderBillboarder({
    billboarder() %>% 
      bb_gaugechart(
        value = data_input$x, 
        name = "Predicted value",
        steps = c(1e4, 5e4, 1e5),
        steps_color = rev(c("#FF0000","#F6C600", "#60B044"))
      ) %>% 
      bb_gauge(
        min = 0, max = 1e5,
        units = "",
        label = list(
          format = htmlwidgets::JS("function(value, ratio) {return d3.format(',')(value);}") # format value with thousand separator
        ),
        width = 80
      )
  })
}

shinyApp(ui = ui, server = server)