Lab 8A: R in Practice (Easy)

  1. Load shiny
library(shiny)
  1. Create a UI
ui <- fluidPage("Hello World!")
  1. Create a server
server <- function(input, output) {}
  1. Create a shiny app
shinyApp(
    ui = ui,
    server = server)
  1. Create a UI with I/O controls
ui <- fluidPage(
    titlePanel("Input and Output"),
    sidebarLayout(
        sidebarPanel(
            sliderInput(
                inputId = "num",
                label = "Choose a Number",
                min = 0,
                max = 100,
                value = 25)),
        mainPanel(
            textOutput(
                outputId = "text"))))
  1. Create a server than maps input to output
server <- function(input, output) {
    output$text <- renderText({
        paste("You selected ", input$num )})
}
  1. Create a shiny app
shinyApp(
    ui = ui,
    server = server)
  1. Set the working directory
setwd("C:/Workshop/Data")
  1. Load the decision tree model
load("Tree.RData")
  1. Load the color brewer package
library(RColorBrewer)
  1. Create a color palette
palette <- brewer.pal(3, "Set2")
  1. Create user interface code
ui <- fluidPage(
    titlePanel("Iris Species Predictor"),
    sidebarLayout(
        sidebarPanel(
            sliderInput(
                inputId = "petal.length",
                label = "Petal Length (cm)",
                min = 1,
                max = 7,
                value = 4),
            sliderInput(
                inputId = "petal.width",
                label = "Petal Width (cm)",
                min = 0.0,
                max = 2.5,
                step = 0.5,
                value = 1.5)),
        mainPanel(
            textOutput(
                outputId = "text"),
            plotOutput(
                outputId = "plot"))))
  1. Create server code
server <- function(input, output) {
    output$text = renderText({
        
        # Create predictors
        predictors <- data.frame(
            Petal.Length = input$petal.length,
            Petal.Width = input$petal.width,
            Sepal.Length = 0,
            Sepal.Width = 0)
        
        # Make prediction
        prediction = predict(
            object = treeModel,
            newdata = predictors,
            type = "class")
        
        # Create prediction text
        paste(
            "The predicted species is ",
            as.character(prediction))
    })
    
    output$plot = renderPlot({
        
        # Create a scatterplot colored by species
        plot(
            x = iris$Petal.Length, 
            y = iris$Petal.Width,
            pch = 19,
            col = palette[as.numeric(iris$Species)],
            main = "Iris Petal Length vs. Width",
            xlab = "Petal Length (cm)",
            ylab = "Petal Width (cm)")
        
        # Plot the desicion boundaries
        partition.tree(
            treeModel,
            label = "Species",
            add = TRUE)
        
        # Draw predictor on plot
        points(
            x = input$petal.length,
            y = input$petal.width,
            col = "red",
            pch = 4,
            cex = 2,
            lwd = 2)
    })
}
  1. Create a shiny app
shinyApp(
    ui = ui,
    server = server)