【问题标题】:R Shiny: relayout plotly annotationsR Shiny:重新布局情节注释
【发布时间】:2018-08-01 19:08:04
【问题描述】:

如果用户单击闪亮应用程序中的按钮,我想要一个情节图来更改注释。 我不知道为什么这不起作用:

library(shiny)
library(plotly)

d <- data.frame(x = c(1,2,3), y = c(9,99,999))


ui <- fluidPage(
  plotlyOutput("plot"),

actionButton("button", "toggle visibility"))

server <- function(input, output) {

output$plot <- renderPlotly({

plot_ly(d)%>%
  add_lines(y=d$y, x= d$x)%>%
  layout(annotations = list(x = 2, y= 99 , text = "hi"))})

  observeEvent(input$button, {
    plotlyProxy("plot", session= shiny::getDefaultReactiveDomain()) %>%
      plotlyProxyInvoke("relayout", list(annotations= list(x = 2, y= 99 , 
text = "ho")))})}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny annotations plotly


    【解决方案1】:

    这不是在plotly 中使用relayout 的方式。请参阅下面使用 relayout 的示例。

    我更喜欢为此目的使用原生闪亮按钮,因为它提供了更大的灵活性。以下是实现 hi-ho 切换的方法。

    shiny方式

    library(shiny)
    library(plotly)
    
    d <- data.frame(x = c(1,2,3), y = c(9,99,999))
    
    
    ui <- fluidPage(
      plotlyOutput("plot"),
      
      actionButton("button", "toggle visibility"))
    
    server <- function(input, output) {
      
      output$plot <- renderPlotly({
        p <- plot_ly(d)%>%
          add_lines(y=d$y, x= d$x)
        if (is.null(input$button) | (input$button%%2 == 0)) {
          p <- p %>% layout(annotations = list(x = 2, y= 99 , text = "hi"))
        } else {
          p <- p %>% layout(annotations = list(x = 2, y= 99 , text = "ho"))
        }
        p
      })
    }
    
    shinyApp(ui, server)
    

    不过,在这种情况下,让relayout 功能工作很简单,尽管它确实需要一个额外的按钮。

    plotlyrelayout方式

    library(shiny)
    library(plotly)
    
    d <- data.frame(x = c(1,2,3), y = c(9,99,999))
    
    
    ui <- fluidPage(
      plotlyOutput("plot")
    )
    
    server <- function(input, output) {
      output$plot <- renderPlotly({
        updatemenus <- list(
          list(
            active = -1,
            type = 'buttons',
            buttons = list(
              list(
                label = "hi",
                method = "relayout",
                args = list(list(annotations = list(list(x = 2, y= 99 , text = "hi"))))), 
              list(
                label = "ho",
                method = "relayout",
                args = list(list(annotations = list(list(x = 2, y= 99 , text = "ho")))))
              )
          )
        )
        p <- plot_ly(d) %>%
          add_lines(y=d$y, x= d$x) %>% 
          layout(updatemenus = updatemenus)
        p
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢,不幸的是,这不是我的解决方案。我想要使​​用 plotly 重新布局的闪亮 UI 输入元素,因为我的情节很大并且重新布局更快......这就是我使用 plotlyProxy 的原因
    • @PascalIv。我已经包含了这两种解决方案,下面是relayout 方式。
    【解决方案2】:

    我相信所有需要更改代码以使其正常工作的方法是在您的 plotly 代理重新布局代码中围绕已定义的注释列表包装另一个列表。我最近发现这个递归列表结构是使用重新布局操作注释所需要的全部 - 您可以查看我对与同一问题相关的另一个 SO 问题的回答,但上下文略有不同:https://stackoverflow.com/a/70610374/17852464

        library(shiny)
        library(plotly)
        
        d <- data.frame(x = c(1,2,3), y = c(9,99,999))
        
        ui <- fluidPage(
          plotlyOutput("plot"),
          
          actionButton("button", "toggle visibility"))
        
        server <- function(input, output) {
        
          output$plot <- renderPlotly({
            plot_ly(d)%>%
              add_lines(y=d$y, x= d$x)%>%
              layout(annotations = list(x = 2, y= 99 , text = "hi"))
            })
          
          observeEvent(input$button, {
            plotlyProxy("plot", session= shiny::getDefaultReactiveDomain()) %>%
              plotlyProxyInvoke("relayout", list(annotations= list(list(x = 2, y= 99 , 
                                                                   text = "ho"))))})}
          
        }
        
        shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-12
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 2023-04-04
      • 2019-11-22
      相关资源
      最近更新 更多