Constructing RStudio add-ins

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:

  • Text macros: These only produce a text insertion within your code (for instance the (){} structure to be added after the function token).
  • Shiny gadgets: These are little Shiny apps that are shown within the viewer pane, a pop-up window, or a browser window. They let you perform advanced activities such as statistical parameter definition or data-wrangling tasks.

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.

Getting ready

Let's first install the shiny and miniUI packages:

install.packages(c("shiny", "miniUI"), type = "source")
library(shiny)
library(miniUI)

How to do it...

  1. Define the function that will be called from your add-in:
    weatheraddin <-function (){}
  2. Define your ui add-in:
      ui <- miniPage(
        gadgetTitleBar("weather forecasts"),
        miniContentPanel(
          textInput("city","input your city name", value = "milan")
        )
      )
  3. Define your server add-in:
      server <- function(input,output,session){
        observeEvent(input$done, {
          weather_command <- paste0("finger ",input$city,"@graph.no")
          system(weather_command)
          stopApp()
        })
      }
  4. Define where your add-in will be displayed:
      runGadget(ui, server, viewer = dialogViewer("weather forecasts add in"))
  5. Try your add-in:
    weatheraddin()
  6. Put your add-in in a package.

    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:

    How to do it...

    After choosing this option, the following window will show up, asking you whether any source code is available on which to base the package:

    How to do it...

    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.

  7. Host your package on GitHub.

    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.

  8. Install your package and try your add-in. In order to install your package, we will leverage the 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.

  9. Create an 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:

    How to do it...

    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.

There's more…

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.

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

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