Load or refresh R file functions as a temporary package
Source:R/jamsession_functions.R
refresh_functions.RdLoad or refresh R file functions as a temporary package
Usage
refresh_functions(
session,
pkg_name = NULL,
functions_path = jamsession_paths()$functions,
fn_pattern = "_functions.R$",
define_defaults = FALSE,
pkg_suffix = "",
use_tempdir = base::tempdir(),
verbose = TRUE,
...
)Arguments
- session
charactervector of one or more function files to load. Note than when multiple functions are loaded, they are combined into the same temporary package.- pkg_name
NULLorcharacterstring indicating the name to use for the temporary package. By default it uses the firstsession, and if there are multiplesessionfiles then it appends the number of files as a suffix.- functions_path
character vector of one or more file paths to search for saved R functions. When
NULL, it uses the output fromjamsession_paths()$functions. Whensessionmatches multiple files, then the most recently modified file is used, as defined byjamba::newestFile().- fn_pattern
characterpattern appended tosessionto match compatible R files. The default format is"session_functions.R"and so defaultfn_pattern="_functions.R".- define_defaults
logicalindicating whether to defineoptions("usethis.description")using jamsession default values. IfgetOption("usethis.description")is not defined, then default values are used regardless. Otherwise, the values fromgetOption("usethis.description")are only modified whendefault_defaults=TRUE. As the R package is only temporary, these defaults have little actual impact. Just be sure to edit the DESCRIPTION file if you intend to use the R package long term.- pkg_suffix
NULLorcharacterstring used as a suffix, appended to the end of the temporary package name. Thepkg_suffixorpkg_nameare useful to avoid name conflicts with proper R packages. For example"igraph_functions.R"should be not be loaded as"igraph"or else it will conflict with the actual"igraph"R package.- use_tempdir
characterpath, or function which returns a valid file path suitable to create a subdirectory for this temporary R package. By defaultbase::tempdir()returns a single temporary directory for each R session, note that this directory may be deleted when the R session is ended.- verbose
logicalindicating whether to print verbose output.- ...
additional arguments are ignored.
Details
This function loads an R file that contains custom R functions, as if that R file were part of a temporary R package.
It assumes the R file or files contain only valid R functions,
which are optionally documented using the roxygen2 style
described in the roxygen2 package.
This mechanism of loading R functions is an improvement over
using base::source() or base::sys.source() in these ways:
Help documents for each function will be prepared, if they were defined using
roxygen2format for each function.R functions are loaded into a namespace, separate from the
.GlobalEnvworkspace. When saving the R session itself, the custom functions will not be included, because they are not part of the workspace.When the custom R functions are updated, repeat the call to this function and it will replace the namespace with the newer namespace.
The package is included in
sessionInfo()which helps indicate that custom functions were also included.
Aside: It really is fast and easy to create a new R package,
however it makes sense that small one-off projects do not
justify creating a whole new R package for only one or
two custom R functions. However, the temporary R package
created by this function can be used to create an actual
R package, by copying the temporary folder, or by supplying
a specific folder with the argument use_tempdir.
To find the temporary R package folder:
system.file(package=pkg_name)
Ideally you should also use version control like Git, subversion, CVS, or a similar system. But for one-off projects, this mechanism is a good start – and we think it will become a gateway to creating proper R packages as needed.
Note that this function refresh_functions() does not validate
the .R file, nor does it check whether the R file is
valid to be part of an R package. However, because refresh_functions()
also calls roxygen2::roxygenize(), it inherits a lot of
validation from that process and will display error messages
as relevant. At this point, those error messages are intended
to be a good thing because they make syntax errors visible.
Note that this function does not itself remove the temporary R package
directory, it assumes that when use_tempdir is defined by
base::tempdir(), see that function help for more information.
Note that on some linux systems, temporary files not accessed for
more than 7 days may be deleted automatically.
Note that multiple files can be used as input to session, in which
case the pkg_name argument can be used to define a specific package
name for the full collection of R functions in the files.
See also
Other jamsession functions:
grep_functions(),
list_functions_exports()
Examples
# create two temporary functions
tempfn <- function(x){x}
another_tempfn <- function(x){length(x)}
# save these functions to a temporary file
# tempfns_functions.R
tempfn_file <- file.path(tempdir(), "tempfns_functions.R");
dump(c("tempfn", "another_tempfn"), file=tempfn_file);
# remove the functions from the environment
rm(tempfn);
rm(another_tempfn);
# load the functions
refresh_functions("tempfns", functions_path=tempdir())
#> ✔ Creating C:/Users/JAMES~1.WAR/AppData/Local/Temp/Rtmp2N4Xv5/tempfns/.
#> ✔ Setting active project to
#> "C:/Users/james.ward/AppData/Local/Temp/Rtmp2N4Xv5/tempfns".
#> ✔ Creating R/.
#> ✔ Writing DESCRIPTION.
#> ✔ Writing NAMESPACE.
#> ✔ Setting active project to "<no active project>".
#> ## (17:56:28) 17Nov2025: refresh_functions(): Loaded temporary package as 'tempfns'
# the tempfn() function is now inside a package
find("tempfn")
#> [1] "package:tempfns"
#> package:tempfns
# the function can be called
tempfn(c("one", "two"))
#> [1] "one" "two"
#> [1] "one" "two"