plotlyOutput显示在Shiny Apps中刷新之前的前一个图

时间:2018-07-24 06:41:37

标签: shiny r-plotly

我已经使用Plotly热图创建了一个小的Shiny应用程序,并打算根据用户的输入使用withSpinner绘制热图。目前我有两个问题。

a。)WithSpinner仅在生成热图时第一次出现。如果更改了用户输入并重新绘制,则不会出现。

b。)在更改用户输入时,将显示以前的热图而不是微调器,并在一段时间后刷新。我打算在重新绘制热图时使用微调器而不是显示旧图。

library(shiny)
library(shinydashboard)
library(shinycssloaders)
library(shinyjs)
library(plotly)

ui <- shinydashboard::dashboardPage(
  # Dashboard header
  shinydashboard::dashboardHeader(),

  # Dashboard sidebar
  shinydashboard::dashboardSidebar(disable = TRUE),
  # Dashboard body
  shinydashboard::dashboardBody(
    id = "myBody",
    # Tab items
    shinydashboard::tabItem(tabName = "visual",
                            fluidRow(
                              shinydashboard::tabBox(id="tabBix1",
                                                     shiny::tabPanel(
                                                       "Parameters & Settings",
                                                       value = "paramsetting",
                                                       sliderInput("obs", "Number of observations:",
                                                                   min = 0, max = 1000, value = 500
                                                       )),
                                                     shiny::tabPanel(
                                                       "PlotOutput",
                                                       value = "Ops",
                                                       withSpinner(plotly::plotlyOutput("plotNewExp"))
                                                       )
                                                     )
                            ))
  )
)

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

  p <- NULL
  observeEvent(input$obs,{
    p <- NULL
    m <- matrix(rnorm(input$obs), nrow = 3, ncol = 3)
    output$plotNewExp <- plotly::renderPlotly({
      p <- plot_ly(
        x = c("a", "b", "c"), y = c("d", "e", "f"),
        z = m, type = "heatmap"
      )
    })
  })
}

shinyApp(ui=ui,server=server)

1 个答案:

答案 0 :(得分:0)

If you try this you will see that the spinner is working but it is fast. So you probably don't have the time to see it when you switch from one tab to the other.

ui <- shinydashboard::dashboardPage(
  # Dashboard header
  shinydashboard::dashboardHeader(),

  # Dashboard sidebar
  shinydashboard::dashboardSidebar(disable = TRUE),
  # Dashboard body
  shinydashboard::dashboardBody(
    id = "myBody",
    # Tab items
    shinydashboard::tabItem(tabName = "visual",
                            fluidRow(
                              shinydashboard::tabBox(
                                                     tabPanel(
                                                       "Parameters & Settings",
                                                       value = "paramsetting",
                                                       sliderInput("obs", "Number of observations:",
                                                                   min = 0, max = 1000, value = 500
                                                       ),
                                                       withSpinner(plotly::plotlyOutput("plotNewExp")))
                                                   #  tabPanel(
                                                    #   "PlotOutput",
                                                     #  withSpinner(plotly::plotlyOutput("plotNewExp"))
                                                    # )
                              )
                            ))
  )
)

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

 # p <- NULL
 # observeEvent(input$obs,{
 #   p <- NULL
    m <- reactive({matrix(rnorm(input$obs), nrow = 3, ncol = 3)})


    output$plotNewExp <- renderPlotly({
      p <- plot_ly(
        x = c("a", "b", "c"), y = c("d", "e", "f"),
        z = m(), type = "heatmap"
      )
  #  })
  })
}

shinyApp(ui=ui,server=server)

or if you add a delay, you will see it is working.

library(shinyjs)
ui <- shinydashboard::dashboardPage(
  # Dashboard header
  shinydashboard::dashboardHeader(),

  # Dashboard sidebar
  shinydashboard::dashboardSidebar(disable = TRUE),
  # Dashboard body
  shinydashboard::dashboardBody(
    useShinyjs(),
    id = "myBody",
    # Tab items
    shinydashboard::tabItem(tabName = "visual",
                            fluidRow(
                              shinydashboard::tabBox(id="tabBix1",
                                                     shiny::tabPanel(
                                                       "Parameters & Settings",
                                                       value = "paramsetting",
                                                       sliderInput("obs", "Number of observations:",
                                                                   min = 0, max = 1000, value = 500
                                                       )),
                                                     shiny::tabPanel(
                                                       "PlotOutput",
                                                       value = "Ops",
                                                       withSpinner(plotly::plotlyOutput("plotNewExp"))
                                                     )
                              )
                            ))
  )
)

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

 # p <- NULL

  Graph <- function() {

    p <- NULL
    m <- matrix(rnorm(input$obs), nrow = 3, ncol = 3)
    output$plotNewExp <- plotly::renderPlotly({
      p <- plot_ly(
        x = c("a", "b", "c"), y = c("d", "e", "f"),
        z = m, type = "heatmap"
      )
    })
  }

  observeEvent(input$obs,{
delay(4000, Graph())
  })
}

shinyApp(ui=ui,server=server)