Search code examples
functionrquantmod

How can I eliminate quote marks around parameters in R function?


Here are the first few lines of an R function that works:

teetor <- function(x,y) {

require("quantmod")
require("tseries")

alpha <- getSymbols(x, auto.assign=FALSE)
bravo <- getSymbols(y, auto.assign=FALSE)

t     <- as.data.frame(merge(alpha, bravo))

# ... <boring unit root econometric code>

}

When I pass two stock symbols as function parameters, I need to enclose them with quotes:

teetor("GLD", "GDX")

I want to be able to simply type:

teetor(GLD, GDX)

Solution

  • There are several ways of doing this, but generally I wouldn't advise it.

    Typically calling something without quotes like that means that the object itself is in the search path. One way to do this without assigning it is to use the with() function.

    You can get the name of something without having it actually exist by deparse(substitute(...)):

    > blah <- function(a) {
        deparse(substitute(a))
      }
    > blah(foo) 
    [1] "foo"
    > foo 
    Error: object 'foo' not found
    

    So in principle you can get the names using deparse(substitute(...)) as in the above example in your teetor function instead of passing in the names.