繁体   English   中英

R Shiny中的隐藏和显示小部件

[英]hide and show widget in R Shiny

我正在尝试根据文件夹选择隐藏和显示按钮。方案是如果用户选择文件夹,则显示按钮,否则按钮应该隐藏。 我并尝试使用Shinyjs包实现相同的目的 这是我编写的代码:

library(shiny)
library(shinyTree)
library(shinyjs)

ui <- shinyUI(
pageWithSidebar(
# Application title
headerPanel("shinyTree with 'selected' input"),

sidebarPanel(
  helpText(HTML("An example of using shinyTree's <code>get_selected</code> function to extract the cells which are currently selected.
                <hr>Created using <a href = \"http://github.com/trestletech/shinyTree\">shinyTree</a>.")),

  actionButton('submit','SUBMIT')

),
mainPanel(
  "Currently Selected:",
  verbatimTextOutput("selTxt"),
  hr(),
  shinyTree("tree")
)
))

server <- shinyServer(function(input, output, session) {
log <- c(paste0(Sys.time(), ": Interact with the tree to see the logs   here..."))

output$tree <- renderTree({
list(
  root1 = structure("123"),
  root2 = list(
    SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
    SubListB = list(leafA = "", leafB = "")
  )
 )
})

output$selTxt <- renderText({
tree <- input$tree
if (is.null(tree)){
  "None"
} else{
  unlist(get_selected(tree))

}})

observe({
if('leaf' %in% get_selected(input$tree)){
  shinyjs::show("submit")
}else{
  shinyjs::hide("submit")
}
})

})

shinyApp(ui, server)

您能否建议在这段代码中进行错误或修改才能启用隐藏和显示功能。

您需要添加useShinyjs()行来设置您的闪亮应用程序以使用Shinyjs。 此后唯一的事情就是默认隐藏按钮。 然后在get_selected(input $ tree)返回的字符串中包含模式“ leaf”时显示它。 这是你想要的吗 ?

library(shiny)
library(shinyTree)
library(shinyjs)

ui <- shinyUI(
  pageWithSidebar(
    # Application title

    headerPanel("shinyTree with 'selected' input"),

    sidebarPanel(
      helpText(HTML("An example of using shinyTree's <code>get_selected</code> function to extract the cells which are currently selected.
                    <hr>Created using <a href = \"http://github.com/trestletech/shinyTree\">shinyTree</a>.")),

      actionButton('submit','SUBMIT')

    ),
    mainPanel(
      useShinyjs(),
      "Currently Selected:",
      verbatimTextOutput("selTxt"),
      hr(),
      shinyTree("tree")
    )
  ))

server <- shinyServer(function(input, output, session) {
  log <- c(paste0(Sys.time(), ": Interact with the tree to see the logs   here..."))
  shinyjs::hide("submit")
  output$tree <- renderTree({
    list(
      root1 = structure("123"),
      root2 = list(
        SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
        SubListB = list(leafA = "", leafB = "")
      )
    )
  })

  output$selTxt <- renderText({
    tree <- input$tree
    if (is.null(tree)){
      "None"
    } else{
      unlist(get_selected(tree))

    }})

  observe({
    if(!is.null(unlist(get_selected(input$tree)))){
      if(length(unlist(strsplit(unlist(get_selected(input$tree)),"leaf")))>1){
        shinyjs::show("submit")
      }else{
        shinyjs::hide("submit")
      }
    }

  })

})

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM