Contents
Introduction
Prerequisites and recommendations
Step 1 - Creating a package
Step 2 - Writing the first functions
Step 3 - Setting up an addin
Step 4 - Updating our DESCRIPTION and NAMESPACE
What is next - Always paying our (technical) debts
Wrapping up
TL;DR - Just give me the package
References
Introduction
This is the first post in the RStudio:addins series. The aim of the series is to walk the readers through creating an R package that will contain functionality for integrating useful addins into the RStudio IDE. At the end of this first article, your RStudio will be 1 useful addin richer.
The addin we will create in this article will let us run a script open in RStudio in R vanilla mode via a keyboard shortcut and open a file with the script’s output in RStudio.
This is useful for testing whether your script is reproducible by users that do not have the same start-up options as you (e.g. preloaded environment, site file, etc.), making it a good tool to test your scripts before sharing them.
If you want to get straight to the code, you can find it at https://gitlab.com/jozefhajnala/jhaddins.git
Prerequisites and recommendations
To make the most use of the series, you will need the following:
R, ideally version 3.4.3 or more recent, 64bit
RStudio IDE, ideally version 1.1.383 or more recent
Also recommended
git, for version control
TortoiseGit, convenient shell interface to git for those using Windows, with pretty icons and all
Recommended R packages (install with install.packages(&quot;packagename&quot;), or via RStudio’s Packages tab):
devtools - makes your development life easier
testthat - provides a framework for unit testing integrated into RStudio
roxygen2 - makes code documentation easy
Step 1 - Creating a package
Use devtools::create to create a package (note that we will update more DESCRIPTION fields later and you can also choose any path you like and it will be reflected in the name of the package)
devtools::create(
path = &quot;jhaddins&quot;
, description = list(&quot;License&quot; = &quot;GPL-3&quot;)
)
In RStudio or elsewhere navigate to the jhaddins folder and open the project jhaddins.Rproj (or the name of your project if you chose a different path)
Run the first check and install the package
devtools::check() # Ctrl+Shift+E or Check button on RStudio's build tab
devtools::install() # Ctrl+Shift+B or Install button on RStudio's build tab
Optionally, initialize git for version control
devtools::use_git()
Step 2 - Writing the first functions
We will now write some functions into a file called makeCmd.R that will let us run the desired functionality:
makeCmd to create a command executable via system or shell, with defaults set up for executing an R file specified by path
makeCmd