与ShinyDashboard有计划地集成 [英] Integrate plotly with shinydashboard

查看:0
本文介绍了与ShinyDashboard有计划地集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在几个小规模的项目中使用过SHINY,现在正在尝试shinydashboard包,它看起来相当有前途。我还希望使用PLOTLY集成交互式绘图(尽管将来可以考虑使用其他库)。

我可以轻松地运行在SHINY(example)中集成的示例,但在尝试使用shinydashboard实现类似结果时会遇到问题。

例如,下面的代码(app.R)运行不正常。

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)

该页面应该显示两个类似的绘图:一个使用Plotly生成,另一个使用R BASE生成。第二个可以正确显示,但有情节的显示在RStudio查看器面板中。如果我使用runApp()函数在终端上运行App,则会打开一个用于仪表板的网页,另一个网页仅用于图形交互绘图。

我曾尝试创建一些反应性绘图(绘图会根据某些闪亮控件的内容而改变,如滑动条),并且在查看器或另一个浏览器窗口/选项卡中打开的绘图会被更新。 所以一切似乎都很正常,唯一的问题是绘图没有插入仪表板,而是插入到了不同的位置(查看器或其他选项卡),这当然是一个问题。

尽管我做了研究,但我找不到问题的解决方案(甚至没有人提到它...)。有线索吗?

推荐答案

再次仔细查看教程,我认为您需要在ui

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

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

server中只需替换

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

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

这篇关于与ShinyDashboard有计划地集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆