R语言 闪亮的应用程序,根据选择的时间选项而变化

j0pj023g  于 2023-10-13  发布在  其他
关注(0)|答案(2)|浏览(106)

我想创建一个闪亮的图表应用程序,根据所选的时间窗口而变化。在我粘贴的代码中,我使用了一个滚动条(滑块),允许您滚动并选择所选的值范围,它可以工作。
但是,我希望每周有一种selectInput,具有以下值选项:**a)前4周B)前8周a c)前12周d)前16周e)整个周期(176周)**我希望图表根据所选选项进行更改。我就是做不到。我附上我的代码,我用滑块,而不是答案选择。

ui <- fluidPage( column(4,
                          helpText("Time window of interest"),
                                        
                                sliderInput("range1", label = "Selected weeks:",
                                            min = min(df$week), max = max(df$week), value = c(min(df$week), max(df$week)))
                                 ),
                                 
                mainPanel(
                           plotOutput("sp")
                                 )))
                

server <- function(input, output){
  
  output$sp <- renderPlot({
  df2<-df[df$week>=input$range1[1] & df$week<=input$range1[2],]
  ggplot(df2,aes(x = week, y = rate))+
    labs(x = "Week", y = "Incidence Rate", title = "Incidence event rate",
         subtitle = "Stratified by event") + xlim(c(input$range1[1], input$range1[2]))+ geom_smooth(se = FALSE) +style
   })
  
}

shinyApp(ui, server)
z4bn682m

z4bn682m1#

在UI代码中,您需要按照以下方式添加选择输入

selectInput(inputId="week_selection", "Weeks of interest", choices = c("4", "8", "12", "16", "176"), selected = "176")

在服务器端,您需要添加一个事件来记录用户选择,然后过滤所选时间段的数据

observeEvent(input$week_selection, { 
output$sp <- renderPlot({
  df2<-df[df$week<=input$week_selection]
  ggplot(df2,aes(x = week, y = rate))+
    labs(x = "Week", y = "Incidence Rate", title = "Incidence event rate",
         subtitle = "Stratified by event") + xlim(c(input$range1[1], input$range1[2]))+ geom_smooth(se = FALSE) +style
   })
  
}

试试这个代码。

yhxst69z

yhxst69z2#

下面的代码可能会给你给予一些关于如何实现这一点的灵感:

library(shiny)
library(ggplot2)

df<-data.frame(week=1:20,rate=jitter(1:20))  # some fake data

ui <- fluidPage( column(4,
                        helpText("Time window of interest"),
                         # this is probably the interesting part                       
                        selectInput("range2",label = "Selected weeks",
                                    choices = list('4 weeks'=4,
                                                   '8 weeks'=8,
                                                   '12 weeks'=12))
),

mainPanel(
  plotOutput("sp")
))

server <- function(input, output){
  
  output$sp <- renderPlot({
    df2<-df[df$week<=as.numeric(input$range2),] # choosing data points to plot
    print(df2)  # will print the dataframe into console so that you can check what will be plotted
    ggplot(df2,aes(x = week, y = rate))+
      labs(x = "Week", y = "Incidence Rate", title = "Incidence event rate",
           subtitle = "Stratified by event") + xlim(c(input$range1[1], input$range1[2]))+ 
      geom_smooth(method="lm",se = FALSE)  # I changed the smoothing method for the example to work as I don't know your real data frame
  })
  
}

shinyApp(ui, server)

相关问题