Reputation: 8404
Lets say I have a vector of year and week like: yw<-c("2020-01","2020-02","2020-03")
. How can I pass it in dateRangeInput()
as start
and end
date
library(shiny)
library(plotly)
yw<-c("2020-09", "2020-10", "2020-11")
name<-c("AD","DF","FD")
value<-c(4,5,6)
df<-data.frame(yw,name,value)
v1 <- range(as.Date(paste0(df$yw, 1), format = '%Y-%W%w'), na.rm = TRUE)
shinyApp(
ui = fluidPage(
dateRangeInput("dr", "Date range:",
start = v1[1],
end = v1[2]),
plotlyOutput("pl")
)
,
server = function(input, output, session) {
output$pl<-renderPlotly({
sub<-subset(df, yw==input$dr)
plot_ly(sub,
x = ~ yw,
y = ~value,
) %>%
add_trace(
type = 'scatter',
mode = 'lines+markers',
hoveron = 'points')
})
}
)
Upvotes: 1
Views: 120
Reputation: 887148
As the dates are in year-week format, concat with a dummy week day at the end using paste
, convert to Date
class and get the range
of dates which can be used as start
and end
in dateRangeInput
v1 <- range(as.Date(paste0(yw, 1), format = '%Y-%W%w'), na.rm = TRUE)
v1
#[1] "2020-01-27" "2021-04-05"
-shiny
..
fluidPage(
dateRangeInput("daterange1", "Date range:",
start = v1[1],
end = v1[2]),...
..
Upvotes: 1