简体   繁体   English

R shiny 根据用户输入过滤数据并更新 plot

[英]R shiny filter data based on user input and update the plot

I have an shiny application where I am filtering data based on 3 user inputs (dropdown, date and checkbox).我有一个 shiny 应用程序,我在其中根据 3 个用户输入(下拉列表、日期和复选框)过滤数据。 How do we update the plot based on user selection.我们如何根据用户选择更新 plot。 Example, do not show the plot when no checkbox is selected.例如,当没有选中复选框时不显示 plot。 Also how to color the plot based on checkbox selection.Also is there a way to plot all the data as default screen with examiner in x-axis and totals in y axis还有如何根据复选框选择为 plot 着色。还有一种方法可以将 plot 的所有数据作为默认屏幕,x 轴为检查者,y 轴为总计

mydata<-structure(list(Id = structure(c(1L, 11L, 19L, 27L, 28L, 29L, 
30L, 31L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 25L, 26L), .Label = c("DB-1", 
"DB-11", "DB-12", "DB-13", "DB-14", "DB-15", "DB-16", "DB-17", 
"DB-18", "DB-19", "DB-2", "DB-20", "DB-23", "DB-25", "DB-26", 
"DB-27", "DB-28", "DB-29", "DB-3", "DB-30", "DB-31", "DB-32", 
"DB-34", "DB-35", "DB-36", "DB-37", "DB-4", "DB-5", "DB-6", "DB-7", 
"DB-9"), class = "factor"), examiner = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Alex", 
"Jhon", "Kim", "Maymoon", "Mike"), class = "factor"), Relationship = structure(c(4L, 
2L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 
3L, 2L, 3L, 3L, 3L, 3L, 4L, 1L, 2L, 2L, 2L, 2L, 3L, 1L), .Label = c("father", 
"mother", "self", "sibling"), class = "factor"), application_date = structure(c(10L, 
6L, 8L, 3L, 6L, 3L, 7L, 15L, 20L, 2L, 20L, 3L, 14L, 11L, 2L, 
8L, 10L, 5L, 20L, 14L, 13L, 11L, 17L, 12L, 1L, 16L, 19L, 9L, 
18L, 21L, 4L), .Label = c("1/10/19", "1/15/19", "11/13/18", "11/15/18", 
"11/20/18", "11/27/18", "11/28/18", "11/30/18", "12/20/18", "12/4/18", 
"12/6/18", "12/7/18", "2/14/19", "2/25/19", "2/26/19", "3/12/19", 
"3/14/19", "3/21/19", "3/22/19", "4/3/19", "4/5/19"), class = "factor"), 
    gender = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 
    2L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("female", "male"), class = "factor"), 
    stage1_date = structure(c(8L, 3L, 6L, 2L, 3L, 7L, 3L, 10L, 
    13L, 11L, 13L, 2L, 10L, 3L, 11L, 5L, 1L, 3L, 7L, 17L, 12L, 
    1L, 14L, 9L, 5L, 16L, 15L, 4L, 16L, 1L, 3L), .Label = c("", 
    "1/10/19 21:40", "1/10/19 21:45", "1/17/19 19:26", "1/31/19 20:25", 
    "1/9/19 19:50", "1/9/20 14:50", "2/21/19 21:15", "2/6/19 20:36", 
    "3/15/19 16:50", "3/21/19 18:21", "3/4/19 16:30", "4/26/19 19:20", 
    "4/8/19 12:40", "4/8/19 12:41", "5/1/19 18:05", "7/30/19 15:10"
    ), class = "factor"), stage2_date = structure(c(1L, 1L, 1L, 
    1L, 4L, 1L, 9L, 1L, 2L, 1L, 10L, 7L, 8L, 1L, 1L, 1L, 5L, 
    1L, 1L, 6L, 2L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L), .Label = c("", 
    "5/11/21 17:37", "5/11/21 17:42", "5/11/21 17:50", "5/11/21 17:52", 
    "5/14/21 16:07", "5/15/21 16:07", "5/16/21 16:07", "5/21/21 17:46", 
    "5/21/21 17:47"), class = "factor"), status_stage1_2019 = c(1L, 
    1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L
    ), status_stage1_2020 = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), status_stage1_2021 = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), status_stage1_2022 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), status_stage2_2020 = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), status_stage2_2021 = c(0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
    1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), status_stage2_2022 = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    )), class = "data.frame", row.names = c(NA, -31L))

My code我的代码

server <- function(input, output, session) {
  #Summarize Data and then Plot
  data <- reactive({
    
    req(input$examiner)
   
    mydata %>%
      dplyr::filter(
        examiner %in% input$examiner ,
if_any(matches(str_c('status_', tolower(input$stage))), ~ 
                 .x ==1),
        stage1_date >= input$daterange[1] &
          stage1_date <= input$daterange[2]
      ) %>%
      group_by(Relationship) %>% summarize(Total = n())
    
  })
output$selected_var <- renderText({ 
    paste("You have chosen ", input$examiner)
  }) 
  #Plot 
  output$plot <- renderPlot({
    req(data())
    g <- ggplot(data(), aes( y = Total, x = Relationship))
    g + geom_bar(stat = "sum")
  })
}
ui <- basicPage(
  titlePanel("My Dashboard"),
  helpText("Shows my data"),
  sidebarPanel(
    selectInput(inputId = "examiner",
                label = h5("Select examiner"),
                choices = c(as.character(mydata$examiner))
    ),
    dateRangeInput("daterange",
                   h5("SelectDates"),
                   format="yyyy-mm-dd",
                   start = "2001-01-01"
    ),
    checkboxGroupInput("stage",
                  h5("Select stage"),
                  choices = c("Stage1","Stage2"),
                  selected = c("Stage1","Stage2")
                  )
    ),
  
  mainPanel(
    textOutput("selected_var"),
    plotOutput("plot")
    
  )
)

The problem is the class of stage1_date -> it is factor.问题是 stage1_date 的stage1_date -> 它是因素。 Use lubridate s ymd_hm function to transform it to date.使用lubridate s ymd_hm function 将其转换为最新。 Then your code will work!然后你的代码就可以工作了!

Just add this line of code straight after my_data %>% :只需在my_data %>%之后直接添加这行代码:

library(lubridate)
....
....

    mydata %>%
      mutate(stage1_date = mdy_hm(as.character(stage1_date))) %>%
....
....

Here is the complete code with working app:这是带有工作应用程序的完整代码:

library(shiny)
library(tidyverse)
library(lubridate)


mydata<-structure(list(Id = structure(c(1L, 11L, 19L, 27L, 28L, 29L, 
30L, 31L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 25L, 26L), .Label = c("DB-1", 
"DB-11", "DB-12", "DB-13", "DB-14", "DB-15", "DB-16", "DB-17", 
"DB-18", "DB-19", "DB-2", "DB-20", "DB-23", "DB-25", "DB-26", 
"DB-27", "DB-28", "DB-29", "DB-3", "DB-30", "DB-31", "DB-32", 
"DB-34", "DB-35", "DB-36", "DB-37", "DB-4", "DB-5", "DB-6", "DB-7", 
"DB-9"), class = "factor"), examiner = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Alex", 
"Jhon", "Kim", "Maymoon", "Mike"), class = "factor"), Relationship = structure(c(4L, 
2L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 
3L, 2L, 3L, 3L, 3L, 3L, 4L, 1L, 2L, 2L, 2L, 2L, 3L, 1L), .Label = c("father", 
"mother", "self", "sibling"), class = "factor"), application_date = structure(c(10L, 
6L, 8L, 3L, 6L, 3L, 7L, 15L, 20L, 2L, 20L, 3L, 14L, 11L, 2L, 
8L, 10L, 5L, 20L, 14L, 13L, 11L, 17L, 12L, 1L, 16L, 19L, 9L, 
18L, 21L, 4L), .Label = c("1/10/19", "1/15/19", "11/13/18", "11/15/18", 
"11/20/18", "11/27/18", "11/28/18", "11/30/18", "12/20/18", "12/4/18", 
"12/6/18", "12/7/18", "2/14/19", "2/25/19", "2/26/19", "3/12/19", 
"3/14/19", "3/21/19", "3/22/19", "4/3/19", "4/5/19"), class = "factor"), 
gender = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("female", "male"), class = "factor"), 
stage1_date = structure(c(8L, 3L, 6L, 2L, 3L, 7L, 3L, 10L, 
13L, 11L, 13L, 2L, 10L, 3L, 11L, 5L, 1L, 3L, 7L, 17L, 12L, 
1L, 14L, 9L, 5L, 16L, 15L, 4L, 16L, 1L, 3L), .Label = c("", 
"1/10/19 21:40", "1/10/19 21:45", "1/17/19 19:26", "1/31/19 20:25", 
"1/9/19 19:50", "1/9/20 14:50", "2/21/19 21:15", "2/6/19 20:36", 
"3/15/19 16:50", "3/21/19 18:21", "3/4/19 16:30", "4/26/19 19:20", 
"4/8/19 12:40", "4/8/19 12:41", "5/1/19 18:05", "7/30/19 15:10"
), class = "factor"), stage2_date = structure(c(1L, 1L, 1L, 
1L, 4L, 1L, 9L, 1L, 2L, 1L, 10L, 7L, 8L, 1L, 1L, 1L, 5L, 
1L, 1L, 6L, 2L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L), .Label = c("", 
"5/11/21 17:37", "5/11/21 17:42", "5/11/21 17:50", "5/11/21 17:52", 
"5/14/21 16:07", "5/15/21 16:07", "5/16/21 16:07", "5/21/21 17:46", 
"5/21/21 17:47"), class = "factor"), status_stage1_2019 = c(1L, 
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L
), status_stage1_2020 = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), status_stage1_2021 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), status_stage1_2022 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), status_stage2_2020 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), status_stage2_2021 = c(0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), status_stage2_2022 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
)), class = "data.frame", row.names = c(NA, -31L))

 
server <- function(input, output, session) {
  #Summarize Data and then Plot
  data <- reactive({
    
    req(input$examiner)
    
    mydata %>%
      mutate(stage1_date = mdy_hm(as.character(stage1_date))) %>% 
      dplyr::filter(
        examiner %in% input$examiner ,
        if_any(matches(str_c('status_', tolower(input$stage))), ~ 
                 .x ==1),
        stage1_date >= input$daterange[1] &
          stage1_date <= input$daterange[2]
      ) %>%
      group_by(Relationship) %>% summarize(Total = n())
    
  })
  output$selected_var <- renderText({ 
    paste("You have chosen ", input$examiner)
  }) 
  #Plot 
  output$plot <- renderPlot({
    req(data())
    g <- ggplot(data(), aes( y = Total, x = Relationship))
    g + geom_bar(stat = "sum")
    #browser()
  })
 # browser()
}
ui <- basicPage(
  titlePanel("My Dashboard"),
  helpText("Shows my data"),
  sidebarPanel(
    selectInput(inputId = "examiner",
                label = h5("Select examiner"),
                choices = c(as.character(mydata$examiner))
    ),
    dateRangeInput("daterange",
                   h5("SelectDates"),
                   format="yyyy-mm-dd",
                   start = "2001-01-01"
    ),
    checkboxGroupInput("stage",
                       h5("Select stage"),
                       choices = c("Stage1","Stage2"),
                       selected = c("Stage1","Stage2")
    )
  ),
  
  mainPanel(
    textOutput("selected_var"),
    plotOutput("plot")
    
  )
)

shinyApp(ui, server)

在此处输入图像描述

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

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