1
votes

I have a shiny app in which I can load a file. I wan't to extract the min and max date range from my file use these as input for a slider input.

Here is what I have so far

ui <- fluidPage(
dateRangeInput(inputId = "date", label = "Input a date range", start=textOutput("datemin") , end = textOutput("datemax"),"%d-%m-%y"))
)
server = function(input, output){
output$datemin <- renderText({as.character(as.Date(paste("01",as.character.Date(min(dataset()$date)),sep="-"),"%d-%y-%m"))})
     output$datemax <- renderText({as.character(as.Date(paste("01",as.character.Date(max(dataset()$date)),sep="-"),"%d-%y-%m"))})
}

I know the outputs datemin and datemax are a bit tricky, this is because the "date" column in the dataset is a string such as "13-05" for may 2013 (the dataset is a summarized version with monthly means for some values)

Edit.

I've tried 3 different outputs for my date in order to check the format, and I display them as textoutput as follows :

UI

    textOutput("datemin1"),
    textOutput("datemin2"),
    textOutput("datemin3"),

Server

 output$datemin1 <- renderText({(as.character.Date(min(dataset()$date)))})
 output$datemin2 <- renderText({as.character(as.Date(paste("01",as.character.Date(min(dataset()$datem)),sep="-"),"%d-%y-%m"))})
 output$datemin3 <- reactive({as.Date(as.yearmon(min(dataset()$datem)))})

The outputs are the following :

datemin1 : 14-01
datemin2 : 2014-01-01
datemin3 : 0014-01-01

But still when I try to use an output as an input value for my slider as follows :

    sliderInput("slider1", "Date Range",
                min = textOutput("datemin2"), max=textOutput("datemax2"),
                value = textOutput("datemax2")
    ),

I get this error : ERROR: non-numeric argument to binary operator

Thanks

1
How do you want to show the date format on sliderInput? What is the complete date format in your dataset?Sagar
Originally the complete date format in the dataset is yyyy-mm-dd, but it is then dropped to yy-mm when the dataset is summarized to compute the monthly means of the data. On the sliderInput, the format can be mm-yyyy or dd-mm-yyyy with day initialized to 01 automatically.Boidot
Okay, please check the answer below.Sagar

1 Answers

0
votes

library(zoo) includes yearmon which is a class for representing monthly data. Please see code below, it converts 13-05 to 13-05-01.

library(shiny)
library(zoo)

dataset <- data.frame(minDate = c('13-05'), maxDate = c('14-05'))

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      sliderInput("slider1", "Date Range",
                  min = as.Date(as.yearmon(dataset$minDate)),
                  max = as.Date(as.yearmon(dataset$maxDate)),
                  value = as.Date(as.yearmon(dataset$minDate))
                  )
      ),

    server = function(input, output){

      }
  )
}