top of page
  • Writer's picture♫Shokolatte♬

My First App (R)

Updated: Feb 19, 2022

Since I decide to write my capstone project in R programming language, I have been practicing coding literally all day and every day, in order to improve my level up to where I can manipulate the language to create an app on Shiny library.


Today, I created my first one!🎉🎊🎉🎊


The data is "mtcars" which every R programmers got mad love for, or are just too familiar with (R's pre-loaded dataset). The dataset contains only 11 variables with 32 rows but so popular as it's explorable to find correlations. The codes are written based on the material used in the Data Visualization with R | Coursera course which made me achieve my first goal of the year. I totally enjoyed each process during the course, while taking my time like 5 hours on each 5-minute videos in the course.


If you are not an R-coder, you probably wouldn't really care about '73 made cars (deep in my heart, me neither), but still, I would like for you to be encouraged to click on below and play with it a little bit. The color scheme and the plot themes are pretty random but I made it and kept it that way, so that I can modify them later with my own datasets later. I also know if seen on mobile phones, it's a bit hard to control. Remember, this is MY VERY FIRST app, I'm leaving lots of room for growth.




Just for the record, I'm leaving my codes here. I tried to make it as simple as it can.


ui


library(shiny)
library(tidyverse)
data("mtcars")

categorical_varibles = c('cyl', 'vs', 'am', 'gear', 'carb')


shinyUI(fluidPage(
  br(),
  titlePanel(em("Data Exploration Demo with Shiny by Sho-Viz")),

  sidebarLayout(
    sidebarPanel(
      h3('Explore mtcars'),
      p("The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models)"),
      br(),
      varSelectInput("continuous_variable",
                     "Select Continuous Variable for 1, 2, and 4.",
                     data = select(mtcars, -categorical_varibles),
                     selected = "mpg"),
      strong("Histogram settings for 1."),
      sliderInput("bins",
                  "Number of bins:",
                  min = 2,
                  max = 20,
                  value = 10),
      
      radioButtons("hist_fill",
                   "Histogram fill:",
                   choices = c("default", "gray", "pink", "blue")),
      
      varSelectInput("categorical_variable",
                     "Select Categorical Variable for 3, and 4.",
                     data = mtcars[categorical_varibles],
                     selected = "cyl"),
      br(),
      h4(em("Plot Variable Map Guide")),
      p('Miles/gallon = mpg', br(),
        'Displacement (cu in.) = disp', br(),
        'Gross horsepower = hp', br(),
        'Rear axle ratio = drat', br(),
        'Weight (1000 lbs) = wt', br(),
        '1/4 mile time = qsec', br(),
        'Number of cylinders = cyl', br(),
        'Engine\n(0 = V-shaped, 1 = straight) = vs', br(),
        'Transmission\n(0 = automatic, 1 = manual) = am', br(),
        'Number of forward gears = gear', br(),
        'Number of carburetors = carb')
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          "Distribution of Numerical Variables (1, 2)",
          plotOutput("p1"), 
          plotOutput("p2")   
        ),
        tabPanel(
          "Distribution of Categorical Variables (3)",
          plotOutput("p3")  
        ),
        tabPanel(
          "Plots for Observing Data Correlation (4)",
          plotOutput("p4"))
      )
    ),
    position = "right"
  )
))


server

library(shiny)
library(ggplot2)
library(ggthemes)

shinyServer(function(input, output) {
 
  output$p1 <- renderPlot({
    p <- ggplot(mtcars, aes(x = !!input$continuous_variable)) +
      labs(y = "Number of Cars", title = paste("1. Trend of ", input$continuous_variable))+
      theme_excel_new()
    
    if (input$hist_fill == "default") {
      p + geom_histogram(bins = input$bins, fill = "green", color = "dodgerblue3")
    }
    else if (input$hist_fill == "pink"){
      p + geom_histogram(bins = input$bins, fill = "pink", color = "red")
    }
    
    else if (input$hist_fill == "blue"){
      p + geom_histogram(bins = input$bins, fill = "lightblue", color = "purple")
    }
    
    else {
      p + geom_histogram(bins = input$bins, color = "white")
    }
  })
  
  output$p2 <- renderPlot({
    ggplot(mtcars, aes(y = !!input$continuous_variable)) + 
      geom_boxplot() + 
      labs(title = paste("2. How", input$continuous_variable, "value is spread")) +
      coord_flip()+
      theme_wsj()
  })
  
  output$p3 <- renderPlot({
    ggplot(data = mtcars, aes(x = factor(!!input$categorical_variable), fill = factor(!!input$categorical_variable))) +
      geom_bar() +
      labs(x = input$categorical_variable, title = paste("3. Trend of", input$categorical_variable)) +
      theme_tufte() + theme_dark()+
      scale_fill_brewer(palette = "OrRd")
  })
  
  output$p4 <- renderPlot({
    ggplot(mtcars, aes(x = !!input$continuous_variable, y = wt, color = factor(!!input$categorical_variable))) + 
      geom_point(size = 3) +
      labs(title = paste("4. Distribution of", input$continuous_variable, "with respect to Weight"))+
      theme_economist()
  })
})


I hope you enjoyed my first app, and if you actually did, I got so many questions for you.😂😂 Jk, I'm glad you did. But obviously I'm not gonna stop here and I'll be posting more soon.



Recent Posts

See All
bottom of page