ETC5523: Communicating with Data

Tutorial 8

Author

Michael Lydeamore

Published

August 1, 2001

🎯 Objectives

  • make your first R package
  • make a simple R package as a container for data
  • add a shiny app to an R package with function to launch it
  1. install.packages(c("devtools", "usethis", "roxygen2"))

Exercise 8A

Use the usethis package to create a new package called cwdata

library(usethis)
create_package("cwdata")

Edit the DESCRIPTION file created and modify the Authors@R tag to include yourself as the creator and author of the package.

Exercise 8B

Use the usethis package to create an R script called key_crop_yields.R in the data-raw directory in your packages folder.

use_data_raw(name = "key_crop_yields")

The data-raw directory is used to hold scripts that generate package data. We will use the data from Our World in Data and sourced from Tidy Tuesday. This data contains agricultural yields across crop types and by entity (country or region) from 1960 to 2018.

Edit the script to process the data into a long form tidy representation. Here’s some code to get you started:

library(janitor)
library(tidyverse)
url <- 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/key_crop_yields.csv'

key_crop_yields <- read_csv(url) %>% 
  clean_names() %>% 
  pivot_longer(
    cols = -___,
    names_to = "crop", 
    values_to = "yield",
    names_pattern = "([^_]+)"
  )

After you’ve finished writing your script make sure you’ve run it.

You should now have a folder called data in your package, with a file called key_crop_yields.rda.

Exercise 8C (Optional)

We cover documentation next week but if you would like to tackle this, take a look at R Packages Section 8.2.1 for guidance.

We now need to create an R script that lives in the R directory that documents our data object key_crop_yields.

use_r("key_crop_yields.R")

Modify the script and document the data.

  • Describe the number of rows and columns
  • Type of object key_crop_yields is.
  • What are the types of the columns?
  • What do the columns mean?
  • Where did you find it?

It is possible to use ordinary markdown syntax in documenation with roxygen2 by running (in fact this is set by default if you used usethis::create_package())

use_roxygen_md()

Once you’ve finished modifying run

devtools::document()

at the console to build your package documentation.

Exercise 8D

We now have a minimally working package that contains some data. Restart your R session and then run

devtools::load_all()

Now the key_crop_yields data is your available to your R session.

Edit your DESCRIPTION file to have a more appropriate title and description and version number.

After you’ve finished editing run:

devtools::check()

Do you get any errors or warnings?

Once you have passed the check, install the package locally:

devtools::install()

Restart your R session and try loading your package. Check out the help page for the data (if you did 8C).

library(cwdata)
key_crop_yields
?key_crop_yields

Exercise 8E

Make a shiny app that explores the crop yields over time facetted by the crop type by selecting a particular country or region. Below is a template for you to get started.

library(shiny)
library(tidyverse)
library(cwdata)

ui <- fluidPage(
  br(),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("entity",
                     "Select a country or region:",
                     choices = ___,
                     selected = "Australia")
    ),
    mainPanel(
      plotOutput("tsplot")
    )
  )
)

server <- function(input, output, session) {

  output$tsplot <- renderPlot({
      ___
  })

}

shinyApp(ui, server)

Exercise 8F

Add this shiny app to the cwdata package along with an R function to launch the app.

An example package for this tutorial can be found here.