Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate plotly with shinydashboard

I have used shiny for several small scale project and am now giving a try to the shinydashboard package which looks rather promising. I also want to integrate interactive plot using plotly (although other libraries can be considered in the future).

I have no problem running examples integrating plotly in shiny (example) but face problems when attempting to achieve a similar results with shinydashboard.

For example, the code below (app.R) is not working properly.

library(shiny)
library(shinydashboard)
library(plotly)
library(ggplot2)




data(movies, package = "ggplot2")
minx <- min(movies$rating)
maxx <- max(movies$rating)


#######
####### UI
#######

ui <- dashboardPage(

  ############# Header
  dashboardHeader(title = "Test"),

  ############# Sidebar
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
    )
  ),


  ############# Dashboard body
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(plotOutput("plot1"), width=6, height=500),
      box(plotOutput("plot2"), width=6, height=500)
    )
  )
)


#######
####### Server
#######


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



  output$plot1 <- renderPlot({


    # a simple histogram of movie ratings
    p <- plot_ly(movies, x = rating, autobinx = F, type = "histogram",
                 xbins = list(start = minx, end = maxx, size = 2))
    # style the xaxis
    layout(p, xaxis = list(title = "Ratings", range = c(minx, maxx), autorange = F,
                           autotick = F, tick0 = minx, dtick = 2))

  })

  output$plot2 <- renderPlot({


    hist(movies$rating, col="blue")

  })
}


shinyApp(ui, server)

The page is supposed to display two similar plots: one generated with plotly, the other with R base. The second one is displayed properly but the plotly one is displayed in Rstudio viewer panel. If I run the App on a terminal using the runApp() function, one webpage opens for the dashboard and another one using only for the plotly interactive plot.

I have tried to create some reactive plots (plots change in function of the content of some shiny controls such as slide bars) and the plot opened either in the viewer or another browser window/tab is updated. So everything seems to work, the only problem is that the plotly plot is not inserted in the dashboard but in a different location (viewer or other tab), which, of course, is a problem.

Despite of my research, I could not find a solution to the problem (or even people mentioning it...). Any clue?

like image 816
Philippe Avatar asked Nov 26 '15 14:11

Philippe


People also ask

Can I use Plotly in shiny?

Creating the observeEvent for Plotly. Within Shiny, we can use either the observeEvent or observe functions to see how the end-user interacts with the app. Those functions can then make adjustments to the app, based on what code we add.

How do you render a Plotly graph shiny?

Plotly charts are rendered with plotlyOutput() and renderPlotly() . Two changes to the code are required: Change mainPanel() to mainPanel(plotlyOutput("crimeplot")) Change output$crimeplot to output$crimeplot<-renderPlotly({


1 Answers

Check again the tutorial carefully, I think you need to replace in the ui

  box(plotOutput("plot1"), width=6, height=500)

by

  box(plotlyOutput("plot1"), width=6, height=500)

And in the server just replace

output$plot1 <- renderPlot({
......

by

output$plot1 <- renderPlotly({
....
like image 109
dickoa Avatar answered Sep 21 '22 00:09

dickoa