当源图尚未渲染时,使用plotlyproxy更新shiny中的图

rta7y2nd  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(81)

我想使用plotlyproxy根据输入值更新shiny中的一个图,其中图位于不是第一个选项卡的选项卡中,因此当用户更改输入值时可能尚未呈现。
目前,如果用户在更改输入值之前根本没有单击包含绘图的选项卡,则当他们查看绘图选项卡时,绘图不会更新。即,在绘图开始响应输入值的变化之前,需要查看具有绘图的标签至少一次。
这是一个最小的例子:

library(shiny)
library(plotly)
library(shinyWidgets)
library(data.table)

trace_1 <- rnorm(100, mean = 5)
trace_2 <- rnorm(100, mean = 0)
trace_3 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.table(x, trace_1, trace_2, trace_3)

ui <- fluidPage(
  radioGroupButtons("tracenum", label = "Trace number to change marker colour", choices = 1:3, selected = NULL),
  tabsetPanel(id = "tabs",
              tabPanel("First tab", textOutput("text")),
              tabPanel("Second tab", plotlyOutput("plot"))
  )
)

server <- function(input, output, session) {
 
  output$text <- renderText({
    paste("Trace number", input$tracenum, "has been selected")
  })
 
  output$plot <- renderPlotly({
    fig <- plot_ly(data, x = ~x, y = ~trace_1, name = 'trace 1', type = 'scatter', mode = 'lines+markers')
    fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'lines+markers')
    fig <- fig %>% add_trace(y = ~trace_3, name = 'trace 3', mode = 'lines+markers')
    fig
  })
 
  observeEvent(input$tracenum, {
   
    if (!is.na(input$tracenum)) {
      data[, updatevar := get(paste0("trace_", input$tracenum))]
      plotlyProxy("plot", session) %>%
        plotlyProxyInvoke("addTraces", list(x = c(data$x, data$x),
                                            y = c(data$updatevar, data$updatevar),
                                            type = "scatter", mode = "markers",
                                            marker = list(color = "black")))
    }
   
  })
   
}

shinyApp(ui, server)

如果用户在他们第一次查看第一选项卡时选择更新轨迹2(即,他们还没有点击第二个标签在所有和情节还没有呈现),我希望该选择有一个效果,当他们点击第二个标签,其中包含情节。

fjnneemd

fjnneemd1#

我们需要通过outputOptions()设置suspendWhenHidden = FALSE

library(shiny)
library(plotly)
library(shinyWidgets)
library(data.table)

trace_1 <- rnorm(100, mean = 5)
trace_2 <- rnorm(100, mean = 0)
trace_3 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.table(x, trace_1, trace_2, trace_3)

ui <- fluidPage(
  radioGroupButtons("tracenum", label = "Trace number to change marker colour", choices = 1:3, selected = NULL),
  tabsetPanel(id = "tabs",
              tabPanel("First tab", textOutput("text")),
              tabPanel("Second tab", plotlyOutput("plot"))
  )
)

server <- function(input, output, session) {
  
  output$text <- renderText({
    paste("Trace number", input$tracenum, "has been selected")
  })
  
  output$plot <- renderPlotly({
    fig <- plot_ly(data, x = ~x, y = ~trace_1, name = 'trace 1', type = 'scatter', mode = 'lines+markers')
    fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'lines+markers')
    fig <- fig %>% add_trace(y = ~trace_3, name = 'trace 3', mode = 'lines+markers')
    fig
  })
  
  outputOptions(output, "plot", suspendWhenHidden = FALSE)
  
  observeEvent(input$tracenum, {
    if (!is.na(input$tracenum)) {
      data[, updatevar := get(paste0("trace_", input$tracenum))]
      plotlyProxy("plot", session) %>%
        plotlyProxyInvoke("addTraces", list(x = c(data$x, data$x),
                                            y = c(data$updatevar, data$updatevar),
                                            type = "scatter", mode = "markers",
                                            marker = list(color = "black")))
    }
    
  })
  
}

shinyApp(ui, server)

相关问题