【问题标题】:Show "loading graph" message in plotly在情节中显示“加载图”消息
【发布时间】:2016-07-07 20:56:10
【问题描述】:

我想在绘图过程中显示“正在加载图表”之类的消息。

我如何做到这一点?

预期输出:

【问题讨论】:

  • 是的..我将它用于其他功能,它只是在仪表板上显示一条消息..我正在寻找的内容可以在我从 plotly 网站捕获的图像中看到..他们正在情节中显示此图像。我不想在仪表板上显示消息,但在情节区域本身中显示这样的图像..

标签: r shiny plotly shinydashboard


【解决方案1】:

我想出了一个从https://codepen.io/doeg/pen/RWGoLR开始的方法。

  1. 将 CSS 的内容复制到一个外部 css 文件中,然后将该文件放在名为 www 的子目录中。
  2. 在 Shiny 脚本中引用该文件。
  3. 在您的脚本中插入适当的 div 语句以包装您要加载的代码。
  4. 修改您的 CSS,使动画的 z-index 低于绘图的 z-index,以便当您的绘图出现时,它会呈现在您的动画之上。

例如,www/custom.css:

.plotly.html-widget.html-widget-output.shiny-bound-output.js-plotly-plot {
  z-index: 22;
  position: relative; 
}

.plotlybars {
  padding: 0 10px;
  vertical-align: bottom;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
  box-sizing: border-box;
}

.plotlybars-wrapper {
  width: 165px;
  height: 100px;
  margin: 0 auto;
  left: 0;
  right: 0;
  position: absolute;
  z-index: 1;
}

.plotlybars-text {
  color: #447adb;
  font-family: 'Open Sans', verdana, arial, sans-serif;
  font-size: 80%;
  text-align: center;
  margin-top: 5px;
}

.plotlybars-bar {
  background-color: #447adb;
  height: 100%;
  width: 13.3%;
  position: absolute;

  -webkit-transform: translateZ(0);
  transform: translateZ(0);

  animation-duration: 2s;
  animation-iteration-count: infinite;
  animation-direction: normal;
  animation-timing-function: linear;

  -webkit-animation-duration: 2s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-direction: normal;
  -webkit-animation-timing-function: linear;
}

.b1 { left: 0%; top: 88%; animation-name: b1; -webkit-animation-name: b1; }
.b2 { left: 14.3%; top: 76%; animation-name: b2; -webkit-animation-name: b2; }
.b3 { left: 28.6%; top: 16%; animation-name: b3; -webkit-animation-name: b3; }
.b4 { left: 42.9%; top: 40%; animation-name: b4; -webkit-animation-name: b4; }
.b5 { left: 57.2%; top: 26%; animation-name: b5; -webkit-animation-name: b5; }
.b6 { left: 71.5%; top: 67%; animation-name: b6; -webkit-animation-name: b6; }
.b7 { left: 85.8%; top: 89%; animation-name: b7; -webkit-animation-name: b7; }

@keyframes b1 { 0% { top: 88%; } 44% { top: 0%; } 94% { top: 100%; } 100% { top: 88%; } }
@-webkit-keyframes b1 { 0% { top: 88%; } 44% { top: 0%; } 94% { top: 100%; } 100% { top: 88%; } }

@keyframes b2 { 0% { top: 76%; } 38% { top: 0%; } 88% { top: 100%; } 100% { top: 76%; } }
@-webkit-keyframes b2 { 0% { top: 76%; } 38% { top: 0%; } 88% { top: 100%; } 100% { top: 76%; } }

@keyframes b3 { 0% { top: 16%; } 8% { top: 0%; } 58% { top: 100%; } 100% { top: 16%; } }
@-webkit-keyframes b3 { 0% { top: 16%; } 8% { top: 0%; } 58% { top: 100%; } 100% { top: 16%; } }

@keyframes b4 { 0% { top: 40%; } 20% { top: 0%; } 70% { top: 100%; } 100% { top: 40%; } }
@-webkit-keyframes b4 { 0% { top: 40%; } 20% { top: 0%; } 70% { top: 100%; } 100% { top: 40%; } }

@keyframes b5 { 0% { top: 26%; } 13% { top: 0%; } 63% { top: 100%; } 100% { top: 26%; } }
@-webkit-keyframes b5 { 0% { top: 26%; } 13% { top: 0%; } 63% { top: 100%; } 100% { top: 26%; } }

@keyframes b6 { 0% { top: 67%; } 33.5% { top: 0%; } 83% { top: 100%; } 100% { top: 67%; } }
@-webkit-keyframes b6 { 0% { top: 67%; } 33.5% { top: 0%; } 83% { top: 100%; } 100% { top: 67%; } }

@keyframes b7 { 0% { top: 89%; } 44.5% { top: 0%; } 94.5% { top: 100%; } 100% { top: 89%; } }
@-webkit-keyframes b7 { 0% { top: 89%; } 44.5% { top: 0%; } 94.5% { top: 100%; } 100% { top: 89%; } }

然后在 app.R 中:

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

ui <- dashboardPage(
title = "Loading animation test"
  , dashboardHeader(title = "Animated Test")
  , dashboardSidebar()
    ,dashboardBody(
      tags$head(
        tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
      )
    , h1("Plotly Bars (Animated CSS)")
    , div(id = "plot-container"
          , div(class = "plotlybars-wrapper"
            , div( class="plotlybars"
              , div(class="plotlybars-bar b1")
              , div(class="plotlybars-bar b2")
              , div(class="plotlybars-bar b3")
              , div(class="plotlybars-bar b4")
              , div(class="plotlybars-bar b5")
              , div(class="plotlybars-bar b6")
              , div(class="plotlybars-bar b7")
            )
            , div(class="plotlybars-text"
              , p("loading")
            )
          )
          , plotlyOutput("plot")
      )
    )
)

server <- function(input, output) {
  Sys.sleep(10) # just for demo so you can enjoy the animation
  output$plot <- renderPlotly({
    plot_ly(
      x = 2, y = 3, type = "scatter", mode = "markers"
    )
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    【解决方案2】:

    基于上面的代码,我创建了一个 Shiny 模块,该模块根据是否绘制了 Shiny 情节自动显示/隐藏加载动画(例如,如果情节应该只在单击动作按钮后显示,你需要确保加载动画直到那时才显示)。

    该模块在 github 上的https://github.com/andrewsali/plotlyBars 上可用,安装后您也可以直接从那里运行示例。

    加载这个迷你库可以很容易地创建动画闪亮图,只需将 plotlyOutput / renderPlotly 替换为 plotlyBarsUI 并调用模块 plotlyBars。有关如何使用 Shiny 模块的更多信息,请参阅 github 站点上的示例应用程序。

    代码实际上是在创建绘图的反应式启动后显示动画,并在反应式静默失败的情况下隐藏动画(例如,请求或验证停止处理)。

    【讨论】:

      【解决方案3】:

      您可以将 shinycssloaders 库用于 8 种不同类型的加载器。我发现这个包更稳定,更容易实现。

      library(shinycssloaders)
      shinycssloaders::withSpinner(plotlyOutput("plot"), type=2)
      

      包裹:https://cran.r-project.org/web/packages/shinycssloaders/index.html

      演示应用:https://daattali.com/shiny/shinycssloaders-demo/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        相关资源
        最近更新 更多