2016-09-11 54 views
1

我正在使用shinyTree呈現數據表。以下是目前使用代碼的數據集: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列的表格被渲染。

如何渲染所有列? 有沒有辦法可以刪除分支根/節點級別的複選框?

回答

2

廣告1.你得到這個錯誤,因爲如果你選擇了樹的第一個分支,然後catdat()返回與"PDT_TOTAL""TOTAL_VALUE_OF_MERCHANDISE"一個載體,沒有在你的數據集"TOTAL_VALUE_OF_MERCHANDISE"沒有這樣的變量。

廣告2.如果選擇所有五個選項,然後catdat()回報還"PDT_CAT"和你有同樣的問題,因爲上面 - 有你的數據集中沒有這樣的變量。 (同上 - 如果您選擇了所有選項,所以"PDT_TOTAL",它另外"TOTAL_VALUE_OF_MERCHANDISE"返回)


爲了使你可以做以下的所有列:

首先,選擇動態地從數據集中的變量,然後刪除重複因爲當選擇第一個選項TOTAL_VALUE時,catdat()返回兩次"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) 
+1

許多感謝詳細的解釋......我是在根節點不會成爲子集的一部分,因爲我在列表組外部使用它...現在它清晰...它的工作原理 – Apricot

+0

如果有幫助,你可以接受答案;) –

+1

:-)在瘋狂中淹死......謝謝一噸 – Apricot