错误时间_trans仅在R中与类posixct的对象一起工作
我得到了错误时间_trans仅在R中与类posixct的对象一起工作,r,user-interface,ggplot2,shiny,posixct,R,User Interface,Ggplot2,Shiny,Posixct,我得到了错误:无效输入:当我在shiny中运行程序时,time_trans仅与类POSIXct的对象一起工作。这是我在中的代码: library(ggplot2) library(Cairo) # For nicer ggplot2 output when deployed on Linux library(shiny) ui <- fluidPage( fluidRow( column(width = 4, class = "well", h4("
错误:无效输入:当我在shiny中运行程序时,time_trans仅与类POSIXct的对象一起工作。这是我在<代码>中的代码:
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)
库(ggplot2)
库(Cairo)#用于在Linux上部署时获得更好的ggplot2输出
图书馆(闪亮)
ui根据SO的回答,您只需将范围$x
转换为日期
或POSIXct
。请参阅下面的代码。我已经生成了一些数据,以使代码可复制
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)
你应该考虑给我们一个可重复的例子。由于您没有提供“在线传感器”,您的代码无法工作。根据变量的名称,我想知道您是否使用as.Date()函数正确定义了record_time您是否看到了这篇文章()?
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)