#' Automatic Generation of R Functions
#'
#' Automatically generate an R funtion using source code contained in an R
#' script.
#'
#' @param fun.name Name of the function.
#'
#' @param read.file A connection, or a character string naming the file that
#' contains the R source code that will constitute the body of the
#' function.
#'
#' @param write.file A connection, or a character string naming the file to
#' write the function to.
#'
#' @details
#' The idea behind \code{autofun} is to use a standalone R script to create an
#' R function. This is done parsing the R script and using the returned
#' expression as the function body.
#'
#' A function \code{foo} returned by \code{autofun} gets all the arguments from
#' the working environment. This is done by evaluating the function with
#' \code{eval(foo())}. Such a function can return several objects.
#'
#' @author
#' Alessandro Samuel-Rosa \email{alessandrosamuelrosa@@gmail.com}
#'
#' @seealso \code{\link{base}{parse}}, \code{\link{utils}{capture.output}},
#' \code{\link{base}{eval}}, \code{\link{base}{expression}}
#'
#' @export
autofun <-
function (fun.name, read.file, write.file) {
# Prepare info
info <- .autofun_info(read.file)
# Function head, body and tail
head <- paste(fun.name, "<-function(...){", sep = "")
body <- capture.output(parse(file = read.file))
tail <- paste("}")
# Prepare output and write to file
text <- c(info, head, body, tail, sep = "")
writeLines(text = text, con = write.file)
}
# INTERNAL FUNCTION - prepare comments added to the generated R file ###########
.autofun_info <-
function (read.file) {
pv <- packageVersion("autofun")
info <- paste("# Generated by autofun (", pv, "): do not edit by hand!!!\n",
"# Please edit source code in ", read.file, sep = "")
return (info)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.