【问题标题】:Error time_trans works with objects of class posixct only in R错误 time_trans 仅在 R 中适用于类 posixct 的对象
【发布时间】:2026-01-05 23:15:01
【问题描述】:

当我以闪亮的方式运行程序时,我得到了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

【问题讨论】:

  • 你应该考虑给我们一个可重现的例子。只要您不提供“sensor_online”,您的代码就不起作用。根据您的变量名称,我想知道您是否使用 as.Date() 函数 正确定义了 record_time
  • 你看到这个帖子了吗(*.com/questions/34058001/…)?

标签: r user-interface ggplot2 shiny posixct


【解决方案1】:

根据this SO answer,您只需将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() +
      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)

这是输出。


考虑使用dygraphs 制作交互式时间序列图。你可以阅读它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)

【讨论】:

    最近更新 更多