Overview of the youdrawitR Package

Interactive Data Visualization in R

Dillon Murphy, Cal Poly - San Luis Obispo, CA

Google Summer of Code 2023

Overview of the youdrawitR Package

  Interactive Data Visualization in R

 Engaging users with data

Motivations Behind Creating the Package

D3 is not intuitive for us R users

Add additional functionality for broader use

Getting Started with youdrawitR

Using youdrawitR

  • Install: devtools::install_github("earobinson95/youdrawitR")

  • Load: library(youdrawitR)

  • Data generation: customDataGen() and linearDataGen()

  • Visualization: drawr()

Package website:https://earobinson95.github.io/youdrawitR/

Data Generation with youdrawitR

  • Using customDataGen and linearDataGen for data preparation.
# Example of using customDataGen
custom_data <- customDataGen(
  df = mtcars,
  xvar = "mpg",
  yvar = "hp",
  regression_type = "linear",
  conf_int = TRUE
)
# Example of using linearDataGen
linear_data <- linearDataGen(
  N = 20,
  slope = 1.5,
  y_int = 0,
  sigma = 2,
  x_min = 0,
  x_max = 20,
  conf_int = TRUE
)

Visualization with youdrawitR

Visualizing with drawr

  • Renders interactive plots
  • Users can draw predictions
  • Customization options
# Example using previously generated custom_data
drawr(custom_data, 
      title = "Horsepower vs Miles per Gallon", 
      subtitle = "For mtcars dataset",
      x_lab = "Miles per Gallon",
      y_lab = "Horsepower",
      conf_int = TRUE)

More Examples:

Logistic Regression

# Generating data with logistic regression
# For this example, we'll need a binary response variable. 
# Let's create a binary variable based on horsepower.
mtcars$high_hp <- ifelse(mtcars$hp > 120, 1, 0)

logistic_data <- customDataGen(
  df = mtcars,
  xvar = "mpg",
  yvar = "high_hp",
  regression_type = "logistic"
)

drawr(logistic_data, 
      title = "Probability High Horsepower vs MPG (Logistic)", 
      subtitle = "For mtcars dataset",
      x_lab = "Miles per Gallon",
      y_lab = "High Horsepower (1 = Yes, 0 = No)")

More Examples:

Polynomial Regression & Customized Plot

# Generating data with polynomial regression of degree 2
poly_data <- customDataGen(
  df = mtcars,
  xvar = "mpg",
  yvar = "hp",
  regression_type = "polynomial",
  degree = 2
)

# Customized drawr output
drawr(poly_data, 
      title = "Horsepower vs Miles per Gallon (Polynomial)", 
      subtitle = "For mtcars dataset",
      x_lab = "Miles per Gallon",
      y_lab = "Horsepower",
      drawn_line_color = "red",
      true_line_color = "rgba(0,255,0,.8)",
      draw_region_color = "#FFFFFF",
      show_tooltip = TRUE,
      aspect_ratio = 1.5)

More Examples:

Loess Regression

# Generating data with loess regression
loess_data <- customDataGen(
  df = mtcars,
  xvar = "mpg",
  yvar = "hp",
  regression_type = "loess",
  span = 0.75
)

drawr(loess_data, 
      title = "Horsepower vs Miles per Gallon (Loess)", 
      subtitle = "For mtcars dataset",
      x_lab = "Miles per Gallon",
      y_lab = "Horsepower")

Your Turn 1: Using drawr

https://earobinson95.github.io/youdrawitR/articles/customDataGen.html

  1. Install and load youdrawitR

  2. Generate data using the customDataGen function and your favorite R data set

  3. Pass your generated data into the drawr function to create your interactive graphic

  4. Using the documentation help(drawr) make changes to your graphic such as: changing colors, hiding the true line, or adjusting the start of the drawn line (Hint: look at the example in documentation).

Embedding youdrawitR into HTML

  • Quarto
# Render with quarto/Rmarkdown  
## For quarto: use quarto >= 1.3.450 or knitr <1.43
drawr(linear_data)
  • Direct Save as HTML
# Save into an html
drawr(linear_data, 
      save_html_file_path = "myViz.html")

## Embedded HTML below

Using youdrawitR with Shiny

‘Can You Draw It?’ Shiny App

  • Real time interaction with plot:
    • Change data sources: Input, Simulate, or Use R Dataset
    • Drawing, saving users drawn lines, and resetting functionalities
    • Customization: Color changes, tooltips, confidence intervals.
  • Access:
shiny::runApp(system.file("shinyapp/youdrawit", package = "youdrawitR"))

Try For Yourself

Your Turn 2: Drawing Cat Competition

Add your cat to the slide deck at https://docs.google.com/presentation/d/1prNX34FKP45x9FkRH_f35ejHhDUAdrfRHWAjY_hacQU/edit?usp=sharing

Integrating Into Shiny

  • Displaying Visualizations
# ui 
r2d3::d3Output("shinydrawr", height = "500px", width = "800px")

# server
output$shinydrawr <- r2d3::renderD3({ drawr(data, hide_buttons = T )})
  • Retrieving Drawn Data
# Drawn data is sent to shiny from the 
# js file upon completion of the original line.

# Fetch the original line data
jsonlite::fromJSON(input$completedLineData)

# Fetch any additional lines drawn
jsonlite::fromJSON(input$newLineData)
  • Adding Button Functionality
# Send message to js file
session$sendCustomMessage("resetAction", "true")
session$sendCustomMessage("newLine", "true")

Your Turn 3: Create a Shiny App

  1. File > New File > Shiny Web App (multiple files)
  2. Copy the ui.R and server.R code from the next two slides
  3. Add your generated data and interactive plot from Your Turn 1
  4. Print out the user original line

Your Turn 3: ui.R

library(shiny)

# Define UI
fluidPage(
  # Application title
  titlePanel("My drawr plot"),
      mainPanel(
        # Show the interactive plot of the generated data
        r2d3::d3Output("shinydrawr"),
        
        fluidRow(
          column(
            width = 4,
            # Add the reset button
            actionButton("resetBtn", "Reset")
          ),
          column(
            width = 4,
            # Add the checkbox for confidence interval
            checkboxInput("confInterval", "Show Confidence Interval", value = TRUE)
          ),
          column(
            width = 4,
            # Show the user data
            tableOutput("user_data")
          )
        )
      )
)

Your Turn 3: server.R

library(shiny)
library(youdrawitR)

function(input, output, session) {
  custom_data <- reactive({
    customDataGen(
      # Paste your previously written code for data generation here!
      conf_int = input$confInterval
    )
  })
  
  observeEvent(input$completedLineData, {
    # This block will be executed whenever completedLineData is updated from the JS side
    output$user_data <- renderTable({
      jsonlite::fromJSON(input$completedLineData)
    })
  })
  
  observeEvent(input$resetBtn, {
    session$sendCustomMessage("resetAction", "true")
  })

  output$shinydrawr <- r2d3::renderD3({ drawr(custom_data(), 
                                              hide_buttons = T, 
                                              conf_int = input$confInterval) })
}

Importance & Uses of youdrawitR

Potential Applications

  • Engaging educational tool.
  • Personal data visualization projects.
  • Encouraging active participation in data interpretation.
  • Testing graphics for perception.

Future Directions for youdrawitR

  • Continuous development.
  • Potential enhancements based on feedback.
  • Expansion of features and functionalities.