我正在尝试在ggplot2
应用中将plotly
与shiny
一起使用。以下是我使用的代码:
# Loading the libraries ---------------
library(shiny)
library(tidyverse)
library(plotly)
# Global ----------------
if (!exists('ds', envir = .GlobalEnv, inherits = FALSE)) {
ds <- readRDS("Analysis/Data/df_sim_updated.rds")
}
if (!exists('ado', envir = .GlobalEnv, inherits = FALSE)) {
ado <- readRDS("Analysis/Data/df_ADOs.rds")
}
ds <- as_tibble(ds)
file_ID <- unique(ds$file.ID)
ado_Name <- c("ditiExpeon6", "VanC5", "NeonC4", "llacCadi3", "WhiteC2",
"Ford1", "BMWC10", "StarT7", "owT8Yell", "peC9Esca", "tarCWins13",
"rC11RoveLand", "F150C12", "CargoT4", "SemiT3", "RedT1", "RaceT11",
"BlueT5", "artTWalm9", "ghtTFrei10", "MoveT12", "WhiteT2", "ilT6Carg"
)
ado <- as_tibble(ado)
# Define UI for application --------------------
ui <- fluidPage(
# Application title
titlePanel("exploRing simulation"),
# Sidebar
sidebarLayout(
sidebarPanel(
selectInput("fileid", label = h3("Select scenario"),
choices = file_ID),
selectInput("adoName", label = h3("Select ADO(s)"),
multiple = TRUE, choices = ado_Name)
),
# Main Panel
mainPanel(
uiOutput("FS"),
plotlyOutput("plot1")
)
)
)
# Define server logic ---------------------------------
server <- function(input, output) {
# Filter ds according to fileid
data <- reactive({ds %>% filter(file.ID==input$fileid)})
# Time Frame Slider
output$FS <- renderUI(
sliderInput("fs", label = h3("Time Frame"), min = min(data()$frames),
max = max(data()$frames), value = min(data()$frames), width = "800px")
)
# Position of OV at given time
data2 <- reactive({data() %>% filter(frames==input$fs)})
# Filter ado(s) according to fileid
ado_data1 <- reactive(ado %>%
filter(file.ID==input$fileid,
ADO_name %in% input$adoName))
# Position of ado at given time
ado_data2 <- reactive(ado_data1() %>%
filter(frames==input$fs))
# Plot data
output$plot1 <- renderPlotly(
ggplotly( ggplot() +
geom_line(data=data(),
aes(x = x, y = y)) +
geom_point(data = data2(),
aes(x = x, y = y), size = 2) +
geom_line(data = ado_data1(),
aes(x = pos.2, y = pos.1, color = ADO_name)) +
geom_point(data = ado_data2(),
aes(x = pos.2, y = pos.1, color = ADO_name),
size = 2) +
geom_hline(yintercept = 278, linetype="longdash") +
geom_hline(yintercept = 278-16) +
geom_hline(yintercept = 278+16) +
labs(x = "Longitudinal Position",
y = "Lateral Position") +
theme_bw())
)
}
# Run the application ---------------------------------
shinyApp(ui = ui, server = server)
以下是我收到的错误消息:
> runApp('exploRe')
Listening on http://127.0.0.1:7666
Warning: Error in filter_impl: incorrect length (0), expecting: 26826
Stack trace (innermost first):
109: <Anonymous>
108: stop
107: filter_impl
106: filter_.tbl_df
105: filter_
104: filter
103: function_list[[k]]
102: withVisible
101: freduce
100: _fseq
99: eval
98: eval
97: withVisible
96: %>%
95: <reactive:data2> [C:\Users\my_username\Google Drive\DrivingSimulator\exploRe/app.R#65]
84: data2
83: fortify
82: layer
81: geom_point
80: ggplotly
79: ggplotly
78: func
77: origRenderFunc
76: output$plot1
1: runApp
Warning: Error in order: argument 1 is not a vector
Stack trace (innermost first):
88: order
87: [.data.frame
86: [
85: to_basic.GeomLine
84: to_basic
83: layers2traces
82: gg2list
81: ggplotly.ggplot
80: ggplotly
79: ggplotly
78: func
77: origRenderFunc
76: output$plot1
1: runApp
如果我只使用renderPlot
和plotOutput
,该应用就可以了。我在这里做错了什么?
答案 0 :(得分:2)
我无法重现您的示例,但在我自己的应用中遇到并解决了完全相同的错误。发生此错误的原因是,正在向Plotly传递一个绘图对象,该绘图对象本身在绘图对象中没有数据,但是仅具有与其他几何图形层关联的数据。这是因为您在ggplot()
的初始调用中没有传递任何数据,并且传递给至少一个几何图形的数据的重要列之一可能存在问题(即丢失)。 (geom_path
在绘制之前对每个轴进行排序,因此可能是x
或y
。)
如果您运行:
plot <- ggplot() +
geom_line(data=data(),
aes(x = x, y = y)) +
geom_point(data = data2(),
aes(x = x, y = y), size = 2) +
geom_line(data = ado_data1(),
aes(x = pos.2, y = pos.1, color = ADO_name)) +
geom_point(data = ado_data2(),
aes(x = pos.2, y = pos.1, color = ADO_name),
size = 2) +
geom_hline(yintercept = 278, linetype="longdash") +
geom_hline(yintercept = 278-16) +
geom_hline(yintercept = 278+16) +
labs(x = "Longitudinal Position",
y = "Lateral Position") +
theme_bw())
plot$data
您可能会找到类似以下的输出
list()
attr(,"class")
[1] "waiver"
显然,即使其中一个层存在某些问题,ggplot也适用于此空对象。它将显示警告,只是错过那些因缺少尺寸而无法绘制的点/层。
与(通常)Plotly一样,通常不会出现故障层,只要底层绘图对象中有一些数据,其他层仍然可以正常显示。问题与层之一的数据结合在一起,并且主图对象中没有数据导致Plotly出现问题。
我建议您尝试分别绘制每个图层,但是调用ggplot()
中的数据,以识别特定的图层是否存在问题(我强烈怀疑存在问题)。另外,在传递给plotly之前,将每个对象作为普通图绘制。
即:
plot <- ggplot(data = data()) +
geom_line(aes(x = x, y = y))
print(plot)
ggplotly(plot)
plot <- ggplot(data = data2())+
geom_point(aes(x = x, y = y), size = 2)
print(plot)
ggplotly(plot)
# and so on...
如果您既在测试脚本中又在闪亮的应用程序中进行了检查,那么我确定您会找出问题所在。
我还发现,尝试在绘制不同数据对象之前将它们连接在一起也可能会有所帮助,即使使用bind_rows()
也是如此。 (仅在适当的情况下才适用。)这样,您可以先将一个数据传递给ggplot()
调用,然后分别调整每个几何的美观度。 Plotly仍为该图获取一个基础数据对象,这看起来很顺利,但可能无法尽快揭示出真正的基础问题。