quantmod :: chart_Series和mapply给出图表参数错误

时间:2019-07-14 00:57:38

标签: quantmod

如何在chart_Series中正确使用 MoreArgs

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()

错误

> 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

1 个答案:

答案 0 :(得分:0)

错误状态moreArgs与多个参数匹配(您在调用MoreArgs时提供了两个名为mapply的参数),这就是您的问题。您应用于chart_Series的每次调用的所有参数都必须是{strong>一个 命名的列表,该列表必须提供给moreArgs

您的MBY符号是有问题的,因为它在2017年之后有常量数据或没有数据,最终在对chart_Series的调用中生成错误,因此为了简单起见,我们在此删除该示例,因为对其进行了处理是与mapply无关的特殊情况。

这是在示例中使用mapply的方式:

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"))