简体   繁体   English

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

[英]quantmod::chart_Series and mapply gives error with chart parameters

How do I use MoreArgs properly with chart_Series? 如何在chart_Series中正确使用MoreArgs?

p.txt p.txt

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

sof.r 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

The error states moreArgs matched by multiple arguments (you supply two arguments named MoreArgs in your call to mapply ), which is your problem. 该错误表明moreArgs由多个参数匹配(您在调用mapply提供了两个名为MoreArgs参数),这是您的问题。 All your arguments that apply to each call of chart_Series must be one named list that is supplied to moreArgs . 适用于chart_Series每次调用的所有参数必须是提供给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 . 你的MBY符号是有问题的,因为它在2017之后有恒定数据或没有数据,最终在调用chart_Series时产生错误,所以为了简单起见,让我们放弃这个例子,因为处理它是与mapply无关的mapply

This is how you could use mapply in your example: 这是您在示例中使用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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM