[英]R Shiny Plot Output Error “Error:: object 'xx' not found”
这里真是令人沮丧的问题! 我有一个 plot 可以在我的本地环境中工作,但是当它被包装在renderPlot()
中时会引发错误。
dataframe 有很长的代码行,因此,为了简化这个问题,为了本示例的目的,我将数据源截断为一行data = allData
。
我收到错误消息Error: object 'runtime' not found ,这是我正在使用的 dataframe 中的功能之一。 当我添加x = data$runtime
和data$filtSpeed
时,我得到一个空白 plot...
这是代码
server = function(input, output, session) {
data = reactive(allData)
output$plot = renderPlotly({
data = allData
plot_ly(data, x = ~runtime, y = ~filtSpeed, type = 'scatter', mode = 'line')
})
}
ui = basicPage(
h1("Title here"),
plotOutput('plot')
)
# Run the application
shinyApp(ui = ui, server = server)
关于如何解决这个问题或我哪里出错了有什么想法吗?
更新
我正在使用原始 gps 数据,我根据数据帧中的纬度、经度和采样频率计算了距离和速度。 我在 PC 上有多个会话 as.gpx 文件,我已经循环读取它们,创建速度和距离,存储在列表中并将它们全部合并为 dataframe。
该代码本质上是在执行以下操作:
library(plotKML) #for reading gpx documents
folder <- 'gpx_files/'
files = list.files(path = folder, pattern = "*.gpx")
numfiles = length(files)
datalist = list()
for (file in files){
data = readGPX(paste0(folder, file))
data = as.data.frame(data$tracks[[1]])
##**a bunch more wrangling**##
data = data %>% filter(filtSpeed > 8)
datalist[[file]] = data
}
allData = as.data.frame(do.call(rbind, datalist))
head(allData)
并生产:
隆 | 纬度 | 电子 | 时间 | 日期 | 赫兹 | 运行 | 距离 | 过滤速度 |
---|---|---|---|---|---|---|---|---|
153.37 | -28.01 | 0.4 | 20-12-10 00:18:13 | 2020-12-10 | 1 | 1 | 0 | 0 |
153.37 | -28.01 | 1.2 | 20-12-10 00:18:14 | 2020-12-10 | 1 | 2 | 2.9 | 9.5 |
153.37 | -28.01 | 1.8 | 20-12-10 00:18:15 | 2020-12-10 | 1 | 3 | 8.7 | 14.7 |
153.37 | -28.01 | 1.8 | 20-12-10 00:18:16 | 2020-12-10 | 1 | 4 | 13.9 | 17.2 |
153.37 | -28.01 | 1.8 | 20-12-10 00:18:17 | 2020-12-10 | 1 | 5 | 20.5 | 18.4 |
………… _ _
隆 | 纬度 | 电子 | 时间 | 日期 | 赫兹 | 运行 | 距离 | 过滤速度 |
---|---|---|---|---|---|---|---|---|
153.37 | -28.01 | 0.4 | 20-12-28 00:18:13 | 2020-12-28 | 1 | 1 | 0 | 0 |
153.37 | -28.01 | 1.2 | 20-12-28 00:18:14 | 2020-12-28 | 1 | 2 | 4.8 | 10.2 |
153.37 | -28.01 | 1.8 | 20-12-28 00:18:15 | 2020-12-28 | 1 | 3 | 8.6 | 13.7 |
153.37 | -28.01 | 1.8 | 20-12-28 00:18:16 | 2020-12-28 | 1 | 4 | 16.4 | 16.2 |
153.37 | -28.01 | 1.8 | 20-12-28 00:18:17 | 2020-12-28 | 1 | 5 | 21.5 | 16.4 |
如果我用汽车数据替换您的数据,您的代码就可以正常工作。
library(shiny)
library(plotly)
server = function(input, output, session) {
#data = reactive(allData)
data <- cars %>% mutate(filtSpeed=speed, runtime=dist)
output$plot = renderPlotly({
#data = allData
plot_ly(data, x = ~runtime, y = ~filtSpeed, type = 'scatter', mode = 'line')
})
}
ui = basicPage(
h1("Testing with cars data"),
plotlyOutput('plot')
)
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.