首页 > 解决方案 > R Shiny Plot Output Error "Error:: object 'xx' not found"

问题描述

Really frustrating issue here! I have a plot that will work in my local environment but when it is wrapped in renderPlot() throws an error.

The dataframe has long lines of wrangling code, so, to simplify this question I have truncated the data sourcing to one line data = allData for the purpose of this example.

I am getting the error message Error: object 'runtime' not found, which is one of the features in the dataframe I am using. When I add in x = data$runtime and data$filtSpeed I get a blank plot... When I run the plot line of code on its own, I get the plot I want.

This is the code

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)

Any idea's in how to solve this issue or where I am going wrong?

UPDATE

I am working with raw gps data which I have calculated the distance and speed based on the latitude, longitude and sample frequency into the data frame. I have multiple sessions as .gpx files on the PC which I have looped over to read them in, create the speed and distance, store in a list and rbind them all together as a dataframe.

The code is essentially doing the following:

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)

And producing:

lon lat ele time Date hz runtime dist filtSpeed
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

... ... ...

lon lat ele time Date hz runtime dist filtSpeed
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

标签: rshiny

解决方案


如果我用汽车数据替换您的数据,您的代码就可以正常工作。

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)

输出


推荐阅读