简体   繁体   English

R Shiny:重新布局绘图注释

[英]R Shiny: relayout plotly annotations

I want a plotly plot to change an annotation if the user clicks a button in a shiny app.如果用户单击闪亮的应用程序中的按钮,我想要一个情节图来更改注释。 I have no idea why this does not work:我不知道为什么这不起作用:

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)

That is not the way to use relayout in plotly . 这是不使用的方式relayoutplotly See below for your example using relayout . 请参见下面的有关使用relayout的示例。

I prefer using native shiny buttons for this purpose because of the greater flexibility it offers. 我更喜欢为此使用本机闪亮按钮,因为它提供了更大的灵活性。 Here is how one might go about achieving the hi-ho toggle. 这是实现踩-切换的方式。

shiny way 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)

In this case though, it is simple enough to make the relayout feature work, although it does require an extra button. 在这种情况下,尽管需要一个额外的按钮,但使relayout功能正常工作很简单。

plotly relayout way plotly relayout

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)

I believe all that needs to change in your code in order to get this to work is wrapping another list around the defined annotation list in your plotly proxy relayout code.我相信所有需要在您的代码中进行更改才能使其工作的是在您的 plotly 代理重新布局代码中围绕定义的注释列表包装另一个列表。 I recently discovered that this recursive list structure is all that's needed in order to manipulate annotations using relayout - you can check out my answer to another SO question related to the same issue, but with slightly different context: https://stackoverflow.com/a/70610374/17852464我最近发现这个递归列表结构是使用重新布局操作注释所需要的全部 - 您可以查看我对与同一问题相关的另一个 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM