Wrapping code for sharing and reuse can be done using the following steps:
- Load an existing source code file:
source(file.path(getwd(), "datasets", "ch11", "my_source_file.R"))
my_sourced_function()
- Create a package skeleton:
usethis::create_package("newpackage")
- Write code:
my_package_function <- function(x){
return( c("I come from a package!") )
}
- Load the package code into memory:
devtools::load_all()
- Install the package into your current R installation:
devtools::install()
library(newpackage)