How it works...

The first step of this code shows a very effective but very rudimentary method of loading in your own pre-written external code. We use the source() function to load in a file of R code to the current namespace. The particular file here contains normal R functions and nothing else. The source() function simply reads the code in the external file and executes it as if it was typed directly into the current console. As the file just contains functions, then you have to get those loaded into memory for immediate use. 

Step 2 takes things a step further and creates a bare-bones package with the usethis::create_package() function. The function creates a new folder with the name that you provide (so, in this case, newpackage) and puts all of the essential files and folders you need for a package in there. You can now fill the R/ subfolder in the package with R code that will eventually be loaded when you load the package. Try it with the function in step 3; add this function to a file called my_functions.R in the R/ folder. It doesn't matter too much what the files in the R/ folder are called and you can have many—make sure they end in .R though.

Step 4 will take your source package and load it into memory using the devtools::load_all() function. This roughly emulates what happens when we call the library() function but without actually installing the package. By using devtools::load_all(), we can quickly load code to test it out, without having to first install it, so if we need to change the code, we don't have a broken version installed. We don't provide any arguments, so it loads the package in the current directory (if you provide a path as the first argument, it will load the package it finds there).

In step 5, we actually install the code properly into R. We use the devtools::install() function and it builds the package and copies the built version into the normal place in R. We can now load the built version as any other package with library (newpackage). Note that this means that we have two copies of the package—the one we installed and the one we are working on. You'll need to repeat steps four and five as needed as you develop more code and add it to your package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.12.95