根据闪亮的导入数据按不同日期范围更改图

问题描述 投票:0回答:1

我正在尝试制作一个闪亮的应用程序,该应用程序首先在第一个选项卡中导入数据,然后根据第二个选项卡中的导入数据绘制ggplot。我收到以下错误:

do not know how to convert 'input$date[1]' to class “Date”
library(shiny)
library(ggplot2)

df <- data.frame("date" = c("2020-01-01", "2020-01-01", "2020-01-02", "2020-01-02"), "id" = c("A", "A", "B", "B"), "number" = c(1, 3, 2, 5))

write.csv(df, "df.csv", row.names = F)


if (interactive()) {
  ui <- fluidPage(
    tabsetPanel(
      tabPanel("Import Data",
               fileInput("file", "Upload Your File:"),
               DT::dataTableOutput("data")
               ),

      tabPanel("Plot",
               uiOutput("daterange"),
               plotOutput("graph")
               )
)
)


  server <- function(input, output) {
    newdata <- reactive({
      req(input$file)

      inFile <- input$file
      read.csv(inFile$datapath)
    })

    output$daterange <- renderUI({
      dates <- as.Date(newdata()$date)
      dateRangeInput("daterange", "Choose Your Date",
                     start = min(dates), end = max(dates),
                     min = min(dates), max = max(dates))
    })

    output$graph <- renderPlot({
      dfplot <- subset(newdata(), date >= as.Date(input$date[1])) & date <= as.Date(input$date[2]))

      g <- ggplot(dfplot, aes(x = id, y = number)) +
        geom_boxplot() +
        theme_classic()
      print(g)
    })

    output$data <- DT::renderDataTable({
      req(input$file)
      inFile <- input$file
      if (is.null(inFile))
        return(NULL)
      read.csv(inFile$datapath)
    })
  }
  shinyApp(ui = ui, server = server)
}
shiny date-range
1个回答
1
投票

您是否尝试过read.csv(inFile$datapath, colClasses=c("date"="Date"))?顺便说一句,在您的代码中,有一个额外的括号dfplot <- subset(newdata(), date >= as.Date(input$date[1])) &。希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.