简体   繁体   English

R:将字符转换为只有年份和月份的日期,以在 Shiny 应用程序中的箱线图 output 上应用 dateRange 输入

[英]R: transforming character to date with only year and month to apply a dateRange input on a boxplot output in a Shiny app

I have dataframe (in the following called df) with the first column df$date being dates with type character, formatted as %Y-%m .我有 dataframe(以下称为 df),第一列 df$date 是类型字符的日期,格式为%Y-%m

My first question would be: how can I transform the types of all the entries in this column from character to date, keeping the same format with only year and month?我的第一个问题是:如何将这一列中所有条目的类型从字符转换为日期,只保留年份和月份的相同格式? I've tried as.Date("2011-08", format(%Y-%m)), as.yearmon("2011-08") (returns a num, but I want a Date), format(as.Date("2011-08"),"%Y-%m").我试过 as.Date("2011-08", format(%Y-%m)), as.yearmon("2011-08") (返回一个数字,但我想要一个日期),格式(as.日期(“2011-08”),“%Y-%m”)。 All didn't work.一切都没有奏效。

The reason I want to change the type of this column is that I want to implement a dateRange input in a Shiny app, ranging from the minimum to the maximum date in the column mentioned above.我想更改此列的类型的原因是我想在 Shiny 应用程序中实现 dateRange 输入,范围从上述列中的最小值到最大值。 Maybe there is another solution to this without needing to change the type?也许有另一种解决方案而无需更改类型?

This is my input in the Shiny-App:这是我在 Shiny-App 中的输入:

box(width = 4, height = "50px",
                 dateRangeInput(inputId = 'dateRange', 
                                label = "Period of analysis : ",
                                format = "yyyy-mm",language="en",
                                start = min(df$date),
                                end = max(df$date),
                                startview = "year", separator = " - ")
                 )

I want to have min(df$date) resp.我想要min(df$date) resp。 max(df$date) for start and end, but it is not working. max(df$date)用于开始和结束,但它不起作用。 Again, the problem seems to be, that df$date has the type chr, eg "2011-08".同样,问题似乎是,df$date 的类型为 chr,例如“2011-08”。

My Output-Code in the server function of the Shiny-App looks like this:我在 Shiny-App 的服务器 function 中的输出代码如下所示:

output$PartPlot <- renderPlot({
       PartPlot_new <- subset(df, date >= input$dateRange[1] & date <= input$dateRange[2])
       boxplot(PartPlot_new[, input$Table2], xlab = "Part", ylab = "Percentage")
     })

As you can see, the goal is to have boxplots from the other columns of df (containing percentages).如您所见,目标是从 df 的其他列(包含百分比)中获取箱线图。

Appreciate any help.感谢任何帮助。 Thanks in advance.提前致谢。

You can transform date string into a date value, with make use of this handy library:您可以使用这个方便的库将日期字符串转换为日期值:

library(anytime)

times <- c("2004-03-21 12:45:33.123456",
           "2004/03/21 12:45:33.123456",
           "20040321 124533.123456",
           "03/21/2004 12:45:33.123456",
           "03-21-2004 12:45:33.123456",
           "2004-03-21",
           "20040321",
           "03/21/2004",
           "03-21-2004",
           "20010101")

anydate(times)
 [1] "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21"
 [7] "2004-03-21" "2004-03-21" "2004-03-21" "2001-01-01"

In your case, if you want to convert with %Y-%M format, you can also try:在您的情况下,如果您想使用 %Y-%M 格式进行转换,您也可以尝试:

times <- c("2018-11")
anydate(times)
[1] "2018-11-01"

What novica commented is absolutely correct. novica的评论是绝对正确的。 Leave your date as a full proper date with year, month, and day, where day can be 1. Column type will obviously be date.将您的日期保留为包含年、月和日的完整日期,其中日可以是 1。列类型显然是日期。 If you need to show the year and month in a label client-side, take the date value, format as a string and drop off the day.如果您需要在 label 客户端中显示年份和月份,请获取日期值,格式化为字符串并删除日期。 In your shiny client page where you select the inputs, you can show the year and month "label" but the "value" you pass will be a full date (or you can add the day server-side before using dateRangeInput.在 select 输入的 shiny 客户端页面中,您可以显示年份和月份“标签”,但您传递的“值”将是完整日期(或者您可以在使用 dateRangeInput 之前添加服务器端的日期。

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

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