Quantmod :: chart_Series и mapply выдает ошибку с параметрами диаграммы

Как правильно использовать MoreArgs с 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()

Ошибка

> 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

person AG1    schedule 14.07.2019    source источник
comment
проверьте эту ветку. Это может помочь передать-выражения-в-moreargs-of- mapply   -  person bugsb    schedule 26.07.2019


Ответы (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"))
person FXQuantTrader    schedule 11.08.2019