Error time_trans works with objects of class posixct only in R

青春壹個敷衍的年華 提交于 2020-05-31 10:12:20

问题


I got the Error: Invalid input: time_trans works with objects of class POSIXct only when I run the program in shiny. And this is my code in shiny:

library(ggplot2)
library(Cairo)   # For nicer ggplot2 output when deployed on Linux
library(shiny)

ui <- fluidPage(
  fluidRow(
    column(width = 4, class = "well",
           h4("Brush and double-click to zoom"),
           plotOutput("plot1", height = 300,
                      dblclick = "plot1_dblclick",
                      brush = brushOpts(
                        id = "plot1_brush",
                        resetOnNew = TRUE
                      ))),
             column(width = 6,
                    plotOutput("plot3", height = 300)
             )))

server <- function(input, output) {
  # -------------------------------------------------------------------
  # Single zoomable plot (on left)
  ranges <- reactiveValues(x = NULL, y = NULL)

  output$plot1 <- renderPlot({
    ggplot(sensor_online, aes(x= record_time, y= temperature)) +
      geom_point() +
      coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)
  })

  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observeEvent(input$plot1_dblclick, {
    brush <- input$plot1_brush
    if (!is.null(brush)) {
      ranges$x <- c(brush$xmin, brush$xmax)
      ranges$y <- c(brush$ymin, brush$ymax)

    } else {
      ranges$x <- NULL
      ranges$y <- NULL
    }})}

  # -------------------------------------------------------------------
shinyApp(ui, server)

What should I correct in my shiny server so that don't have the Error?


回答1:


According to this SO answer, all you need is to convert range$x to Date or POSIXct. See the code below. I've generated some data to make code reproducible.

library(ggplot2)
library(Cairo)   # For nicer ggplot2 output when deployed on Linux
library(shiny)
library(dplyr)

ui <- fluidPage(
  fluidRow(
    column(width = 4, class = "well",
           h4("Brush and double-click to zoom"),
           plotOutput("plot1", height = 300,
                      dblclick = "plot1_dblclick",
                      brush = brushOpts(
                        id = "plot1_brush",
                        resetOnNew = TRUE
                      ))),
    column(width = 6,
           plotOutput("plot3", height = 300)
    )))

server <- function(input, output) {
  # -------------------------------------------------------------------
  # Single zoomable plot (on left)
  ranges <- reactiveValues(x = NULL, y = NULL)

  # Generate some data
  #######################################
  sensor_online <- tibble(record_time = seq.POSIXt(as.POSIXct("2017-06-20 10:00"),
                                                   as.POSIXct("2017-08-20 10:00"),
                                                   by = "1 day"),
                          temperature = sin(rnorm(62, 35, sd = 1)) / 3)

  ########################################

  output$plot1 <- renderPlot({

    # I've added this chunck
    ########################################
    if (!is.null(ranges$x)) {
      # ranges$x <- as.Date(ranges$x, origin = "1970-01-01")
      ranges$x <- as.POSIXct(ranges$x, origin = "1970-01-01")
    }
    #########################################

    ggplot(sensor_online, aes(x = record_time, y = temperature)) +
      geom_point() +
      coord_cartesian(xlim = ranges$x,
                      ylim = ranges$y,
                      expand = FALSE)
  })

  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observeEvent(input$plot1_dblclick, {
    brush <- input$plot1_brush
    if (!is.null(brush)) {

      ranges$x <- c(brush$xmin, brush$xmax)

      ranges$y <- c(brush$ymin, brush$ymax)

    } else {
      ranges$x <- NULL

      ranges$y <- NULL
    }})}

# -------------------------------------------------------------------
shinyApp(ui, server)

And here is the output.


Consider using dygraphs for making interactive timeseries graphs. You can read about it here

library(shiny)
library(dygraphs)
library(xts)
library(dplyr)


ui <- shinyUI(fluidPage(

    mainPanel(
      dygraphOutput("dygraph")
    )
))

server <- shinyServer(function(input, output) {

  sensor_online <- tibble(record_time = seq.POSIXt(as.POSIXct("2017-06-20 10:00"),
                                                   as.POSIXct("2017-08-20 10:00"),
                                                   by = "1 day"),
                          temperature = sin(rnorm(62, 35, sd = 1)) / 3)

  sensor_online <- xts(x = sensor_online$temperature, order.by = sensor_online$record_time)

  output$dygraph <- renderDygraph({
    dygraph(sensor_online)
  })

})


shinyApp(ui, server)



来源:https://stackoverflow.com/questions/61093134/error-time-trans-works-with-objects-of-class-posixct-only-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!