简体   繁体   中英

Create a dataset with dynamic number of rows based on shiny input

I have the following shiny app up with the data included. I want to create a line chart which will have in the x-axis the Date selected and in the y-axis the number of cases as they are computed by bupaR::n_cases(mydata2()) . So basically I need the initial dataset to be reformated in a way that it will have 2 columns Month and Cases and I will be able to include them on the chart but with the correct chronological order. For example December 20 must be always be displayed before January 21 even if January 21 is selected first. I do not have problem if the Date columns must be reformated as well in order to achive this (for example it may be 2020-12 and 2021-01

library(shiny)
library(bupaR)
mydata<-structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689", 
                                   "WC4121068", "WC4120667", "WC4121937", "WC4121767", "WC4121952", 
                                   "WC4121889", "WC4121921", "WC4121870"), lifecycle = c(110, 110, 
                                                                                         110, 110, 120, 110, 110, 110, 110, 10, 110), action = c("WC4120721-CN354877", 
                                                                                                                                                 "WC4120667-CN354878", "WC4120689-CN356752", "WC4121068-CN301950", 
                                                                                                                                                 "WC4120667-CSW310", "WC4121937-CN301302", "WC4121767-CN354089", 
                                                                                                                                                 "WC4121952-CN355950", "WC4121889-CN301291", "WC4121921-CSW304", 
                                                                                                                                                 "WC4121870-CN301116"), activity = c("Forged Wire, Medium (Sport)", 
                                                                                                                                                                                     "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", 
                                                                                                                                                                                     "Forged Wire, Medium (Sport)", "BBH-1&2", "Needle Point", "Brazing Machines", 
                                                                                                                                                                                     "Needle Point", "Needle Point", "SOLO Salt", "Needle Point"), 
                       resource = c("3419", "3216", "3409", "3201", "C3-100", "6206", 
                                    "7709", "6201", "5202", "C3-040", "5503"), timestamp = structure(c(1606975200, 
                                                                                                       1607126280, 1607446560, 1607578920, 1607641020, 1609695420, 
                                                                                                       1609696800, 1609697040, 1609697460, 1609697460, 1609700340
                                    ), tzone = "UTC", class = c("POSIXct", "POSIXt")), Date = c("December-20", 
                                                                                                "December-20", "December-20", "December-20", "December-20", 
                                                                                                "January-21", "January-21", "January-21", "January-21", "January-21", 
                                                                                                "January-21"), .order = c(1L, 2L, 3L, 4L, 5L, 86L, 87L, 88L, 
                                                                                                                          89L, 90L, 91L)), row.names = c(NA, -11L), class = c("eventlog", 
                                                                                                                                                                              "log", "tbl_df", "tbl", "data.frame"), case_id = "case_id", activity_id = "activity", activity_instance_id = "action", lifecycle_id = "lifecycle", resource_id = "resource", timestamp = "timestamp")
ui <- fluidPage(
selectInput("mon","month",choices = unique(mydata$Date),selected  = unique(mydata$Date),multiple = T),  
verbatimTextOutput("cases"),
plotOutput("linechart")
)

server <- function(input, output, session) {
 mydata2<-reactive({
   my<-subset(mydata,Date%in%input$mon)
 }) 
 output$cases<-renderPrint({
   bupaR::n_cases(mydata2())
 })
# output$linechart<-renderPlot({
#   ggplot(data=mydata2(), aes(x=Date, y=#cases
#                               )) +
#     geom_line()+
#     geom_point()
# })
}
shinyApp(ui = ui, server = server)

As I understand your question the goal is to show the cases per date. To this end you could transform your data using:

dat <- mydata2() |> group_by(date = as.Date(timestamp)) |> bupaR::n_cases()

And full reproducible app code:


library(shiny)
library(bupaR)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  selectInput("mon", "month", choices = unique(mydata$Date), selected = unique(mydata$Date), multiple = T),
  verbatimTextOutput("cases"),
  plotOutput("linechart")
)

server <- function(input, output, session) {
  mydata2 <- reactive({
    my <- subset(mydata, Date %in% input$mon)
  })
  output$cases <- renderPrint({
    bupaR::n_cases(mydata2())
  })
  output$linechart<-renderPlot({
    dat <- mydata2() |> group_by(date = as.Date(timestamp)) |> bupaR::n_cases()
    ggplot(data = dat, aes(x=date, y = n_cases)) +
      geom_line() +
      geom_point()
  })
}
shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:6871

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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