错误的time_trans仅在R中适用于posixct类的对象 [英] Error time_trans works with objects of class posixct only in R

查看:111
本文介绍了错误的time_trans仅在R中适用于posixct类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了错误:无效的输入:当我以闪亮的方式运行程序时,time_trans仅适用于POSIXct类的对象.这是我在 shiny 中的代码:

 库(ggplot2)library(Cairo)#在Linux上部署时获得更好的ggplot2输出图书馆(闪亮)ui<-fluidPage(fluidRow(列(宽度= 4,类别=井",h4(画笔并双击以缩放"),plotOutput("plot1",height = 300,dblclick ="plot1_dblclick",brush = brushOpts(id ="plot1_brush",resetOnNew = TRUE))),列(宽度= 6,plotOutput("plot3",height = 300))))服务器<-功能(输入,输出){#-------------------------------------------------------------------#单个缩放图(左侧)范围<-reactValues(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)})#双击时,检查情节上是否有刷子.#如果是,则缩放到画笔边界;如果没有,请重设变焦.watchEvent(input $ plot1_dblclick,{刷<-输入$ plot1_brush如果(!is.null(brush)){范围$ x<-c(brush $ xmin,brush $ xmax)范围$ y<-c(brush $ ymin,brush $ ymax)} 别的 {范围$ x<-NULL范围$ y<-NULL}})}#-------------------------------------------------------------------ShinyApp(用户界面,服务器) 

我应该在我的 shiny 服务器中更正什么,以便没有 Error 错误?

解决方案

根据


考虑使用 dygraphs 制作交互式时间序列图.您可以在

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?

解决方案

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)

这篇关于错误的time_trans仅在R中适用于posixct类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆