0
我正在使用R/shiny應用程序使用dateRangeInput的輸出設置ggplot2圖中的x軸限制。使用dateRangeInput子集數據工作正常,但我無法獲得scale_x_date(或scale_x_continuous或scale_x_datetime)來識別dateRangeInput返回的日期。我試過使用格式(),as.Date,as.POSIXct,但沒有任何運氣。無論我嘗試dateRangeInput的輸出轉換或不獲取:使用閃亮的dateRangeInput在ggplot中設置軸限制
Invalid input: date_trans works with objects of class Date only
這裏是我的問題的(希望)重複的例子:
library(shiny)
library(ggplot2)
library(dplyr)
tp_date_pressed <- as.POSIXct(c("2016-01-01", "2016-03-01"))
sigma <- c(1, 1.5)
data <- data.frame(tp_date_pressed, sigma)
ui <- fluidPage(
dateRangeInput('date',
label = 'Date Range',
start = Sys.Date() - 180,
end = Sys.Date(),
max = Sys.Date()
),
plotOutput("plot")
)
server <- function(input, output) {
subData <- reactive({
data %>%
filter(
as.Date(tp_date_pressed) >= as.Date(input$date[1]),
as.Date(tp_date_pressed) <= as.Date(input$date[2])
)
})
output$plot <- renderPlot({
ggplot(subData(), aes(tp_date_pressed, sigma)) +
geom_point() +
scale_x_date(limits = input$date)
})
}
shinyApp(ui = ui, server = server)
謝謝John!我一直在反對在scale_x_date中使用輸入$ date來敲打我的頭,因爲當tp_date_pressed以POSIXct形式輸入時,ggplot奇蹟般地做了正確的事情。我從來沒有想到,軸數據和軸限制需要是相同的對象類型! – blongworth