43  Building Data Products

43.1 Manipulate

The manipulate package creates a quick interactive graphic that offers simple controls, including sliders, pickers and checkboxes.

library(manipulate) 
manipulate(plot(1:x), x = slider(1, 100))

With manipulate you can have more than one set of controls by simply adding more arguments to the manipulate function.

Note that it’s difficult to share a manipulate interactive graphic.

This is a link on how manipulate actually works: link

43.2 Shiny

It is described by RStudio as “A web application framework for R”. “Turn your analyses into interactive web applications No HTML, CSS, or JavaScript knowledge required”.

Only those who have shiny installed and have access to your code could run your web page. However, RStudio offers a service for hosting shiny apps (their servers) on a platform called shinyapps.io.

If on Windows, make sure that you have Rtools installed. Then, you can install shiny with library(shiny):

Example:

library(shiny)
textInput("name", "What is your name?")
numericInput("age", "How old are you?", NA, min = 0, max = 150)

A shiny app consists of two files. First, a file called ui.R that controls the User Interface (hence the ui in the filename) and secondly, a file server.R that controls the shiny server (hence the server in the filename).

43.2.1 Create ui.R file:

library(shiny)
shinyUI(
  pageWithSidebar( 
    headerPanel("Hello Shiny!"), 
    sidebarPanel( 
      h3('Sidebar text')
    ),
    mainPanel( 
      h3('Main Panel text')
    )
  )
)

43.2.2 Create server.R file:

library(shiny) 
shinyServer( 
  function(input, output) {
  }
)

The current version of Rstudio has a “Run app” button in the upper right hand corner of the editor if a ui.R or server.R file is open.

To get used to programming shiny apps, you need to throw away a little of your thinking about R; it’s a different style of programming.

43.2.3 Types of inputs

  • Numeric input:

    numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 10, step= 1)
  • Checkbox input:

    checkboxGroupInput("id2", "Checkbox", c("Value 1" = "1", "Value 2" = "2", "Value 3" = "3"))
  • Date input:

    dateInput("date", "Date:")

43.2.4 Sharing your app

Now that we have a working app we’d like to share it with the world. It’s much nicer to have it display as a standalone web application.

This requires running a shiny server to host the app. Instead of creating and deploying our own shiny server, we’ll rely on RStudio’s service: link

  • After login in shinyapps:

    install.packages("devtools")
    install.packages("shinyapps")
  • Run code:

    shinyapps::setAccountInfo(name='<ACCOUNT NAME>', 
          token='<TOKEN>', 
          secret='<SECRET>')
  • Submit code:

    deployApp(appName = "myFirstApp")

43.2.5 Build a GUI with html

Check out this link

43.3 Reproducible presentations

43.3.1 Slidify

Slidify is for building reproducible presentations.

install.packages("devtools") 
library(devtools)/ require(devtools)
install_github('ramnathv/slidify')
install_github('ramnathv'/'slidifyLibraries') o devtools::install_github(pkgs, force = TRUE)
library(slidify)

43.3.2 R studio presenter

For more information about R studio presenter, chek out this link

Tip: if you’re sort of a hacker type and you like to tinker with things, use slidify. If you just want to get it done and not worry about it, use RPres. Either way, you really can’t go wrong.

43.4 Interactive Graphs

43.4.1 htmlwidgets

HTML is an interactive format, and you can take advantage of that interactivity with htmlwidgets, R functions that produce interactive HTML visualizations. For example, take the leaflet map below, where you can drag the map around, zoom in and out, etc. You obviously can’t do that in a book, so Quarto automatically inserts a static screenshot for you.

Example:

library(leaflet)
leaflet() |>
  setView(-3.7077, 40.4156, , zoom = 16) |> 
  addTiles() |>
  addMarkers(-3.7077, 40.4156, popup = "Plaza Mayor de Madrid") 

There are many packages that provide htmlwidgets, including:

  • dygraphs for interactive time series visualizations.

  • DT for interactive tables.

  • threejs for interactive 3d plots.

  • DiagrammeR for diagrams (like flow charts and simple node-link diagrams).

To see a complete list of the packages that provide htmlwidgets visit https://www.htmlwidgets.org.

43.4.2 leaflet

link to leaflet

leaflet seems to be emerging as the most popular R package for creating interactive maps.

The map widget (the leaflet() command) starts out a map and then you add elements or modify the map by passing it as arguments to mapping functions.

43.4.3 rCharts

link to rCharts

rCharts is a way to create interactive javascript visualizations using R.

require(devtools) 
install_github('rCharts', 'ramnathv')

43.4.4 googleVis

link to googlevis

Google has some nice visualization tools built into their products (e.g. Google Maps). These include maps and interactive graphs.

install.packages("googleVis")
library(googlevis)

43.4.5 plot.ly

link to plot.ly

plotly relies on the platform/website plot.ly for creating interactive graphics.

require(devtools) 
install_github("ropensci/plotly")

Plotly will give you the json data and gives a tremendous number of options for publishing the graph.

Notably, plotly allows for integration with ggplot2.

For interactive graphics, learning some javascript and following that up with D3 would be the logical next step