In R notebooks or rmarkdown using structural times series adding lines to basic plots

plot(spy_pr$adjusted, type = "l", lwd = 2)
lines(fitted(fit), lty = "dashed", lwd = 2, col = "green")
lines(tsSmooth(fit), lty ="dotted", lwd = 2, col = "blue")

Runs as expected output here
image
However, when the exact same code is run from Notebooks or rmarkdown it gives a succession of errors at every attempt at change or adjustment to try and debug from errors list.
The data above for the example is generated below

spy_pr  <- tq_get("SPY", get = "stock.prices", from = " 2020-01-01")
fit <- StructTS(spy_pr$adjusted, type = "level")

NB. The issue is not the data format I have tried tibbles, dataframes, ts, xts etc. It works best with Matt Dancho's Tidyquant data.

Can you post the actual error messages you get? Or even better, can you provide a minimal REPRoducible EXample (reprex)?

Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
4.
plot.xy(xy.coords(x, y), type = type, ...)
3.
lines.default(time(as.ts(x)), x, ...)
2.
lines.ts(fitted(fitNile), lty = "dashed", lwd = 2)

Errors above

The errors are generic when Y or X variable is omitted or wrong data type but it is a uni-variate Time series and runs fine with all the exact same parameters not in Rmarkdown or a notebook as soon as Rmarkdown is involved no go. This has been a consistent problem with many time series observations I have just decided to seek help lately. As I do not want work arounds any more.

the data above is a minimal reprex??? everything needed to reproduce the example is in the question?

Almost, you haven't explicitly specified the used libraries (i.e. tidyquant) and you haven't put all elements together in a self-contained reprex. This would be closer to a proper reprex for your problem I think.

---
title: "reprex"
output: html_document
---

```{r setup, include=FALSE}
library(tidyquant)
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
```

```{r}
spy_pr  <- tq_get("SPY", get = "stock.prices", from = " 2020-01-01")
fit <- StructTS(spy_pr$adjusted, type = "level")

plot(spy_pr$adjusted, type = "l", lwd = 2)
lines(fitted(fit), lty = "dashed", lwd = 2, col = "green")
lines(tsSmooth(fit), lty ="dotted", lwd = 2, col = "blue")
```

The above example runs as expected when you knit it or execute the complete code chunks but indeed fails when you execute individual lines of code by sending them to the console with Ctrl + Enter . I think this has to do with the use of different graphics devices for the inline output and for the "Plots" panel.

As a workaround you could consider using ggplot2 instead of base R plots

Thank you kindly for your information I will try and knit together, and see the results yes I do ctr enter.
Sometimes I do chunks hardly ever knit complete. will take on board reprex advice thank you.
I have wondered on a side issue I assumed that anything plot would run so would ggplot2, is this in fact correct, taken as given the need for different syntax and semantic structure.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.