shinyTree: set variable to value if checkbox is checked

三世轮回 提交于 2019-12-10 22:02:19

问题


Follow-up to 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 would like to set a variable such that if I.1.2. lorem impsum is selected, it has a value of 4, for example.

All I know is that I have to use reactive(). As you can see here, I've learned how to do this with checkboxGroupInputs, but it is unclear to me if this can even be done within a shinyTree. There is no documentation on this that I can find online.

How can this be done?

I have also seen the functions here but am not sure how to use them.


回答1:


As a side note, I'm really shocked how sparse the documentation is for this package.

The function get_selected() returns a vector, as it can be seen in the GitHub code. I am going to use format = "slices".

Consider the following code:

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)

Upon selecting I.1.2. lorem impsum, the following is returned:

This is a vector of length 1 with the column name. Notice that dots are being used instead of spaces.

Thus, if we want to set a variable x equal to 4 when this is selected, we should see if I.1.2.lorem.impsum is in the names above, and then perform the assignment.

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)

giving

as desired.



来源:https://stackoverflow.com/questions/39209411/shinytree-set-variable-to-value-if-checkbox-is-checked

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!