Saturday, May 6, 2017

MIT Step by Step Instructions for Creating Your Own R Package.

We will mention the steps to create your own R package using RStudio and we will provide the link to download the complete MIT guide:"Instructions for Creating Your Own R Package", In Song kim, Phil Martin, Nina McMurry. February 23, 2016 .

1. Start by opening a new .R file. Make sure your default directory is clear by typing rm(list= ls()). Check to see that it is empty using ls() (you should see character(0)).

2. Write the code for your functions in this .R file. You can create one file with all of your functions or create separate files for each function. Save these files somewhere where you can easily find them.

3. Install the ‘devtools’ package (install.packages(‘devtools’)).

4. Open a new project in RStudio. Go to the ‘File’ menu and click on ‘New Project.’ Then select ‘New Directory,’ and ‘R Package’ to create a new R package.

5. Type the name of your package, then upload the .R file you created in step 1 under ‘Create package based on source files’. Click ‘Create project.’

6. On the lower right hand side of your screen, you should see a file directory. The ‘R’ folder contains the code for your functions. The ‘man’ folder will contain the help files for each function in your package. Depending on your version of RStudio, the help files may have been generated automatically as .Rd or “R documentation” files when you created your package.
If the ‘man’ folder already contains .Rd files, open each file, add a title under the ‘title’ heading, and save (if not, see step 7). You can go back and edit the content later, but you will need to add a title to each .Rd file in order to compile your package.

7. If your ‘man’ file is empty, you will have to manually create a .Rd file for each function. To do this, go to File > New File > R Documentation, enter the title of the function and select ‘Function’ under the ‘Rd template’ menu. Edit your new file to include something in the ‘title’ field (again, you may make other edits now or go back and make edits later, but your package will not compile if the ‘title’ field is empty). Save each .Rd file in the ‘man’ folder.
NOTE: You will need to complete this step if you add more functions to your package at a later point, even if RStudio automatically generated R documentation files when you initially created the package.

8. Now you are ready to compile your package. Go to ‘Build’ on the top toolbar and select ‘Build and Reload’ (note you can also use the keyboard shortcut Ctrl+Shift+B). If this works, your package will automatically load and you will see library(mynewpackage) at the bottom of your console. Test your functions to make sure they work.

9. Go back and edit the documentation (the help file) for each function. Open each .Rd file, add a brief description of the package, define its arguments and, if applicable, values, and include at least one example. Then, re-compile your package and test out your documentation in the R console (?myfun). NOTE: You will need to re-compile (repeating step 8) each time you make changes to your functions or documentation.

10. Once you have finished creating your functions and documentation, compiled your package, and double checked that the functions and help files work, copy the entire folder containing your package to the Dropbox folder with your name on it.

Download the complete guide at:

No comments:

Post a Comment

It will be nice to hear your comments.