Integrate plotly with shinydashboard

血红的双手。 提交于 2020-05-13 04:43:15

问题


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?


回答1:


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({
....


来源:https://stackoverflow.com/questions/33941232/integrate-plotly-with-shinydashboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!