How to switch between different ggplot types?

Viewed 61

I want to have an application which plots my data in different types (as Line, as dots, as boxplot).

Let's consider my code :

library(shiny)
library(quantmod)

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
# Apple stock
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close

### Define UI for application
ui <- fluidPage(

  # Sidebar panel
  sidebarPanel(
    selectInput("type",
      label = "1. Select plot type",
      choices = c("Line" = "geom_line()", "Dot" = "geom_point()", "Boxplot" = "geom_boxplot()"),
      selected = 3
    )
  ),

  # Main Panel
  mainPanel(
    plotOutput("sp")
  )
)





server <- function(input, output) {
  output$sp <- renderPlot({
    colm <- as.numeric(apple)
    ggplot() +
      aes(x = 1:length(colm), y = colm) +
      geom_line() +
      xlab(NULL)
  })
}

# Run the application
shinyApp(ui = ui, server = server)

enter image description here

And I'm not sure what sure I put in server to be able to switch between these different types. I tried to use if statement and get(input$type) but both options give me nothing more than errors.

To summarize - How can I write my application so that I can switch between different plot types (Line, Dots, Boxplot) as shown in image ?

2 Answers

This could be achieved like so. You can e.g. use a switch statement to assign the geom_xxx depending on type

library(shiny)
library(quantmod)
library(ggplot2)

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
# Apple stock
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close

### Define UI for application
ui <- fluidPage(

  # Sidebar panel
  sidebarPanel(
    selectInput("type",
      label = "1. Select plot type",
      choices = c("Line" = "line", "Dot" = "point", "Boxplot" = "box"),
      selected = 3
    )
  ),

  # Main Panel
  mainPanel(
    plotOutput("sp")
  )
)

server <- function(input, output) {
  output$sp <- renderPlot({
    colm <- as.numeric(apple)
    geom <- switch(input$type,
      line = geom_line(),
      point = geom_point(),
      box = geom_boxplot()
    )
    ggplot() +
      aes(x = 1:length(colm), y = colm) +
      geom +
      xlab(NULL)
  })
}

# Run the application
shinyApp(ui = ui, server = server)

You can use match.fun to find the function to apply :

library(shiny)
library(quantmod)

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
# Apple stock
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close

### Define UI for application
ui <- fluidPage(
  # Sidebar panel
  sidebarPanel(
    selectInput("type",
                label = "1. Select plot type",
                choices = c("Line" = "geom_line", 
                            "Dot" = "geom_point", "Boxplot" = "geom_boxplot"),
                selected = 3
    )
  ),
  
  # Main Panel
  mainPanel(
    plotOutput("sp")
  )
)

server <- function(input, output) {
  output$sp <- renderPlot({
    colm <- as.numeric(apple)
    p <- ggplot() + aes(x = 1:length(colm), y = colm) + xlab(NULL)
    p + match.fun(input$type)()
      
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Related