How can I exclude quotation marks around parameters in an R function?

Here are the first few lines of the R function that work:

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 characters as functional parameters, I need to enclose them in quotation marks:

teetor("GLD", "GDX")

I want to be able to simply type:

teetor(GLD, GDX)
+3
source share
3 answers

There are several ways to do this, but usually I would not recommend this.

Usually invoking something without quotes means that the object itself is in the search path. One way to do this without its purpose is to use a function with().

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

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

deparse(substitute(...)), , teetor .

+6

. , , . , , .

+12

Well, I suppose one solution:

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

On the second thought, it doesn’t matter.

+4
source

Source: https://habr.com/ru/post/1792378/


All Articles