简体   繁体   中英

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 .

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"). 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. Maybe there is another solution to this without needing to change the type?

This is my input in the 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. max(df$date) for start and end, but it is not working. Again, the problem seems to be, that df$date has the type chr, eg "2011-08".

My Output-Code in the server function of the Shiny-App looks like this:

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).

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:

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

What novica commented is absolutely correct. Leave your date as a full proper date with year, month, and day, where day can be 1. Column type will obviously be date. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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