Can anyone show me how to refer to an object name that is passed to a function, from within the function? For example: MyModel <- 1 test <- function(x) { if(x == 1) {cat("x is a valid object.\n")} } test(x) What I would like this to do is pass MyModel to function test, and if it passes a test, be able to print "MyModel is a valid object." Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-an-object-name-from-within-a-function-tp3167147p3167147.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Dec-29 14:32 UTC
[R] Referring to an object name from within a function
On Dec 29, 2010, at 9:18 AM, zerfetzen wrote:> > Can anyone show me how to refer to an object name that is passed to a > function, from within the function?deparse(substitute(x))> > For example: > > MyModel <- 1 > > test <- function(x) { > if(x == 1) {cat("x is a valid object.\n")} > } > > test(x)Well you don't want to test(x) since x has not been defined. You wnat to test(MyModel) MyModel <- 1 test <- function(x) {xname <- deparse(substitute(x)) if(x == 1) {cat(xname, " is a valid object.\n")} } test(MyModel) #MyModel is a valid object.> What I would like this to do is pass MyModel to function test, and > if it > passes a test, be able to print "MyModel is a valid object." > > Thanks.David Winsemius, MD West Hartford, CT
Excellent, thanks, and sorry about the test(x) goof. -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-an-object-name-from-within-a-function-tp3167147p3167174.html Sent from the R help mailing list archive at Nabble.com.