【问题标题】:shinyTree not rendering checkbox outputshinyTree 不呈现复选框输出
【发布时间】:2016-09-11 08:13:02
【问题描述】:

我正在使用 shinyTree 来呈现数据表。以下是目前使用的代码数据集:

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)

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

  1. 树的第一个分支,仅指一列:它不会以闪亮的方式呈现。我收到一条错误消息undefined columns selected

  2. 树的第二个分支应该呈现表格的所有五列。然而,它只呈现任何四列。

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

如何渲染所有列? 有没有办法可以删除分支根/节点级别的复选框?

【问题讨论】:

    标签: shiny shinytree


    【解决方案1】:

    广告 1。 您会收到此错误,因为如果您选择树的第一个分支,则 catdat() 会返回一个带有 "PDT_TOTAL""TOTAL_VALUE_OF_MERCHANDISE" 的向量,并且不存在如下变量"TOTAL_VALUE_OF_MERCHANDISE" 在您的数据集中。

    广告 2。 如果您选择所有五个选项,则 catdat() 会另外返回 "PDT_CAT",并且您遇到与上述相同的问题 - 您的数据集中没有这样的变量。 (同上 - 如果你选择所有选项,所以"PDT_TOTAL",它会额外返回"TOTAL_VALUE_OF_MERCHANDISE"


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

    首先,从数据集中选择动态变量,然后删除重复键,如catdat() 987654330 @选择了第一个选项TOTAL_VALUE

    还有另一个问题:newdata()[,vars] 如果只选择了一个变量,则返回一个向量,而renderDataTable 不会打印任何内容,因为它仅适用于数据帧。要解决此问题,您可以删除 , 以确保子集始终返回数据框 - 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)
    

    【讨论】:

    • 非常感谢您的详细解释...我的印象是根节点不会成为子集的一部分,因为我在列表组之外使用它...现在很清楚了...它有效
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多