Изменение графиков по разным диапазонам дат из импортированных данных в Shiny - PullRequest
0 голосов
/ 26 марта 2020

Я пытаюсь создать блестящее приложение, которое сначала импортирует данные на первой вкладке и отображает 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)
}

1 Ответ

1 голос
/ 27 марта 2020

Вы пробовали read.csv(inFile$datapath, colClasses=c("date"="Date"))? Кстати, в вашем коде есть лишние скобки dfplot <- subset(newdata(), date >= as.Date(input$date[1])) &. Надеюсь, это поможет.

...