6

I'm using renderPlotly for my ggplot graph in shiny. when I run the app no errors are shown but no plots are rendered as well. Is there a problem with my code?

Here's an example of my data set

year region     mean_price
  <int> <fct>           <dbl>
1  2007 Central       360769.
2  2007 East          255519.
3  2007 North         218453.
4  2007 North_East    263780.
5  2007 West          233401.
6  2008 Central       429607.

Here's my code

library("shiny")
library("shinythemes")
library('plotly')
ui <-fluidPage(
    theme=shinytheme("superhero"),
    titlePanel("Average Resale Prices "),
    sidebarLayout(
        sidebarPanel(
            selectInput("year","Year", choices = c("All","2007","2008",
                                                   "2009","2010","2011",
                                                   "2012","2013","2014",
                                                   "2015","2016","2017"), selected="All"),
            selectInput("region",'Region',choices=c("All",'North','Central','North-East','West','East'),selected="All"),

            selectInput("type","Flat-Type",choices = c("All","3 ROOM",'4 ROOM',"5 ROOM"),selected = "All"),width = 2
        ),
        mainPanel(
            tabsetPanel(
                tabPanel("Summary", plotOutput(outputId = "lineChart")),
                tabPanel("Breakdown", plotOutput(outputId = "lineChart1")), 
                type="tab"

            )

        )
    )
)



# Define server logic required to draw a line graph ----

server <- function(input, output, session){
    #df1<-reactive({
    #if(input$type =="All"){
    # plotdata1%>%
    #dplyr::filter(flat_type %in% c("3 ROOM","4 ROOM","5 ROOM"))
    #  }

    # else{
    # plotdata1%>%
    # dplyr::filter(flat_type %in% input$type)
    #   }
    # })


    plotdata1<-data1 %>% 
        group_by(year, region) %>% 
        summarize(mean_price=mean(resale_price))



    options(scipen = 100000)

    output$lineChart <- renderPlotly({
        ggplot(data=plotdata1,aes(x=year,y=mean_price))+
            geom_line(stat = 'identity',aes(colour=region,group=region))+
            geom_point()+
            xlim(c(2006,2018))+
            ylab("Average Price")+
            xlab('Year')

})

}


# Create Shiny object
shinyApp(ui = ui, server = server)
Elliot Tan
  • 149
  • 4
  • 11
  • 2
    Try adding `%>% ggplotly()` at the end of your plot code OR use `renderPlot` with current code. Also you need to use `plotlyOutput` in `ui` if you want to use `renderPlotly`. – Shree Jul 17 '19 at 23:58
  • also might try defining `p <- ggplot(...)` then `p <- ggplotly(p)` then `print(p)` – dbo Jul 18 '19 at 00:11

1 Answers1

7

As mentioned above

You need to use ggplotly if you intend to use plotly to render your graph. This also means you need to use plotlyOutput in the ui.

Additionally make sure all the libraries you want to use are installed. I seemed to not get an error message even though shinythemes was not installed... Although this is unlikely.

Below Code Runs for me generating a plotly graph.

library("shiny")
library("shinythemes")
library('plotly')
library("dplyr")

year <- c(2007,2007,2007,2007,2007,2008)
region <- c("central", "East", "North", "North_East", "West", "Central")
resale_price <- c(360769, 255519, 218453, 263780, 233401, 429607)
data1 <- data.frame(year,region,resale_price)

ui <-fluidPage(
  theme=shinytheme("superhero"),
  titlePanel("Average Resale Prices "),
  sidebarLayout(
    sidebarPanel(
      selectInput("year","Year", choices = c("All","2007","2008",
                                             "2009","2010","2011",
                                             "2012","2013","2014",
                                             "2015","2016","2017"), selected="All"),
      selectInput("region",'Region',choices=c("All",'North','Central','North-East','West','East'),selected="All"),

      selectInput("type","Flat-Type",choices = c("All","3 ROOM",'4 ROOM',"5 ROOM"),selected = "All"),width = 2
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Summary", plotlyOutput("lineChart")),
        type="tab"
      ))
  )
)

# Define server logic required to draw a line graph ----

server <- function(input, output, session){

  plotdata1<-data1 %>% 
    group_by(year, region) %>% 
    summarize(mean_price=mean(resale_price))

  options(scipen = 100000)

  output$lineChart <- renderPlotly({
   p <- ggplot(data=plotdata1,aes(x=year,y=mean_price))+
      geom_line(stat = 'identity',aes(colour=region,group=region))+
      geom_point()+
      xlim(c(2006,2018))+
      ylab("Average Price")+
      xlab('Year')
   p <- ggplotly(p)
   p
  })

}

# Create Shiny object
shinyApp(ui = ui, server = server)

Output

snippet

RK1
  • 2,384
  • 1
  • 19
  • 36