Rmarkdown: Error! Error in `not_tsibble()`

20 views Asked by At

The R code below obtained some Apple closing prices and computed their rolling mean and standard deviation. It aimed to plot the three variables in one plot using the ggplot2 package.

# Get APPLE closing prices
getSymbols("AAPL", src ="yahoo", from=Sys.Date()- 1827, to=Sys.Date())
APPLE <- AAPL$AAPL.Close

# Calculate rolling mean
rolling_mean <- zoo::rollmean(APPLE, k = 13, fill = NA, align = "right")

# Calculate rolling standard deviation
rolling_std <- zoo::rollapply(APPLE, width = 20, FUN = sd, align = 'right', fill = NA)

# Combine data into a data frame for ggplot
DataFrame <- data.frame(
  date=index(APPLE), 
  APPLE=coredata(APPLE), 
  RolMean =rolling_mean, 
  RollStd = rolling_std
)

library(tidyverse)
# Drop NA values from rolling_mean
DataFrame <- DataFrame |> drop_na()

library(ggplot2)
# Plot the data using ggplot2
ggplot2::ggplot(DataFrame, aes(x = date)) +
  ggplot2::geom_line(aes(y = AAPL.Close), color = "blue") +
  ggplot2::geom_line(aes(y = AAPL.Close.1), color = "red") +
  ggplot2::geom_line(aes(y = AAPL.Close.2), color = "black") +
  theme_minimal()

Problem

It runs into an error on Rmarkdown but not on R script

Error! Error in not_tsibble()

What I want

I want it to run successfully on Rmarkdown and R Script.

Here is what I want as

output

0

There are 0 answers