“动作”按钮在侧边栏面板中起作用,但在ShinyApp R的主面板中不起作用?

时间:2019-07-27 20:25:32

标签: r shiny shiny-server shinyapps

我有下面的数据集。我正在尝试构建ShinhyApp,以便当用户单击位置时它应该呈现“系统”以及“系统”的计数图,这在下面的代码中做得很好。但是,单击重置按钮后,将清除操作中的条目,并且不会清除系统中的主面板。我也很好奇为什么运行ShinyApp时App会显示空表?我想知道应用程序何时运行,其中的mainPanel应该没有任何显示?是通过这种方式在Shiny中实现这两个问题吗?

我是Shiny的新手,请提供解释并提供代码

数据集:

structure(list(Systems = c("Sys1", "Sys2", "Sys3", "Sys4", "Sys6", 
"Sys7"), Locations = c("loc1", "loc1", "loc2", "loc2", "loc3", 
"loc1"), frequency = c(2L, 1L, 1L, 1L, 0L, 0L)), row.names = c(NA, 
-6L), class = "data.frame")

代码:

library(shiny)
library(DT)
library(dplyr)
library(shinyWidgets)


resetForm<-function(session){
  updateSelectInput(session,"slct1",selected = '')
}


ui <- basicPage(
  h2("Different Systems"),
  setBackgroundColor(
    color = c("#F7FBFF", "#00CED1"),
    gradient = "linear",
    direction = "bottom"),
  sidebarLayout(
    sidebarPanel(
      selectInput('slct1',"Select Location",choices = c(" ",d$Locations)),
      actionButton('clear',"Reset Form")
    ),

    mainPanel(
      DT::dataTableOutput("mytable"),
      plotOutput('out')
    )
  )
)
server <- function(input, output,session) {

  output$mytable = DT::renderDataTable({
    req(input$slct1)
    d %>%
    filter(Locations==input$slct1)
  })



  output$out<-renderPlot({

    req(input$slct1)

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

    req(nrow(data_filter)>0) #https://stackoverflow.com/questions/51427189/facet-grid-in-shiny-flexdashboard-giving-error-faceting-variables-must-have-at

    ggplot(data_filter, aes(Systems,frequency)) +
      geom_col()+
      facet_grid(.~Locations, space= "free_x", scales = "free_x")
  })


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

shinyApp(ui, server)

0 个答案:

没有答案