panel.abline on "lattice plot in Shiny" got error

29 views Asked by At

I don't know very well about lattice but I know here the openair::timePlot function call lattice.xyplot to plot time series of pollutant concentrations . ('timePlot','selectByDate' functions and dataset called 'mydata' are part of Openair package

I want to add ref line to each panel accordingly. Ref line Y s are calculated and store in ref

library(openair)
df <- selectByDate(mydata, year = 2003, month = 3)
ref <- c(10, 2, 30)
timePlot(df, pollutant = c("so2", "co","pm25"),
         y.relation = "free",
         ref.y = list(h = ref, lty=5, col="blue"))

enter image description here

Biult-in ref.y add all 3 refline to each panel which is not what I want.

So I try this which is works locally.

timePlot(df, pollutant = c("so2", "co","pm25"),
         y.relation = "free")
trellis.last.object() +
    layer(panel.abline(h = ref[1], lty = 5,col="red"), rows = 1) +
    layer(panel.abline(h = ref[2], lty = 5, col="blue"), rows = 2)

enter image description here

But when I use it on a simple Shiny app, I got errors.

Here is an app.R working but can reproduce one error.

library(openair)
library(shiny)
library(latticeExtra)

ui <- fluidPage(
    plotOutput("timePlot")
)

server <- function(input, output) {
    output$timePlot <- renderPlot({
        df <- selectByDate(mydata, year = 2003, month = 3)
        timePlot(df, pollutant = c("so2", "co","pm25"),              
                y.relation = "free")
        ref <- c(15, 1, 40)
        trellis.last.object() +
            layer(panel.abline(h = 15, lty = 5), rows = 1) +
            layer(panel.abline(h = ref[2], lty = 5), rows = 2) 
        })
}

shinyApp(ui = ui, server = server)

It seems that the h only takes numberic value directly. When I assign value from other varible,like ref, it says "object not found", or "object type closure is not subsettable"/double" . enter image description here enter image description here enter image description here

Any clues? Thanks!!

ps: tried creating my panel function. but it breaks timePlot output I want.

0

There are 0 answers