shinyTree未渲染复选框输出
我正在使用shinyTree渲染数据表。以下是迄今为止使用的代码数据集:shinyTree未渲染复选框输出,shiny,shinytree,Shiny,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,
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)
库(闪亮)
图书馆(银树)
newdatAd 1。出现此错误是因为如果选择树的第一个分支,则catdat()
将返回一个向量,其中包含“PDT\u TOTAL”
和“商品的总价值”
,并且数据集中没有“商品的总价值”
这样的变量
Ad 2。如果您选择了所有五个选项,则catdat()
会另外返回“PDT\u CAT”
,并且您会遇到与上述问题相同的问题-数据集中没有此类变量。(同上-如果您选择了所有选项,因此“PDT\u总计”
,它会另外返回“商品的总值”
)
要呈现所有列,可以执行以下操作:
首先,从数据集中动态选择变量,然后删除重复项,因为当选择第一个选项时,TOTAL\u VALUE
返回两次“TOTAL\u 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
})
coldat非常感谢您的详细解释…我的印象是根节点不会是子集的一部分,因为我在列表组之外使用它…现在它很清楚了…它可以工作了
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)