Here below is a piece of, hopefully reproducible, piece of code (with comments) that worked very well for me (links in comments are where code has been found to adapt to situation):
# 1. Load some data from the Internet and close connections
library(quantmod)
tickers <- c("SHOP", "MPNGF", "BABA", "JD")
getSymbols.yahoo(tickers, auto.assign = TRUE, env = globalenv())
closeAllConnections()
# 2. Find all loaded xts files
xtsObjects <- names(which(unlist(eapply(.GlobalEnv, is.xts))))
# 3. Iteratively convert found xts files under 2. into data.frames
# https://stackoverflow.com/a/69246047/2950721
library(zoo)
for (i in seq_along(xtsObjects)) {
assign(xtsObjects[i], fortify.zoo(get(xtsObjects[i])))
}
# 4. Iteratively save converted data.frame objects as .Rds files
# https://stackoverflow.com/a/8345810/2950721
# https://stackoverflow.com/a/69246047/2950721
library(fs)
rdsFilesFolder <- path("rdsFiles")
saveRDSobjects <- paste0("./", rdsFilesFolder, "/", xtsObjects, ".Rds")
for (i in seq_along(xtsObjects)) {
saveRDS(get(xtsObjects[i]), file = saveRDSobjects[i])
}
System used:
- R version: 4.1.1 (2021-08-10)
- RStudio version: 1.4.1717
- OS: macOS Catalina version 10.15.7