[R] get() within function

Ivan Calandra ivan.calandra at univ-reims.fr
Thu Aug 18 12:19:20 CEST 2016


Dear useRs,

For an interactive use, I am trying to write a function that looks for 
all data.frames and lists in the environment and ask the user to select 
one of them. I then run some operations on this object.

This is what I am trying:

foo <- function(){
     df.list <- ls()[sapply(ls(), function(x) class(get(x))) %in% 
c("data.frame","list")]
     dat.str <- select.list(choices=df.list, title="Select object")
     dat <- get(dat.str)
     str(dat)
}

Let's say I have these three objects:
vec <- 1:10
mylist <- list(1:3)
datf <- data.frame(var1=rnorm(10), var2=rnorm(10))

I would like the function to show me only mylist and datf in the dialog 
box. Everything works fine if I take the code out of the function, but 
when I run foo() I get this error:
"Error in get(dat.str) : first argument incorrect"

Is there a problem with using get() within a function? Maybe a problem 
with environments?

Thank you in advance for any help!
Ivan

-- 
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/



More information about the R-help mailing list