Craig Wang
Craig Wang

Reputation: 56

shiny plotly no graph

I'm switching to plotly from ggplot for my shiny app, but when I run app from Rstudio the plots are not showing in browser, if I use print(gg) the plotly shows correctly in Rstudio viewer. The following minimal example works if I switch back to ggplot. Thanks!

ui.R

library(shiny)
library(httr)
library(jsonlite)

# get the list of all packages on CRAN
package_names = names(httr::content(httr::GET("http://crandb.r-
pkg.org/-/desc")))

shinyUI(fluidPage(

# Application title
"R Package Downloads",

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
  br(),
  selectInput("package", 
              label = "Packages",
              selected = "ggplot2", # initialize the graph with a random package
              choices = package_names,
              multiple = TRUE),      
  selectInput("plot1",
              label = "Select Plot Type",
              choices = c("","Downloads vs Time (monthly)","Downloads vs Time (cumulative)","Map (cumulative)","Map (dominance)"),
              multiple = FALSE)),

# Show a plot of the generated distribution
mainPanel(
  plotlyOutput("plot1")
))
))

server.R

library(shiny,quietly = T);    library(stringr,quietly = T)
library(dplyr,quietly = T);    library(plotly,quietly = T)
library(ggplot2,quietly = T);    library(lubridate,quietly = T)
library(cranlogs,quietly = T);    library(zoo,quietly = T)
library(scales,quietly = T);    library(broom,quietly = T)
library(rworldmap,quietly = T);    library(countrycode,quietly = T)
library(data.table,quietly = T)

shinyServer(function(input, output) {
  load("CRANlog_cleaned_month.RData")   
  output$plot1 <- renderPlotly({
    dat.p <<- subset(dat,package %in% input$package)
    dat.ts <<- aggregate(times~package+month,data=dat.p,sum)
      DTmonthly <<- ggplot(dat.ts, aes(month, times, color = package)) + 
        geom_line() + xlab("Date") + scale_y_continuous(name="Number of downloads", labels = comma)
      gg <- ggplotly(DTmonthly)
      gg
  })
})  

Upvotes: 1

Views: 620

Answers (1)

Craig Wang
Craig Wang

Reputation: 56

I managed to solve this by changing the name of selectInput from "plot1" to another name, might be because of the plotlyOutput is also using "plot1". This was fine if I just use plotOutput. Probably due to some error checking code is missing in plotlyOutput.

Upvotes: 1

Related Questions