简体   繁体   English

根据 shiny 输入创建具有动态行数的数据集

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

I have the following shiny app up with the data included.我有以下 shiny 应用程序,其中包含数据。 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()) .我想创建一个折线图,在 x 轴上显示所选Date ,在 y 轴上显示由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.所以基本上我需要重新格式化初始数据集,使其具有 2 列MonthCases ,我将能够将它们包含在图表中,但按正确的时间顺序排列。 For example December 20 must be always be displayed before January 21 even if January 21 is selected first.例如, December 20必须始终显示在January 21之前,即使首先选择了January 21也是如此。 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如果必须重新格式化日期列以实现这一点,我没有问题(例如,它可能是2020-122021-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

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

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