【问题标题】:shinyTree: set variable to value if checkbox is checkedshinyTree:如果选中复选框,则将变量设置为值
【发布时间】:2016-08-29 15:02:57
【问题描述】:

跟进shinyTree: view without selecting

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)

我想设置一个变量,如果 I.1.2. lorem impsum 被选中,例如它的值为4

我只知道我必须使用reactive()。正如你所看到的here,我已经学会了如何使用checkboxGroupInputs 来做到这一点,但我不清楚这是否可以在shinyTree 中完成。我在网上找不到这方面的文档。

如何做到这一点?

我也看过here的功能,但不知道怎么用。

【问题讨论】:

  • 说真的,为什么要投反对票?我已经全力以赴试图解决这个问题。

标签: r shiny shinytree


【解决方案1】:

作为旁注,我真的很震惊这个包的文档是多么稀疏。

函数get_selected() 返回一个向量,如GitHub code 所示。我将使用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)

选择 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)

给予

根据需要。

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    相关资源
    最近更新 更多