Suppose I want to delete everything in my working directory that is not a function. It seems that sapply(ls(),is.function) always returns FALSE, because ls() returns objects of mode character. How do I evaluate is.function(), not on a character string, but on the object that character string represents? Thanks, Tim
testing the contents of an environment
4 messages · Tim Bergsma, jim holtman, Rolf Turner +1 more
Need to use 'get':
ls(9)
[1] "c.Factor" "cls.console" "date2POSIX" "Default.par" "delete.NULLs" [6] "f.apdex" "f.area" "f.axis" "f.HHMMSS2hr" "f.hr2HHMMSS" [11] "f.integrate" "f.queue" "f.TimeAxis" "file.size" "function.print" [16] "generate.stats" "HrsInDay" "level.plot" "list.subset" "ls.obj" [21] "my.axis.POSIXct" "my.func" "my.ls" "my.stats" "plot.sar" [26] "plot.srx.file" "plotReset" "poly.graph" "poly.graph.legend" "poly.graph1" [31] "print.wide" "r.time.cvt" "read.sar" "ReadVMstat" "sar.levelplot" [36] "setParms" "Setwd" "time2POSIXct" "unix2EXCEL" "unix2POSIXct" [41] "x.createColors"
sapply(ls(9), function(x) is.function(get(x)))
c.Factor cls.console date2POSIX
Default.par delete.NULLs
TRUE TRUE TRUE
FALSE TRUE
f.apdex f.area f.axis
f.HHMMSS2hr f.hr2HHMMSS
TRUE TRUE TRUE
TRUE TRUE
f.integrate f.queue f.TimeAxis
file.size function.print
TRUE TRUE TRUE
TRUE TRUE
generate.stats HrsInDay level.plot
list.subset ls.obj
TRUE TRUE TRUE
TRUE TRUE
my.axis.POSIXct my.func my.ls
my.stats plot.sar
TRUE TRUE TRUE
TRUE TRUE
plot.srx.file plotReset poly.graph
poly.graph.legend poly.graph1
TRUE TRUE TRUE
TRUE TRUE
print.wide r.time.cvt read.sar
ReadVMstat sar.levelplot
TRUE TRUE TRUE
TRUE TRUE
setParms Setwd time2POSIXct
unix2EXCEL unix2POSIXct
TRUE TRUE TRUE
TRUE TRUE
x.createColors
TRUE
On 9/27/07, Tim Bergsma <timb at metrumrg.com> wrote:
Suppose I want to delete everything in my working directory that is not a function. It seems that sapply(ls(),is.function) always returns FALSE, because ls() returns objects of mode character. How do I evaluate is.function(), not on a character string, but on the object that character string represents? Thanks, Tim
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
On 28/09/2007, at 6:52 AM, Tim Bergsma wrote:
Suppose I want to delete everything in my working directory that is not a function. It seems that sapply(ls(),is.function) always returns FALSE, because ls() returns objects of mode character. How do I evaluate is.function(), not on a character string, but on the object that character string represents?
sapply(ls(),function(x){is.function(get(x))})
One must distinguish between an object and its name.
(See ``Alice Through the Looking-Glass'' by Lewis Carroll aka Rev.
Charles L. Dodgson. :-) )
cheers,
Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confidenti...{{dropped}}
On 9/27/07, Rolf Turner <r.turner at auckland.ac.nz> wrote:
On 28/09/2007, at 6:52 AM, Tim Bergsma wrote:
Suppose I want to delete everything in my working directory that is not a function. It seems that sapply(ls(),is.function) always returns FALSE, because ls() returns objects of mode character. How do I evaluate is.function(), not on a character string, but on the object that character string represents?
sapply(ls(),function(x){is.function(get(x))})
A conceptually simpler alternative is sapply(ls(), exists, mode = "function", inherits = FALSE) -Deepayan