5

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)
Milktrader
  • 9,278
  • 12
  • 51
  • 69

3 Answers3

14

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.

hadley
  • 102,019
  • 32
  • 183
  • 245
  • Fully agreed -- "Premature optimization", too much cleverness and all that. Also see current r-devel thread on 'good code examples to learn from'. This wouldn't be one. – Dirk Eddelbuettel Feb 16 '11 at 02:12
  • I agree completely. @hadley says is perfectly. Having a clear, simple code goes a long way when it comes to maintaining the code over a extended period of time. – Sam Feb 16 '11 at 02:39
  • If you use a good developing environment, like Eclipse, and possibly ESS, you will get double the amount of quotes for the price of one (auto completion). – Roman Luštrik Feb 16 '11 at 15:00
  • I believe a better solution will be to create a function that loops through a stock list and outputs the results, this way I can avoid typing it in as a parameter, with or without quotes – Milktrader Feb 16 '11 at 22:10
8

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.

Shane
  • 98,550
  • 35
  • 224
  • 217
  • I've embedded the blah() function in front of the getSymbols() code and then passed it to the getSymbols function like this: getSymbols(blah(x), auto.assign=FALSE) and it works. This saves considerable typing. Thanks! I didn't post the rest of the code only because I believe it's not relevant to the question, but it's on my github at RMilk/task_reference/specific_analysis/teetor_deparsed.r if anyone is interested. – Milktrader Feb 16 '11 at 01:24
  • btw, though this answers the question, my code is still broken for some other unknown reasons. So be forewarned should you venture to my github account. – Milktrader Feb 16 '11 at 01:59
  • I'm sorry, but I thought this was working but instead it was grabbing objects from who knows where. This does not work as I implemented in the the notes. – Milktrader Feb 16 '11 at 02:36
  • @Milktrader (1) We're all saying that this is a bad idea. (2) The `blah` function was intended as an example, not to be actually used. You need to understand `deparse(substitute(...))` in order to make this work. – Shane Feb 16 '11 at 02:43
  • This does answer the question, so it deserves the checkmark. I should have phrased the question "Is it practical to code the elimination of quote marks" but that falls on me. Thanks Shane. – Milktrader Feb 16 '11 at 21:46
4

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.

pteetor
  • 511
  • 2
  • 12