在闪亮的仪表板中隐藏元素(框/标签)

问题描述 投票:2回答:3

我有一个闪亮的仪表板,在登录页面上只有一个文本框。用户输入显示相关数据的emailid。这很好用。但是我需要一个盒子/选项卡面板,当用户开始在文本输入中输入文本(emailid)时,它会在到达页面时迎接用户并消失。这可能吗?

output$introbox=renderUI(box(h3("Welcome to the page. Please enter your email id to proceed")),
                                conditionalPanel(condition=input.emailid=="")

该框显示在页面的着陆上,但在输入文本时不会消失。

感谢任何帮助。谢谢

r shiny shinydashboard shinyjs
3个回答
10
投票

奥斯卡的回答是正确的。但它实际上并不使用shinyjs,而是手动包含所有JavaScript。你可以使用他的答案,但这里是使用shinyjs重写他的答案

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <-dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
  ),
  dashboardBody(
    useShinyjs(),
    div(id = "greetbox-outer",
      box( id ="greetbox",
           width  = 12, 
           height = "100%",
           solidHeader = TRUE, 
           status = "info",
           div(id="greeting", "Greeting here") 
      )
    ),
    box( id ="box",
         width  = 12, 
         height = "100%",
         solidHeader = TRUE, 
         status = "success",

         textInput("txtbx","Enter text: ")
    )
      )
    )

server <- shinyServer(function(input, output, session) {
  observeEvent(input$txtbx,{
    if (input$txtbx == "") return(NULL)
    hide(id = "greetbox-outer", anim = TRUE)
    print(input$txtbx)
  })
})

shinyApp(ui = ui, server = server) 

3
投票

是的,这是可能的,因为daattali sugested shinyjs可以帮助您完成一些标准的Javascript任务。

如果你想隐藏shinydashboard box元素你必须(据我所知)使用这样的自定义Javascript:

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <-dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
  ),
  dashboardBody(
    tags$head(
      tags$script(
        HTML("
        Shiny.addCustomMessageHandler ('hide',function (selector) {
          $(selector).parent().slideUp();
        });"
        )
      )
    ),
    box( id ="greetbox",
         width  = 12, 
         height = "100%",
         solidHeader = TRUE, 
         status = "info",
         div(id="greeting", "Greeting here") 
    ),
    box( id ="box",
         width  = 12, 
         height = "100%",
         solidHeader = TRUE, 
         status = "success",

         textInput("txtbx","Enter text: ")
    )
  )
)

server <- shinyServer(function(input, output, session) {
  observeEvent(input$txtbx,{
    if (input$txtbx == "") return(NULL)
    session$sendCustomMessage (type="hide", "#greetbox")
    print(input$txtbx)
  })
})

shinyApp(ui = ui, server = server) 

该框的html布局如下所示:

<div class="box box-solid box-info">
    <div class="box-body" id="greetbox">
        <!-- Box content here -->
    </div>
</div>

由于我们想要隐藏整个框,我们必须将父元素隐藏到框函数中的id集,因此jQuery片段。


1
投票

我有一个类似的问题,我的问题是box():如果我将box()改为div(),那么show / hide选项工作得很好。

这个解决方案更简单,但不如修改标签那么优雅。只需将你的box()包裹在这样的div()上:

div(id = box1, box(...))
div(id = box2, box(...))

然后,使用div的id调用show / hide。

© www.soinside.com 2019 - 2024. All rights reserved.