如何根据用户选择在Shiny R中显示计数图?

时间:2019-07-26 02:30:19

标签: r shiny shiny-server shinyapps

这个问题还是这个question的扩展。基本上,我想根据用户选择将Countplot和表格一起呈现。我可以使用下面的代码来渲染图。但是,下面的代码显示整个数据集的计数图。我想构建该应用程序,以便当用户选择系统1时,它应该显示系统1的频率计数以及该地块上其他系统的其他名称。我不确定如何实现此目的在Shiny中,因为我是Shiny的新手。 请提供带有代码的说明。

用于渲染CountPlot的代码

df <- data.frame("Users" =c('A',"B","A",'C','B'), "Date" = c('17 Mar 2019','15 Mar 2019','11 Mar 2019','20 Apr 2019',"21 Apr 2019"), "Systems" = c("Sys1", "Sys1","Sys2","Sys3","Sys4"), stringsAsFactors = FALSE)
df
library(shiny)
library(DT)
library(dplyr)
library(shinyWidgets)

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


ui <- basicPage(
  h2("Different Systems"),
  setBackgroundColor(
    color = c("#F7FBFF", "#00CED1"),
    gradient = "linear",
    direction = "bottom"),
  sidebarLayout(
    sidebarPanel(
      selectInput('slct',"Select System",choices = c("",df$Systems)),
      actionButton('clear',"Reset Form")
    ),
    mainPanel(
      DT::dataTableOutput("mytable"),
      plotOutput('out')
    )
  )
)
server <- function(input, output,session) {
  #df$system<-rownames(df$Systems)
  output$mytable = DT::renderDataTable({
    req(input$slct) # add this line
    df %>%
      filter(stringr::str_detect(Systems, as.character(input$slct)))
  })

  filter_data<-reactive({
    dplyr::filter(df, Systems == input$slct)
  })

  output$out<-renderPlot({
    req(input$slct)
  #  syst<-reactive({input$slct})

    ggplot(df, aes(x = Systems,fill=Systems)) + geom_bar(stat = 'count')
  })


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

shinyApp(ui, server)

0 个答案:

没有答案