0

How can i dynamically generate a function-paramater in R? I will use the paste0 function for different parameters for the line-function:

lines(paste0(gdaxisymbol[i],"$",gdaxisymbol[i],".Adjusted"))

It ends with an warning: In xy.coords(x, y) : NAs durch Umwandlung erzeugt

thx

Hi, thx for your answer and sorry for the unclear question Here is my full code:

library(XML)
library(RCurl)

s <- getURL("https://de.finance.yahoo.com/quote/^GDAXI/components")
t=readHTMLTable(s)
gdaxi = t[["NULL"]]
gdaxi = gdaxi[,-(3:6) ]
gdaxisymbol <- gdaxi$Symbol

getSymbols(gdaxisymbol,from="2015-12-31",to="2021-01-31", auto.assign = TRUE)
plot(ADS.DE$ADS.DE.Adjusted)
for(i in 1:30)
{
  
  lines(paste0(gdaxisymbol[i],"$",gdaxisymbol[i],".Adjusted"))
}

I would like to print all Adujsted share prices in one Plot. But if i execute the loop i become the error above. How can i give the correct parameter dynamicaly to the lines function? If i print one line with

lines(ALV.DE$ALV.DE.Adjusted)

it works fine.

thx

topi
  • 1
  • 1
  • 1
    Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Feb 04 '21 at 14:12

1 Answers1

0

This questions isn't perfectly clear. Not sure whether the problem is with the lines function or something else. But I'll answer the question regarding how to dynamically pass parameters for a function.

If you want to dynamically pass parameters for a function and send them into paste0, the easiest way to do it is to pass the parameters in on a list or vector. Here is an example:

# Define function 
paste_special <- function(inputs) {
  
 ret <- paste0(inputs, collapse = "") 
  
 return(ret)
}

# Create sample data
myinputs <- c("A", "$", "B", ".adjusted")

# Call function and view results
paste_special(myinputs)
# [1] "A$B.adjusted"

Since the list or vector is flexible, and can take any number of elements, then your function can also take any number of elements.

David J. Bosak
  • 1,386
  • 12
  • 22