Chapter 9 Sharing

This topic covers a variety of ways to share R code with others. The goal is to make your work accessible and reproducible for others. Sharing can take many forms, including sharing your RStudio project, creating a distilled version of your analysis that others can follow, developing a web-based application for others to use, or finding ways to contain and disseminate reproducible analyses. By sharing your work, you enable others to learn from and build upon your research, making it more impactful and useful for the wider community.


At the end of this chapter you should be able to

  • Understand the options for sharing analyses.

  • Start an R Notebook project to share.


9.1 Notebooks

Notebooks in RStudio IDE are interactive documents that allow developers to create and share code, visualizations, and narrative text in a single document. R Notebooks provide an intuitive interface for data analysis, making it easy to explore data, create models, and communicate results.

Creating

To use notebooks in RStudio IDE, follow these steps:

  1. Open RStudio IDE and create a new R Notebook by navigating to File > New File > R Notebook.
  2. Add code chunks by clicking the “Insert a new code chunk” button in the toolbar or by using the keyboard shortcut “Ctrl + Alt + I”.
  3. Write R code in the code chunks and run them by clicking the “Run” button in the toolbar or by using the keyboard shortcut “Ctrl + Enter”.
  4. Add markdown text to the notebook by typing in markdown cells.

One of the most significant benefits of R Notebooks is that they allow you to mix code and narrative text. You can add markdown cells to your notebook to provide context for your code, explain your thought process and methodology, and document your findings. This feature makes it easy for others to understand your work and reproduce your analysis.

With R Notebooks, it is also possible to:

  • Insert tables and images: You can add tables and images to your notebooks using markdown syntax or by using the “Insert” menu in the toolbar.
  • Use LaTeX to display formulas: You can use LaTeX syntax to display mathematical formulas in your notebooks.
  • Use HTML to display interactive widgets: You can use HTML code to create interactive widgets that allow users to interact with your code and data.

Sharing

Sharing your notebook project in RStudio IDE is a straightforward process. By sharing your projects, you can collaborate with other data scientists and benefit from the insights and expertise of your colleagues. Here are the steps to share your projects:

9.2 Packrat

Packrat is an R package that provides a way to manage R package dependencies for projects. It is a powerful tool for reproducible research, as it allows you to create a local library of packages specific to a project that can be shared with collaborators or moved to another machine. With Packrat, packages used in a project are kept at a specific version, ensuring that the same results can be obtained regardless of the version of the package used.

Initiating

To initiate a Packrat project, you need to run the packrat::init() function in your R console. This will create a packrat directory in your project folder, which will contain all the necessary files and information for Packrat to manage the package dependencies for your project.

library(packrat)
packrat::init()

Installing

To install a package into the project-specific library, you can use the packrat::install.packages() function. Packrat will automatically detect package dependencies and install them as well.

packrat::install.packages("dplyr")

Loading

To load a package from the project library, you simply use the library() function as usual. Packrat will ensure that the correct versions are used.

library(dplyr)

Updating

To update a package in the project library, you can use the packrat::update.packages() function. Packrat will update the package and all its dependencies.

packrat::update.packages("dplyr")

Overall, Packrat is a valuable tool for reproducible research, as it allows you to manage package dependencies for your projects and ensure that the same results can be obtained regardless of the version of the package used.

9.3 GitHub

GitHub is an online platform that provides version control and collaboration features for software development projects. It is widely used by developers to store and manage their code repositories, track changes made to code over time, and collaborate with others on projects. It is a powerful tool that simplifies the process of managing code and makes it easier for developers to work together. A key benefit of RStudio IDE is that it has built-in support for version control systems like GitHub, which makes it easy to manage and share code with others.

Sharing

To use GitHub within RStudio IDE, you need to first create a GitHub account and set up a repository. Once you have created a repository, you can follow these steps to use it within RStudio IDE:

  1. Open RStudio IDE and navigate to the “New Project” tab.
  2. Select “Version Control” and then “Git”.
  3. Enter the URL of your GitHub repository and choose a project directory.
  4. Click “Create Project” to create a new RStudio project that is linked to your GitHub repository.

Sharing

Sharing code with others using GitHub and RStudio IDE is a straightforward process. Once you have set up your GitHub repository and linked it to your RStudio project, you can follow these steps to share code with others:

  1. Make changes to your code in RStudio IDE.
  2. Commit your changes to the local Git repository using the “Commit” button in the “Git” tab.
  3. Push your changes to your GitHub repository using the “Push” button in the “Git” tab.
  4. Share the URL of your GitHub repository with others so they can access your code.

9.4 Docker

Docker is an open-source platform that allows developers to easily create, deploy, and run applications in containers. Containers are lightweight, portable, and self-contained environments that can run isolated applications. Docker helps to simplify the process of software development, testing, and deployment by providing a consistent environment that runs the same way on any machine, independent of the host operating system.

For more information check out the main Docker website in addition to the Rocker R Project.

9.5 R Packages

In R, packages are collections of R functions, data, and compiled code that can be easily shared and reused with others. They are an essential part of the R ecosystem and are used for a variety of purposes, such as data analysis, visualization, and statistical modeling.

Creating a package in R is a straightforward process, and RStudio IDE provides several tools to simplify the package development process. Packages are a way of organizing your code and data into a single, self-contained unit that can be easily shared and distributed with other R users.

Creating

To create a package in RStudio, follow these simple steps:

  1. Create a new R Project. Go to “File” -> “New Project” -> “New Directory” -> “R Package”
  2. Choose a name for the package, such as my_new_rpackage and a directory location where it will be saved.
  3. Once the project is created, RStudio will generate a basic package structure with the following files:
  • DESCRIPTION: This file contains information about the package, such as its name, version, author, and dependencies.
  • NAMESPACE: This file defines the package’s API, i.e., the set of functions and objects that are intended for public use.
  • R/: This directory contains the package’s R source code files.
  • man/: This directory contains the package’s documentation files.
  1. Now it’s time to write some code. You can start by creating a simple function that outputs “Hello ASMS”. Here’s an example:
#' Hello ASMS Function
#'
#' This function prints "Hello ASMS" to the console.
#'
#' @return A character vector with the message "Hello ASMS".
#' @export
say_hello <- function() {
  return("Hello ASMS")
}
  1. Save the function in a new R script file called “hello_world.R” and place it in the package’s R/ directory.
  2. Build the package by running “Build” -> “Build & Reload” from the “Build” tab. This will compile the package code and create a binary package file (.tar.gz) in the “build/” directory.
  3. Finally, install the package by running “Install and Restart” from the “Build” tab. This will install the package on your local machine, making it available for use.

Using

Once the package is installed, you can load it into your R session using the library() function. Here’s an example:

library(my_new_rpackage)
say_hello()

This will output “Hello ASMS” to the console.

9.6 R Shiny Applications

R Shiny is an R package that allows users to create interactive web applications using R. With R Shiny, users can create and customize web-based dashboards, data visualization tools, and other interactive applications that can be easily shared with others.

The benefits of using R Shiny include creating powerful data-driven web applications with ease and providing a user-friendly interface for data analysis. R Shiny is widely used in various industries, including finance, healthcare, and e-commerce.

Creating

Creating an R Shiny application is relatively easy, and it can be done in the RStudio IDE. Here are the steps to follow:

  1. Open RStudio and create a new R script file.

  2. Install the ‘shiny’ R package by running the following command:

    install.packages("shiny")

  3. Load the ‘shiny’ package by running the following command:

    library(shiny)

  4. Create a new Shiny application by running the following command:

    shinyApp(ui = ui, server = server)

    The ‘ui’ argument should contain the user interface (UI) code for the application, while the ‘server’ argument should contain the server-side code for the application.

  5. Write the UI code and server-side code for your application, and save the file with a ‘.R’ extension.

  6. Run the application by clicking on the ‘Run App’ button in the RStudio IDE, or by running the following command:

    runApp("path/to/your/app.R")

Example

Here’s an example of an R Shiny application that allows users to plot points on a graph:

library(shiny)

# Define UI for application
ui <- fluidPage(
  titlePanel("Plotting Points"),
  sidebarLayout(
    sidebarPanel(
      numericInput("x", "X Coordinate:", 0),
      numericInput("y", "Y Coordinate:", 0),
      actionButton("plot", "Plot Point")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Define server logic
server <- function(input, output) {
  coords <- reactiveValues(x = numeric(), y = numeric())

  observeEvent(input$plot, {
    coords$x <- c(coords$x, input$x)
    coords$y <- c(coords$y, input$y)
  })

  output$plot <- renderPlot({
    plot(coords$x, coords$y, xlim = c(0, 10), ylim = c(0, 10), pch = 19, col = "blue")
  })
}

# Run the application
shinyApp(ui = ui, server = server)

In this example, the UI code defines a sidebar panel with input fields for the X and Y coordinates of a point, as well as a button to plot the point. The main panel contains a plot that displays all of the points that have been plotted by the user.

The server-side code defines a reactive variable called ‘coords’ which stores the X and Y coordinates of each plotted point. When the user clicks the ‘Plot Point’ button, an observer function is triggered that adds the new point to the ‘coords’ variable. The renderPlot function then plots all of the points on the graph.

Check out the R Shiny web page for more information.