简体   繁体   English

从日期列中提取年份 R Shiny 过滤器输入

[英]Extracting year from date column in R Shiny filter input

I am building a Shiny app and have to extract year from date column to create a slider with years.我正在构建一个 Shiny 应用程序,必须从日期列中提取年份以创建一个带有年份的 slider。 I have a column called date in format YYYY MM DD , trying to extract only the year in the filter part, but it doesn't work as expected.我有一个名为日期的列,格式为 YYYY MM DD ,试图仅提取过滤器部分中的年份,但它没有按预期工作。 I am stuck at the following code:我被困在以下代码中:

 server <- function(input, output){ 
  data <- reactive({                
    ufo %>%                  
      filter(date >= format(as.Date(input$years[1],"%Y")),
             date <= format(as.Date(input$years[2], "%Y"))) 
  })

The error I get is:我得到的错误是:

Problem with filter() input..1. filter() 输入问题..1。

[34mℹ[39m Input..1 is date >= format(as.Date(input$years[1], "%Y")). [34mℹ[39m Input..1 is date >= format(as.Date(input$years[1], "%Y")).

[31mx[39m character string is not in a standard unambiguous format [31mx[39m 字符串不是标准的明确格式

How can I extract only the year for my filter?我怎样才能只为我的过滤器提取年份?

Welcome to stackoverflow, it is always important to ask with a minimal reproducible example of your data, that way we can help you faster.欢迎使用 stackoverflow,询问您的数据的最小可重现示例始终很重要,这样我们可以更快地帮助您。

To do so, you can simulate one or share a fragment of yours.为此,您可以模拟一个或分享您的片段。

Here one with what I think you have这是我认为你拥有的

library(shiny)
library(dplyr)
library(lubridate)

# Creating a minimal reproducible example of the data
set.seed(123)
ufo <- data.frame(
  date = sample(seq(as.Date('2010-01-01'), as.Date('2022-01-01'), by = 'day'), 20),
  ufo = sample(1:15, 20, replace = TRUE)
) %>%
  arrange(date)

#>          date ufo
#> 1  2011-06-10  11
#> 2  2012-08-10   9
#> 3  2012-10-13  10
#> 4  2013-02-15  15
#> 5  2013-12-20   5
#> 6  2014-06-15   7
#> 7  2014-11-25   7
#> 8  2015-01-16  12
#> 9  2015-07-06   7
#> 10 2016-02-05   1
#> 11 2016-09-28   4
#> 12 2016-11-15  14
#> 13 2017-01-10  12
#> 14 2017-07-19   9
#> 15 2017-11-27  11
#> 16 2018-03-05   5
#> 17 2019-03-25  10
#> 18 2019-06-08  13
#> 19 2021-09-30   7
#> 20 2021-10-16   5

Now that we have the data, let's move to the shiny part.现在我们有了数据,让我们转到 shiny 部分。 It is good as well to include the minimal elements of your app, in this case I put a slider input the server using it.包含应用程序的最少元素也很好,在这种情况下,我将 slider 输入服务器使用它。

The problem with your original code is that format() returns a character and you are comparing that with a date, which do not make sence.您的原始代码的问题是format()返回一个字符,并且您将其与日期进行比较,这是没有意义的。 It is a better approach to extract the year of your date as numeric using lubridate::year() and compare that with the slider input values.使用lubridate::year()将日期的年份提取为数字并将其与 slider 输入值进行比较是一种更好的方法。

# User interface
ui <- fluidPage(
  sliderInput('year', 'Year', min = 2010, max = 2022, 
              step = 1, value = c(2015, 2020), sep = ''),

  dataTableOutput('table')
)

server <- function(input, output, session) {
    data <- reactive({
      ufo %>%
        filter(year(date) >= input$year[1], year(date) <= input$year[2])
    })
    
    output$table <- renderDataTable({
      data()
    })
}

Created on 2022-02-14 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-02-14

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

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