Use a Shiny Dash Board to Choose Different Plotly Plots

341 views
Skip to first unread message

dividor

unread,
Jan 25, 2016, 7:18:58 PM1/25/16
to Shiny - Web Framework for R
I set up a local app with Shiny using native R plotting, where I can choose what type of plot from a drop down ...

It works fine with native R plotting. However when I try to use plotly, though I can get it to plot for single plots, once I add in if statements to call different plotting routines based on the drop-down, I don't get any plots output.

Code below, am I doing something silly?



UI.R

library(shiny)
library(plotly)

shinyUI(fluidPage(
  titlePanel("Help Library Analysis"),
  sidebarPanel(
    selectInput("plot_type", "Plot Type:",
                c("Page views by ION area" = "ua_views_by_area",
                  "Word Cloud" = "ua_search_word_cloud",
                  "Legacy Content Age versus Page Views" = "ua_age_access"
                  )),
    dateRangeInput("dates", 
                   "Date range",
                   start = "2015-12-01", 
                   end = as.character(Sys.Date()))
  ),
  mainPanel(
    plotlyOutput("trendPlot") 
  )
))

Server.R 

library(shiny)
library(plotly)
source("helpers.R")

shinyServer(function(input, output) {
  
  output$trendPlot <- renderPlotly({
    
    if(input$plot_type == "ua_views_by_area") ua_views_by_area(start_date,end_date)
    
    if(input$plot_type == "ua_age_access") ua_age_access(start_date,end_date)
    
  })
})
 


helper.R contains the plot routines that ultimately call plotly with something like this, eg ua_views_by_area

  f <- list(
          family
= "Courier New, monospace",
          size
= 18,
          color
= "#7f7f7f"
 
)
  x
<- list(
          title
= "Legacy Document Age (Years)",
          titlefont
= f
 
)
  y
<- list(
          title
= "UA System Page Views",
          titlefont
= f
 
)
  p
<- plot_ly(data = allData, x = allData$Years_Old, y = allData$pageViews, mode = "markers",
               color
= allData$Space, text=allData$Title) %>% layout(xaxis = x, yaxis = y)

Joe Cheng

unread,
Jan 25, 2016, 11:48:09 PM1/25/16
to dividor, Shiny - Web Framework for R
renderPlotly looks at whatever value is returned. The way you have it written now, unless it's "ua_age_access", the return value will be NULL (as an "if" statement that evaluates to false and doesn't have an "else" clause will return NULL).

If you change the code to either of the following I think it'll probably work:

    if(input$plot_type == "ua_views_by_area") ua_views_by_area(start_date,end_date)
    else if(input$plot_type == "ua_age_access") ua_age_access(start_date,end_date)
or
    if(input$plot_type == "ua_views_by_area") return(ua_views_by_area(start_date,end_date))   
    if(input$plot_type == "ua_age_access") return(ua_age_access(start_date,end_date))

or
    switch(input$plot_type,
      ua_views_by_area = ua_views_by_area(start_date,end_date),
   
      ua_age_access = ua_age_access(start_date,end_date)
    )


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/90445a98-2e30-4c55-aaf6-a590277c726c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dividor

unread,
Jan 26, 2016, 8:33:57 AM1/26/16
to Shiny - Web Framework for R, ionua...@gmail.com
Great! The switch worked perfectly.
Reply all
Reply to author
Forward
0 new messages