簡體   English   中英

R Shiny ggplot 對 dateRangeInput 反應

[英]R Shiny ggplot reactive to dateRangeInput

我對 R 比較陌生,我正在嘗試在 Shiny 中構建一個反應性 ggplot,其中 X 軸(日期)對 UI 中的 dateRangeInput 是反應性的。 我一直在谷歌搜索,但我嘗試的每件事都會返回一個錯誤。

在 ggplot 中,來自名為 datecorrected_totals 的數據集的 aes() 調用,其中 x 是日期,y=load 是我希望對 dateRangeInput 做出反應的兩個值,因此 ggplot 將根據時間段調整比例在日期范圍輸入中。

library(tidyverse)
library(shiny)
library(tidyr)
library(lubridate)
library(zoo)
data <- read_csv("--")

# Define UI ----
ui <- fluidPage(

  titlePanel("--"),

  sidebarLayout(
    sidebarPanel(
      h3("Calculator"), 
      dateRangeInput("dates", label = "Dates",
                     start = ("10-18-2018"),
                     end = max("05-29-2019"),
                     min = min("10-18-2018"),
                     max = max("05-29-2019"),
                     format = "mm-dd-yyyy"),
      sliderInput("slider_a", label = "--",
                  min = 0, 
                  max = 7, 
                  value = 0),
      sliderInput("slider_c", label = "--",
                  min = 7, 
                  max = 42, 
                  value = 7)
    ),
    mainPanel(plotOutput('bar_chart'))

  )
)

# Define server logic ----
server <- function(input, output, session) {

  RE <- reactive({

  })

  output$bar_chart <- renderPlot(

    ggplot(data = datecorrected_totals, aes(x = x, y = load)) +
           geom_bar(stat = "identity")
  )
}


# Run the app ----
shinyApp(ui = ui, server = server)

您需要按輸入日期過濾原始數據集。 在此示例中, data將是您的原始數據集。

RE <- reactive({
  data %>% 
    filter(x>=input$dates[1] & x<=input$dates[2])
})

output$bar_chart <- renderPlot(

  ggplot(data = RE(), aes(x = x, y = load)) +
    geom_bar(stat = "identity") 

無需創建單獨的reactive()表達式(除非另有要求)。 過濾器可以直接在renderPlot() 因此, output$bar_chart變成

  output$bar_chart <- renderPlot(
    datecorrected_totals %>%
      filter(between(x, input$dates[1], input$dates[2])) %>%
      ggplot(aes(x = x, y = load)) +
      geom_bar(stat = "identity")
  )

下面是一個獨立的最小可重現示例

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

datecorrected_totals <- tibble(x = seq(as.Date("2018-10-18"), as.Date("2019-05-29"), length.out = 10L),
                               load = day(x))

# Define UI ----
ui <- fluidPage(

  titlePanel("--"),

  sidebarLayout(
    sidebarPanel(
      h3("Calculator"), 
      dateRangeInput("dates", label = "Dates",
                     start = mdy("10-18-2018"),
                     end = mdy("05-29-2019"),
                     min = mdy("10-18-2018"),
                     max = mdy("05-29-2019"),
                     format = "mm-dd-yyyy"),
    ),
    mainPanel(plotOutput('bar_chart'))

  )
)

# Define server logic ----
server <- function(input, output, session) {

  output$bar_chart <- renderPlot(
    datecorrected_totals %>%
      filter(between(x, input$dates[1], input$dates[2])) %>%
      ggplot(aes(x = x, y = load)) +
      geom_col()
  )
}

# Run the app ----
shinyApp(ui = ui, server = server)

請注意,已通過調用mdy()將日期字符串強制為有效的Date對象以避免錯誤消息。

此外, geom_bar(stat = "identity")已被geom_col()取代。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM