RStudio add-ins are one of the newest and most promising developments introduced recently by the RStudio team. They add infinite possibilities for improvement to users' workflows through the enhancement of their IDE.
There are two main types of add-ins:
(){
} structure to be added after the function
token).In this example, we will develop the second type of add-in from the function definition, for deployment and installation as a package on GitHub.
Our example will be a funny one: we will develop an add-in that lets you see weather forecasts for a specified city within the R console.
Let's first install the shiny
and miniUI
packages:
install.packages(c("shiny", "miniUI"), type = "source") library(shiny) library(miniUI)
weatheraddin <-function (){}
ui
add-in:ui <- miniPage( gadgetTitleBar("weather forecasts"), miniContentPanel( textInput("city","input your city name", value = "milan") ) )
server
add-in:server <- function(input,output,session){ observeEvent(input$done, { weather_command <- paste0("finger ",input$city,"@graph.no") system(weather_command) stopApp() }) }
runGadget(ui, server, viewer = dialogViewer("weather forecasts add in"))
weatheraddin()
RStudio offers a custom type of rproj
for packages. Therefore, in order to create a new R package, you will just need to create a new project and select the R Package option:
After choosing this option, the following window will show up, asking you whether any source code is available on which to base the package:
By clicking on the Add… button, you will be able to select the R file containing the add-in function. For the purpose of the recipe, we provided a separate R file containing our weather add-in function named addin_source.R
.
The R package which will result from this procedure is a package ready for distribution.
We'll create a repository on the GitHub website to make it available for distribution and remote downloading by a potential user.
You can learn how to host an RStudio project within a GitHub repository by reading the Using GitHub with RStudio recipe in Chapter 5, Power Programming with R.
install_github()
function from the devtools
package, which you should have already installed from the getting ready section (if not, you can always skip back to that section; I will wait for you here).Once you are done, you can just run the following command:
install_github("andreacirilloac/weather_addin_package") load(weather_addin_package)
Substitute andreacirilloac
with your GitHub username.
addins.dcf
file.To register your add-in in the RStudio IDE, you will have an addins.dcf
file located within your project directory, in a path similar to the following one: project_directory/inst/rstudio/addins.dcf
.
The addins.dcf
file is the file where installed add-ins are recorded and used by RStudio in order to define which functions have to be treated as add-ins rather than as simple functions. Feed your addins.dcf
file.
Installing your add-in will result in the add-in menu showing your add-in, as in the following screenshot:
In order to obtain this, you will have to locate the addins.dcf
file and insert the following lines into this file:
Name: weather addin Description: show weather forecasts within R console Binding: weatheraddin Interactive: false /inst/rstudio/addins.dcf
This piece of text will be separated from the previous one by a blank line.
Having an RStudio add-in that lets you find out weather forecasts can be funny, but I don't really think you are going to consider it useful, unless you are a meteorologist, of course. In all the remaining cases, you should consider this recipe as an explanatory example, using it as a base to develop your custom add-in.