library(shiny)
library(shinyTree)
server <- shinyServer(function(input, output, session) {
output$tree <- renderTree({
sss=list( 'I lorem impsum'= list(
'I.1 lorem impsum' = structure(list('I.1.1 lorem impsum'='1', 'I.1.2 lorem impsum'='2'),stopened=TRUE),
'I.2 lorem impsum' = structure(list('I.2.1 lorem impsum'='3'), stopened=TRUE)))
attr(sss[[1]],"stopened")=TRUE
sss
})
})
ui <- shinyUI(
shiny::fluidPage(
h4('Shiny hierarchical checkbox')
,shinyTree("tree", checkbox = TRUE)
)
)
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我想设置一个变量,如果 I.1.2。lorem impsum 被选中,它的值为4
,例如。
我所知道的是我必须使用reactive()
. 正如你在这里看到的,我已经学会了如何用checkboxGroupInput
s来做到这一点,但我不清楚这是否可以在shinyTree
. 我在网上找不到这方面的文档。
如何才能做到这一点?
顺便说一句,我真的很震惊这个包的文档如此稀疏。
该函数返回一个向量,如GitHub 代码get_selected()
中所示。我要使用.format = "slices"
考虑以下代码:
library(shiny)
library(shinyTree)
ui <- shinyUI(
shiny::fluidPage(
h4('Shiny hierarchical checkbox'),
shinyTree("tree", checkbox = TRUE),
# table of weights
fluidRow(column("",
tableOutput("Table"), width = 12,
align = "center"))
)
)
server <- shinyServer(function(input, output, session) {
output$tree <- renderTree({
sss=list( 'I lorem impsum'= list(
'I.1 lorem impsum' = structure(list('I.1.1 lorem impsum'='1', 'I.1.2 lorem impsum'='2'),stopened=TRUE),
'I.2 lorem impsum' = structure(list('I.2.1 lorem impsum'='3'), stopened=TRUE)))
attr(sss[[1]],"stopened")=TRUE
sss
})
output$Table <- renderPrint({
names(as.data.frame(get_selected(input$tree, format = "slices")))
})
})
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
选择 I.1.2 后。lorem impsum,返回以下内容:
这是一个长度为 1 且带有列名称的向量。请注意,使用的是点而不是空格。
因此,如果我们想设置一个变量x
等于4
选择它时的值,我们应该看看上面是否I.1.2.lorem.impsum
是names
,然后执行赋值。
library(shiny)
library(shinyTree)
ui <- shinyUI(
shiny::fluidPage(
h4('Shiny hierarchical checkbox'),
shinyTree("tree", checkbox = TRUE),
fluidRow(column("",
tableOutput("Table"), width = 12,
align = "center")),
fluidRow(column("",
tableOutput("Table2"), width = 12,
align = "center"))
)
)
server <- shinyServer(function(input, output, session) {
output$tree <- renderTree({
sss=list( 'I lorem impsum'= list(
'I.1 lorem impsum' = structure(list('I.1.1 lorem impsum'='1', 'I.1.2 lorem impsum'='2'),stopened=TRUE),
'I.2 lorem impsum' = structure(list('I.2.1 lorem impsum'='3'), stopened=TRUE)))
attr(sss[[1]],"stopened")=TRUE
sss
})
x <- reactive({
if('I.1.2.lorem.impsum' %in% names(
as.data.frame(
get_selected(
input$tree, format = "slices")))){
x <- 4
}
})
output$Table <- renderPrint({
names(as.data.frame(get_selected(input$tree, format = "slices")))
})
output$Table2 <- renderTable({
as.data.frame(x())
})
})
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
给予
如预期的。