Time_trans работает с объектами класса posixct только в R - PullRequest
0 голосов
/ 07 апреля 2020

Я получил Error: Invalid input: time_trans works with objects of class POSIXct only при запуске программы в shiny. И это мой код в 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)

А sensor_online в ggplot отсюда:

#### 
# This script compute down time due to humidity and temperature out of spec
# It's denoted as out.spec.dt
# Data file is html file downloaded from Presidio
# Data gap could be found in the data file due to sensor break or Presidio server down.
# In this case the down time is denoted as sensor.dt
# assign interval for first row of record as 0. 2nd row's interval is
# delta between date/time of 2nd and first row.
#### 

require(XML)
require(ggplot2)
x = readHTMLTable(choose.files()) #read data from a file
str(x)
y<- x[[2]] #list element2 is the table that contain temperature and humidity data
require(data.table)
head(y)
e<- data.table(y)
str(e)
head(e)
setnames(e,c("record_time","temperature","temp.status","humidity","hum.status","comment")) # change column names
e<- transform(e, record_time= as.character(record_time), comment= as.character(comment),
              temperature= as.numeric(as.character(temperature)), 
              humidity= as.numeric(as.character(humidity))) 
#cannot convert factor to numeric by just "as.numeric(humidity)"
require(lubridate)
t<-dmy_hms(e$record_time)
sep.time<- IDateTime(t) #split "record_time" column into "idate" and "itime" columns
v<- cbind(e, sep.time) #add date and time columns to data table
v$weekdays<- wday(v$idate, label= TRUE)
v$hours<- hour(v$itime)
v$minutes<- minute(v$itime)
setkey(v, idate, hours, minutes) #sort the table in date->hours->minutes orders
#v<- v[(weekdays!="Sat")&(weekdays!="Sun")] #remove Sat and Sun data
v$clock.time<- dmy_hms(v$record_time)
# assign interval for first row of record as 0. 2nd row's interval is
# delta between date/time of 2nd and first row.
v$interval<- c(0, as.double(difftime(v$clock.time[-1], v$clock.time[-nrow(v)],
                                     units= "mins"))) #interval in second
v$day.age<- as.double(difftime(v$idate, v$idate[1],units= "days")) #for reference only
v$record.num<- seq(1,nrow(v), by= 1) #for reference only
#v[, record.num.original:= record.num]
hist(v$interval)

# rn= row number
# decide what extent of interruption is considered sensor down,
# in this script, it is "median(v$interval)*3"
sensor.down.rn<- which(v$interval> median(v$interval)*3) 
sensor.dt<- sum(v$interval[sensor.down.rn]) #dt= down time, in minutes
out.spec.rn<- which((v$temperature<20.5)|(v$humidity>72)|(v$temperature>27.5)|(v$humidity<33))

#refer to "2_down_time_Aug_flowchart.ppt" for the loop below
#the loop mainly leave out sensor.dt from out.spec.dt
out.spec.dt<- 0
for (i in 1:(length(out.spec.rn))) {
  if (i!= length(out.spec.rn)) {
    if ((out.spec.rn[i+1]-out.spec.rn[i])== 1) {
      out.spec.dt<- out.spec.dt+ v$interval[out.spec.rn[i]+1]
      v[(out.spec.rn[i]+1), Out.Spec.Dt:= out.spec.dt] #add downtime to Out.Spec.Dt column, "out.spec.rn[i]+1" row
    } else {
      if (v$interval[out.spec.rn[i]+1]< median(v$interval)*3) {
        out.spec.dt<- out.spec.dt+ v$interval[out.spec.rn[i]+1]
        v[(out.spec.rn[i]+1), Out.Spec.Dt:= out.spec.dt]
      }
    }    
  } else {
    if (v$interval[out.spec.rn[i]+1]< median(v$interval)*3) {
      out.spec.dt<- out.spec.dt+ v$interval[out.spec.rn[i]+1]
      v[(out.spec.rn[i]+1), Out.Spec.Dt:= out.spec.dt]
    }    
  }
}

sensor.dt
out.spec.dt
sensor_online<- e[which(!is.na(e$humidity)), ]
head(sensor_online)
str(sensor_online)
sensor_online$record_time<- dmy_hms(sensor_online$record_time)
ggplot(sensor_online, aes(record_time, temperature)) + geom_point()

Что я должен добавить в свой shiny server чтобы не было Error?

...