Reusing R code

When you write a lot of R code in a project, or accross project, you will often want to make reusable parts of code.

DSS provides several mechanisms for reusing R code:

  • Packaging your code as functions or modules, and making them available in all projects
  • Packaging your code as a reusable plugin, and making it available for coder and non-coder users alike

Sharing R code globally

You can write custom R code files in the global R library editor. he global R library editor is available from the “Settings” menu.

../_images/global-library-editor.png

The click on the “lib/R” tab

Files that are written here can be used by all R code in all projects.

Each time you want to use the functions defined in these R files, you need to use the dkuSourceLibR function, which uses the source feature of R to import the functions in the global namespace.

For example if you wrote a file “miscfuncs.R” that contains a do_awesome_stuff() function, you can use:

library(dataiku)

dkuSourceLibR("miscfuncs.R")

do_awesome_stuff()

Permissions

In order to use the global R library editor, you need to be granted the global (group-level) permission “Edit lib folders”

Manually edition of the R code library folder

Although not recommended, if you have shell access to the DSS machine, you can modify the library folder directly in DATA_DIR/lib/R

Packaging your R code as a package

The source mechanism is practical but does not enforce namespacing. To get namespacing, you need to build a complete R package, compile it, and install it (possibly in the code environment) using install.packages. This generally requires shell access on the machine running DSS.