shinyTree 不呈现复选框输出

shinyTree not rendering checkbox output

我正在使用 shinyTree 渲染数据 table。以下是到目前为止使用过的代码数据集:

library(shiny)
library(shinyTree)

newdat <- structure(list(RESPID = c("41000123", "41004132", "41006132", 
"41007121", "41007123"), PDT_A = c(125, 66, 45, 28, 
0), PDT_B = c(10, 0, 0, 0, 0), PDT_C = c(0, 0, 0, 0, 0), PDT_D = c(450, 
105, 75, 192, 0), PDT_TOTAL = c(585, 171, 120, 220, 0)), .Names = c("RESPID", 
"PDT_A", "PDT_B", "PDT_C", "PDT_D", "PDT_TOTAL"), row.names = c("6", 
"40", "56", "59", "61"), class = "data.frame")


server <- shinyServer(function(input, output, session) {

    newdata <- reactive({newdat})

  output$tree <- renderTree({
    sss=list('TOTAL_VALUE'= list('TOTAL_VALUE_OF_MERCHANDISE'   =  structure(list('PDT_TOTAL'='1001'), stopened=FALSE),
        'PDT_CAT'   =  structure(list('PDT_TOTAL'='1002','PDT_A'='152','PDT_B'='153','PDT_C'='154','PDT_D'='155'), stopened=FALSE)
        ))
    attr(sss[[1]],"stopened")=FALSE 
    sss
  })

  catdat <- reactive({
      tree <- input$tree
      unlist(get_selected(tree))
  })

  coldat <- reactive({
      newdata()[,catdat()]
  })

  output$datatab <- renderDataTable({
        coldat()
  })


})


ui <- shinyUI(
  pageWithSidebar(
    headerPanel("TEST"),
    sidebarPanel(
      shinyTree("tree", checkbox = TRUE)
    ),
    mainPanel(
      dataTableOutput("datatab")
    )
  ))

shinyApp(ui,server)

生成树。我在通过数据 table 输出呈现列时遇到以下问题:

  1. 树的第一个分支,仅指一个列:未呈现闪亮。我收到错误消息 undefined columns selected.

  2. 树的第二个分支应该呈现 table 的所有五列。但是它只呈现任意四列。

如果我 select 第二个分支的根,我得到相同的 undefined columns selected。当我取消选中其中一个分支时,将呈现具有 4 列的 table。

如何呈现所有列? 有没有一种方法可以删除分支根/节点级别的复选框?

Ad 1. 你得到这个错误是因为如果你 select 树的第一个分支,那么 catdat() returns 一个向量使用 "PDT_TOTAL""TOTAL_VALUE_OF_MERCHANDISE" 并且您的数据集中没有 "TOTAL_VALUE_OF_MERCHANDISE" 这样的变量。

广告 2. 如果你 select 所有五个选项然后 catdat() returns 另外 "PDT_CAT" 并且你有相同的问题如上 - 您的数据集中没有这样的变量。 (同上 - 如果你 select 所有选项,那么 "PDT_TOTAL",它 returns 另外 "TOTAL_VALUE_OF_MERCHANDISE"


要呈现所有列,您可以执行以下操作:

首先,select 从您的数据集中动态变量,然后删除重复项作为 catdat() returns 两次 "TOTAL_VALUE" 当第一个选项 TOTAL_VALUE 是 selected.

还有另一个问题:newdata()[,vars] returns 一个向量如果只有一个变量 selected 和 renderDataTable 不会打印任何东西,因为它只工作与数据框。要解决此问题,您可以删除 , 以确保子集 returns 始终是数据帧 - newdata()[vars]

coldat <- reactive({
    vars <- catdat()
    vars <- vars[!(vars %in% c("TOTAL_VALUE", "TOTAL_VALUE_OF_MERCHANDISE", "PDT_CAT"))]
    vars <- unique(vars)
    print(vars)

    # newdata()[,vars] # If you select only one variable then this reactive returns an object of class numeric and not a data.frame
    newdata()[vars] # remove "," and it will always return a data frame
  })

完整示例:

library(shiny)
library(shinyTree)

newdat <- structure(list(RESPID = c("41000123", "41004132", "41006132", 
                                    "41007121", "41007123"), PDT_A = c(125, 66, 45, 28, 
                                                                       0), PDT_B = c(10, 0, 0, 0, 0), PDT_C = c(0, 0, 0, 0, 0), PDT_D = c(450, 
                                                                                                                                          105, 75, 192, 0), PDT_TOTAL = c(585, 171, 120, 220, 0)), .Names = c("RESPID", 
                                                                                                                                                                                                              "PDT_A", "PDT_B", "PDT_C", "PDT_D", "PDT_TOTAL"), row.names = c("6", 
                                                                                                                                                                                                                                                                              "40", "56", "59", "61"), class = "data.frame")


server <- shinyServer(function(input, output, session) {

  newdata <- reactive({newdat})

  output$tree <- renderTree({
    sss=list('TOTAL_VALUE'= list('TOTAL_VALUE_OF_MERCHANDISE'   =  structure(list('PDT_TOTAL'='1001'), stopened=FALSE),
                                 'PDT_CAT'   =  structure(list('PDT_TOTAL'='1002','PDT_A'='152','PDT_B'='153','PDT_C'='154','PDT_D'='155'), stopened=FALSE)
    ))
    attr(sss[[1]],"stopened")=FALSE 
    sss
  })

  catdat <- reactive({
    tree <- input$tree
    unlist(get_selected(tree))
  })

  coldat <- reactive({
    vars <- catdat()
    vars <- vars[!(vars %in% c("TOTAL_VALUE", "TOTAL_VALUE_OF_MERCHANDISE", "PDT_CAT"))]
    vars <- unique(vars)
    print(vars)

    # newdata()[,vars] # If you select only one variable then this reactive returns an object of class numeric and not a data.frame
    newdata()[vars] # remove "," and it will always return a data frame
  })

  output$datatab <- renderDataTable({
    coldat()
  })


})


ui <- shinyUI(
  pageWithSidebar(
    headerPanel("TEST"),
    sidebarPanel(
      shinyTree("tree", checkbox = TRUE)
    ),
    mainPanel(
      dataTableOutput("datatab")
    )
  ))

shinyApp(ui,server)