简体   繁体   English

更改 R Shiny 日期范围输入中的日期格式

[英]Change date format in R Shiny daterangeinput

I'm building an app where users can filter data by date.我正在构建一个应用程序,用户可以在其中按日期过滤数据。 I then later want to display reactive text that says "You're viewing data from start_date to end_date" in the format month, day, year.然后,我想以月、日、年的格式显示“您正在查看从 start_date 到 end_date 的数据”的反应性文本。 Unfortunately, shiny's daterangeInput defaults to year, month, day.不幸的是,shiny 的 daterangeInput 默认为年、月、日。

I've tried using我试过使用

    start_date <- reactive({
        format(as.Date(input$date_filter[1]), "%d/%m/%Y")})

But I receive this error and the app crashes:但我收到此错误并且应用程序崩溃:

Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'character'
  [No stack trace available]

Any ideas?有任何想法吗? Here's the dateRangeInput.这是日期范围输入。 I've also tried changing the format of the start date, but that messes up the date.我也尝试过更改开始日期的格式,但这会弄乱日期。 Thank you!谢谢!

library(shiny)


ui <- fluidPage(
  
  dateRangeInput(
        inputId = "date_filter",
        label = "Filter by Date",
        start = "2020-01-01",
        end = NULL,
        min = "2020-01-01",
        max = NULL,
        format = "m-d-yyyy",
        startview = "month",
        weekstart = 0,
        language = "en",
        separator = " to ",
        width = NULL,
        autoclose = TRUE
      ))

shinyApp(ui, server = function(input, output) {
  
})
~~~~

You can use:您可以使用:

library(shiny)

ui <- fluidPage(
  
  dateRangeInput(
    inputId = "date_filter",
    label = "Filter by Date",
    start = "2020-01-01",
    end = NULL,
    min = "2020-01-01",
    max = NULL,
    format = "m-d-yyyy",
    startview = "month",
    weekstart = 0,
    language = "en",
    separator = " to ",
    width = NULL,
    autoclose = TRUE
  ), 
  
  textOutput('text')
  )

shinyApp(ui, server = function(input, output) {
  output$text <- renderText({
    x <- format(input$date_filter, "%d/%m/%Y")
    sprintf("You're viewing data from %s to %s", x[1], x[2])
  })
})

在此处输入图像描述

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

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