What's the most simple approach to name-spacing R files with `file::function`

Viewed 60

Criteria for answer to this question

Given the following function (within its own script)

# something.R
hello <- function(x){
    paste0("hello ", x)
}

What is the most minimal amount of setup which will enable the following

library(something)
x <- something::hello('Sue')
# x now has value: "hello Sue"

Context

In python it's very simple to have a directory containing some code, and utilise it as

# here foo is a directory
from foo import bar
bar( ... ) 

I'm not sure how to do something similar in R though.

I'm aware there's source(file.R), but this puts everything into the global namespace. I'm also aware that there's library(package) which provides package::function. What I'm not sure about is whether there's a simple approach to using this namespacing within R. The packaging tutorials that I've searched for seem to be quite involved (in comparison to Python).

2 Answers

I don't know if there is a real benefit in creating a namespace just for one quick function. It is just not the way it is supposed to be (I think).

But anyway here is a rather minimalistic solution:

First install once: install.packages("namespace")

The function you wanted to call in the namespace:

hello <- function(x){
  paste0("hello ", x)
}

Creating your namespace, assigning the function and exporting

ns <- namespace::makeNamespace("newspace")
assign("hello",hello ,env = ns)
base::namespaceExport(ns, ls(ns))

Now you can call your function with your new namespace

newspace::hello("you")

Here's the quickest workflow I know to produce a package, using RStudio. The default package already contains a hello function, that I overwrote with your code.

Notice there was also a box "create package based on source files", which I didn't use but you might.

enter image description here

A package done this way will contain exported undocumented untested functions.

If you want to learn how to document, export or not, write tests and run checks, include other objects than functions, include compiled code, share on github, share on CRAN.. This book describes the workflow used by thousands of users, and is designed so you can usually read sections independently.


If you don't want to do it from GUI you can useutils::package.skeleton() to build a package folder, and remotes::install_local() to install it :

Reproducible setup

# create a file containing function definition

# where your current function is located
function_path <- tempfile(fileext = ".R")
cat('
hello <- function(x){
  paste0("hello ", x)
}
', file = function_path)

# where you store your package code
package_path <- tempdir()

Solution :

# create package directory at given location
package.skeleton("something", code_file = file_path, path = package_path)
# remove sample doc to make remotes::install_local happy
unlink(file.path(package_path, "something", "man/"), TRUE) 
# install package
remotes::install_local(file.path(package_path, "something"))
Related