Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

quantmod::chart_Series and mapply gives error with chart parameters

How do I use MoreArgs properly with chart_Series?

p.txt

s,n
ABBV,AbbVie
BMY,Bristol
LLY,EliLily
MRK,Merck
PFE,Pfizer

sof.r

# R --silent --vanilla < sof.r
library(quantmod)
options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)

# setup chart params
cp <- chart_pars()
cp$cex=0.55
cp$mar=c(1,1,0,0) # B,L,T,R
# setup chart theme
ct <- chart_theme() 
ct$format.labels <- ' ' # AG: space needed to remove bottom x-axis labels
ct$lylab <- TRUE        # AG: enable left y-axis labels
ct$rylab <- FALSE       # AG: remove right y-axis labels
ct$grid.ticks.lwd=1

# read values into vectors
csv <- read.csv("p.txt", stringsAsFactors = FALSE) 
symVec <- getSymbols(as.vector(csv$s))
infoVec <- mapply(paste, csv$s, csv$n, sep=": ") # eg. SYM: Name
cpVec = rep(cp, times=nrow(csv))

# create PDF
pdf(file = "p.pdf")
par(mfrow = c( 5, 4 ) )
mapply (chart_Series, mget(symVec), name=infoVec, null, null, null, MoreArgs=cp, MoreArgs=ct)
dev.off()

Error

> mapply (chart_Series, mget(symVec), name=infoVec, null, null, null, MoreArgs=cp, MoreArgs=ct)
Error in mapply(chart_Series, mget(symVec), name = infoVec, null, null,  : 
  formal argument "MoreArgs" matched by multiple actual arguments
Execution halted
like image 521
AG1 Avatar asked Jul 14 '19 00:07

AG1


1 Answers

The error states moreArgs matched by multiple arguments (you supply two arguments named MoreArgs in your call to mapply), which is your problem. All your arguments that apply to each call of chart_Series must be one named list that is supplied to moreArgs.

Your MBY symbol is problematic, as it has constant or no data after 2017, which ends up generating an error in the call to chart_Series, so for simplicity here let's drop that example because handling it is a corner case unrelated to mapply.

This is how you could use mapply in your example:

csv <- data.frame(s = c("ABBV", "MBY", "LLY", "MRK", "PFE"), n = c("AbbVie", "Bristol", "EliLily", "Merck", "Pfizer"))
csv <- csv[-2, ]
symVec <- getSymbols(as.vector(csv$s))
infoVec <- mapply(paste, csv$s, csv$n, sep=": ") # eg. SYM: Name
cpVec = rep(cp, times=nrow(csv))


# Make things a little more interesting in your custom chart theme:
ct$col$up.col<-'darkgreen'
ct$col$dn.col<-'darkred'

par(mfrow = c( 2, 2 ) )
mapply (chart_Series, 
        # vectorised arguments:
        x = mget(symVec), # this is a list of the market data for each symbol, and is consistent with a vectorised argument for `mapply`
        name = infoVec, #`simply character vector of the same length as `mget(symVec)`.
        # one NAMED list to MoreArgs, with the full names of the arguments in chart_Series you want to complete
        MoreArgs = list(pars = cp, theme = ct, subset = "2019"))
like image 122
FXQuantTrader Avatar answered Oct 20 '22 14:10

FXQuantTrader