Statistics/DATA Manipulating

Building Web App with Shiny in R - Day 1

딥스탯 2018. 6. 15. 01:07
Building Web App with Shiny in R - Day 1

참고자료

https://www.datacamp.com/courses/building-web-applications-in-r-with-shiny (Building Web Applications in R with Shiny, DataCamp, Mine Cetinkaya-Rundel)

https://www.rstudio.com/resources/cheatsheets/ (Rstudio Cheat Sheets)

https://shiny.rstudio.com/ (Shiny homepage)

http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html (mtcars)

Building Web App with Shiny in R - Day 1.

Loading packages

# install.packages(c("shiny","ggplot2"))
library(shiny)
library(ggplot2)

Manipulating a dataset (mtcars)

mtcars$cyl  <- factor(mtcars$cyl)
mtcars$vs   <- factor(mtcars$vs)
mtcars$am   <- factor(mtcars$am)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)

cont_choices <- c("Miles/(US) gallon"     = "mpg",
                  "Displacement (cu.in.)" = "disp",
                  "Gross horsepower"      = "hp",
                  "Rear axle ratio"       = "drat",
                  "weight"                = "wt",
                  "1/4 mile time"         = "qsec")

disc_choices <- c("Number of cylinders"   = "cyl",
                  "V/S"                   = "vs",
                  "Transmission (0 = automatic, 1 = manual)" = "am",
                  "Number of forward gears" = "gear",
                  "Number of carburetors"   = "carb")

Step 1 - Editing user interface (UI)

ui - nested R functions that assemble an HTML user interface for the app.

ui <- fluidPage(

  sidebarLayout(

    sidebarPanel(
      
      selectInput(inputId  = "Y", 
                  label    = "Y-axis:",
                  choices  = cont_choices, 
                  selected = cont_choices[1]),
      
      selectInput(inputId  = "X", 
                  label    = "X-axis:",
                  choices  = cont_choices, 
                  selected = cont_choices[2]),
      
      selectInput(inputId  = "Color", 
                  label    = "Color by:",
                  choices  = disc_choices,
                  selected = disc_choices[1]),
      
      sliderInput(inputId = "Alpha",
                  label   = "Alpha:",
                  min     = 0,
                  max     = 1,
                  value   = 1)
    ),

    mainPanel(
      plotOutput(outputId = "plot1")
    )
  )
)

Step 2 - Editing server

server - a function with instructions on how to build and rebuild the R objects displayed in the UI

server <- function(input, output) {

  output$plot1 <- renderPlot({
    ggplot(data = mtcars,
           aes_string(x = input$X,
                      y = input$Y,
                      colour = input$Color)) +
      geom_point(alpha = input$Alpha) +
      theme_classic()
  })
}

Step 3 - Combining ui and server

shinyApp - combines ui and server into the app. Wrap with runApp() if calling from a sourced script or inside a function.

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

A screen shot