Question

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)
Was it helpful?

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.

OTHER TIPS

Don't. It's a bad idea to sacrifice clear, simple code just to save a couple of keystrokes. You have created a function that can only be use interactively, not called from another function.

Well, I suppose one solution is:

GLD <- "GLD"
GDX <- "GDX"
teetor(GLD,GDX)     # No need to quote GLD and GDX

On second thought, never mind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top