错误time_trans仅在R中与posixct类的对象一起使用

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

当我运行程序时,我得到了Error: Invalid input: time_trans works with objects of class POSIXct only。这是我在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)

我应该在shiny服务器中更正什么以便没有Error

r user-interface ggplot2 shiny posixct
1个回答
0
投票

根据this SO答案,您所要做的就是将range$x转换为DatePOSIXct。请参见下面的代码。我生成了一些数据来使代码可重现。

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() +
      # scale_x_date(limits = ranges$x) +
      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)

这是输出。

enter image description here


考虑使用dygraphs制作交互式时间序列图。

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)

enter image description here

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