I have two quite long R files with many function definitions and some comments irregularly between them. Is there any R function to move each function definition to a separate R file? It would be quite nice to name the new file with function name :).
Content of the file (let's call it tuttifrutti.R) with all the functions could be something like this:
fun1 <- function(x) {
# comment
x * x
}
fun2 <- function(x) {
# comment
x/2
}
And then you can collect all functions and print them to samename file.
localenv <- new.env()
source("tuttifrutti.R", local = localenv)
sapply(localenv, print)
sapply(names(localenv), FUN = function(x, en) {
dump(x, file = sprintf("%s.R", x), envir = en)
}, en = localenv)
Related
How do I pass a pre-existing object, such as a DF, to my custom functions, given the setup I have (see below)?
Alternatively, do I need to set up my custom functions differently?
My functions reside in a series of *.R scripts.
I source the functions in my .Rprofile:
.env$fxShortName <- function(){
source("C:\\path\\to\\scriptFile.R")
}
Not-Quite Solutions:
1) Defining the Function Manually || It works w/ the obvious drawback that I need to manually load my functions each time.
2) Rscript + commandArgs || This works if I define the DF within the function, like this:
#foo.R
a <- data.frame(a = c(1))
b <- data.frame(b = c(1))
args <- commandArgs(trailingOnly = TRUE)
print(args)
data.frame.name <- args[1]
print(colnames(get(data.frame.name)))
Rscript creates a new R instance, though, so it doesn't see my pre-existing DF. At least, it doesn't find it out-of-the-box.
3) Function w/ substitute, match.call, etc. || I've adopted %>>% to set up auto-updated views of certain DFs, so I tried modifying the setup that works in that case. For %>>% I have this code in my .Rprofile:
.env$`%>>%` <- function(expr, x) {
x <- substitute(x)
call <- match.call()[-1]
fun <- function() {NULL}
body(fun) <- call$expr
makeActiveBinding(sym = deparse(x), fun = fun, env = parent.frame())
invisible(NULL)
}
This type of setup works with DFs from my current session. However, I prefer the structure offered by keeping my custom scripts separate from my .Rprofile.
4) get() & mget() || This seemed promising, but I don't understand it enough to definitively say whether or not it will help. And, yes, I did RTFM.
Reproducible Example:
myfx(head, preExistingDF)
Sample Code:
myfx <- function(expr, x) {
x <- substitute(x)
call <- match.call()[-1]
fun <- function() {NULL}
body(fun) <- call$expr
print(body(fun))}
Put the sample code in a script. Add the following code to your .Rprofile:
.env$mySamplefx <- function(){
source("C:\\path\\to\\myfx.R")
}
Then try it after adding the code directly to your .Rprofile.
I come from a python background and am trying to get up to speed with R, so please bear with me
I have an R file - util.R with the following lines
util.add <- function(a,b) a + b
util.sub <- function(a,b) {
a - b
}
I need to write a function that returns the following:
findFunctions('path/util.R')
[1] "util.add" "util.sub"
I think sourcing will be necessary, but you don't need to clutter you global environment. I tested this locally and it seems to work:
find_functions = function(file) {
search_env = new.env()
source(file = file, local = search_env)
objects = ls(envir = search_env)
functions = objects[sapply(ls(envir = search_env), FUN = function(x) {
is.function(get(x, envir = search_env))
})]
return(functions)
}
I am working on some text with embedded R code. I am using Sweave to produce PDF documents. I'd like to print the functions as written into my PDF, but print(f) where f is an arbitrary function eliminates some of the key aspects, like the function name. For instance:
f <- function(x, y = 2) {
return(x^y)
}
print(f)
yields this:
> source('~/.active-rstudio-document')
function(x, y = 2) {
return(x^y)
}
Is there some version of print or some similar function that would print something I can cut and paste directly back into R, preserving the function declaration (the f <- part)? Also, and this is kind of an after thought, is there a way to set the maximum width in characters?
There's probably a better way but something like this would work:
f <- function(x, y = 2) {
return(x^y)
}
pretty <- function(fun){
captured <- capture.output(fun)
captured[1] <- paste(as.character(substitute(fun)), "<-", captured[1])
cat(paste(captured, collapse="\n"))
}
pretty(f)
## f <- function(x, y = 2) {
## return(x^y)
## }
I have a question with importing functions.
Say I have a R script named "functions" which looks like this:
mult <- function(x,y){
return(x*y)
}
divide <- function(x,y){
return(x/y)
}
Currently I am importing all functions in the script:
source(file="C:\\functions.R",echo=FALSE)
The problem is that the (actual) R script is getting very large.
Is there a way to import the "mult" function only?
I was looking at evalSource/insertSource but my code was not working:
insertSource("C:\\functions.R", functions="mult")
It looks like your code will work with a slight change: define an empty object for the function you want to load first, then use insertSource.
mult <- function(x) {0}
insertSource("C:\\functions.R", functions="mult")
mult
Which gives:
Object of class "functionWithTrace", from source
function (x, y)
{
return(x * y)
}
## (to see original from package, look at object#original)
The mult object has some additional information that I suppose is related to the original application for insertSource, but you could get rid of them with mult <- mult#.Data, which will set mult to the actual function body only.
Also, you might be interested in the modules project on github, which is trying to implement a lightweight version of R's package system to facilitate code reuse. Seems like that might be relevant, although I think you would have to split your functions into separate files in different subdirectories.
I ended up creating functions to do what you recommended.
This first group allows for multiple functions in one call:
LoadFunction <- function(file,...) {
dots <- match.call(expand.dots = FALSE)$...
dots <- sapply(dots, as.character)
output <- lapply(dots, function(x,file){eval(parse(text=paste(x," <- function(x) {0}",sep="")),envir = .GlobalEnv)
suppressMessages(insertSource(file, functions=x))
eval(parse(text=paste(x," <- ",x,"#.Data",sep="")),envir = .GlobalEnv) },file=file)
}
UnloadFunction <- function(...) {
dots <- match.call(expand.dots = FALSE)$...
dots <- sapply(dots, as.character)
output <- lapply(dots, function(x,file){eval(parse(text=paste("rm(",x,",envir = .GlobalEnv)",sep="")))},file=file)
}
They are called like this:
LoadFunction(file="C:\\functions.R",mult,divide)
UnloadFunction(mult,divide)
The second is only one function per call:
LoadFunction2 <- function(file,function_name) {
eval(parse(text=paste(function_name," <- function(x) {0}",sep="")),envir = .GlobalEnv)
suppressMessages(insertSource(file, functions=function_name))
eval(parse(text=paste(function_name," <- ",function_name,"#.Data",sep="")),envir = .GlobalEnv)
}
UnloadFunction2 <- function(function_name) {
eval(parse(text=paste("rm(",function_name,",envir = .GlobalEnv)",sep="")))
}
They are called like this:
LoadFunction2(file="C:\\functions.R","mult")
LoadFunction2(file="C:\\functions.R","divide")
UnloadFunction2("mult")
UnloadFunction2("divide")
Is there any way to "check" or "verify" a source code file in R when sourcing it ?
For example, I have this function in a file "source.R"
MyFunction <- function(x)
{
print(x+y)
}
When sourcing "source.R", I would like to see some sort of warning : MyFunctions refers to an undefined object Y.
Any hints on how to check / verifiy R code ?
Cheers!
I use a function like this one for scanning all the functions in a file:
critic <- function(file) {
require(codetools)
tmp.env <- new.env()
sys.source(file, envir = tmp.env)
checkUsageEnv(tmp.env, all = TRUE)
}
Assuming source.R contains the definitions of two rather poorly written functions:
MyFunction <- function(x) {
print(x+y)
}
MyFunction2 <- function(x, z) {
a <- 10
x <- x + 1
print(x)
}
Here is the output:
critic("source.R")
# MyFunction: no visible binding for global variable ‘y’
# MyFunction2: local variable ‘a’ assigned but may not be used
# MyFunction2: parameter ‘x’ changed by assignment
# MyFunction2: parameter ‘z’ may not be used
You can use the codetools package in base R for that. And if you had your code in a package, it would tell you about this: