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

AG1*_*AG1 7 r candlestick-chart quantmod mapply

如何在chart_Series中正确使用MoreArgs?

p.txt

s,n
ABBV,AbbVie
BMY,Bristol
LLY,EliLily
MRK,Merck
PFE,Pfizer
Run Code Online (Sandbox Code Playgroud)

苏菲

# 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()
Run Code Online (Sandbox Code Playgroud)

错误

> 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
Run Code Online (Sandbox Code Playgroud)

FXQ*_*der 1

错误状态moreArgs与多个参数匹配(您提供了MoreArgs在调用中指定的两个参数mapply),这是您的问题。适用于每次调用的所有参数都chart_Series必须是提供给 的一个 命名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"))
Run Code Online (Sandbox Code Playgroud)