Sj Bq
Sj Bq

Reputation: 211

How to use function with variable in R?

in R,the pdf function can save graph in c:/test:

pdf("c:/test")

I want to make a variable substitue pdf ,how can i make it run ?

str<-"pdf"
str("c:/test")

Upvotes: 0

Views: 103

Answers (3)

baptiste
baptiste

Reputation: 77096

s = "pdf" ; do.call(s, list("c:/test"))

or in two steps,

cl <- call(s, "c:/test")
eval(cl)

Upvotes: 2

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

You can extract the function specified by the name in str with match.fun:

match.fun(str)("c:/test")

By the way: It is not a good idea to name an object str since this is the name of a basic function in R.

Upvotes: 1

Matthew Lundberg
Matthew Lundberg

Reputation: 42649

get() does this:

get(str)("c:/test")

Upvotes: 2

Related Questions