在用户从 R 的下拉列表中选择选项之前,如何在 Siny Dashboardbody 中显示空白仪表板?

How to display blank dashboard in Siny Dashboardbody until user selects options from drop down in R?

我的数据框如下所示。我正在尝试创建 ShinyDashboard,这样用户应该能够从侧边栏面板中选择 select 选项,并且基于用户 selection Plot 和 Table 应该在 MainPanel 中呈现。目前,下面的代码正在做我想要的。但是,以下代码存在两个问题:

问题#1:每当应用程序处于 运行 时,用户都可以看到空白 table 和空白图,如下所示。相反,只要应用程序 运行,主面板应该是空的,如屏幕截图#2

所示

截图#1

预期结果:ScreenShot#2

问题#2:目前重置按钮只能重置侧边栏中的selection。我想创建仪表板,以便在用户 selects "clear Form" 按钮时主面板显示空屏幕。

数据框:

structure(list(Systems = c("Sys1", "Sys1", "Sys2", "Sys3", "Sys4", 
"Sys6", "Sys7"), Locations = c("loc1", "loc1", "loc1", "loc2", 
"loc2", "loc3", "loc1"), year = structure(c(2L, 1L, 1L, 1L, 1L, 
3L, 3L), .Label = c("2019", "2018", "0"), class = "factor"), 
    frequency = c(1L, 2L, 1L, 1L, 1L, 0L, 0L), freq_cal = c(33.33, 
    66.67, 100, 100, 100, 0, 0), label = c("33.33%", "66.67%", 
    "100.00%", "100.00%", "100.00%", "0.00%", "0.00%")), row.names = c(NA, 
-7L), class = "data.frame")

代码:

library(shiny)
library(shinydashboard)


resetForm<-function(session){
  updateSelectInput(session,"slct1",selected = '')
}
ui<-dashboardPage(
    dashboardHeader(title="System Tracker"),
    dashboardSidebar(
      selectInput('slct1',"Select Location",choices = c(" ",d$Locations)),
      actionButton('clear',"Reset Form"),
      h4("Powered by:"),
      tags$img(src='baka.png',height=50,width=50)
    ),
    dashboardBody(
      fluidRow(
        box( DT::dataTableOutput("mytable")),
             box(plotlyOutput('out'))

      )
    )
)


server<-function(input, output,session) {
  output$mytable = DT::renderDataTable({
    req(input$slct1)

    d %>%
      filter(Locations==input$slct1)

  })


  output$out<-renderPlotly({

    req(input$slct1)
    data_filter<-dd %>%
      filter(Locations==input$slct1)

    req(nrow(data_filter)>0) #

    ggplotly(ggplot(data_filter, aes(Systems,frequency,fill=year)) +
               geom_col(position = 'stack')+geom_text(aes(label=label), position = position_stack(vjust = .5))+
               facet_grid(.~Locations, space= "free_x", scales = "free_x"))

  })


  observeEvent(input$clear,{
    req(input$slct1)
    resetForm(session)
  })
}

shinyApp(ui, server)

提供代码说明。

这里的答案似乎是conditionalPanel()。条件面板只是根据条件显示其内容,该条件可能基于您的输入位置。

如果您将输出编辑为如下所示:

conditionalPanel(
  #Uses a Javascript formatted condition
  condition="input.slct1 !== ' '",
  box( DT::dataTableOutput("mytable")),
             box(plotlyOutput('out'))
)

并将 resetForm() 函数编辑为

updateSelectInput(session,"slct1",selected = ' ')

您应该会收到想要的结果。