1

我有一个带有两个情节输出的应用程序。我想使用串扰将它们组合起来。但是,当我运行该应用程序时,每当我单击其中一个图中的条时,只有一半的条被突出显示。为什么?

library(shiny)
library(crosstalk)
library(plotly)
require(dplyr)

df <- tibble(names = LETTERS[1:5], x = runif(5,10,20), y = runif(5,50,100))
# df <- data.frame(names = LETTERS[1:5], x = runif(5,10,20), y = runif(5,50,100))

ui <- fluidPage(
  fluidRow(
    column(4, plotlyOutput("outp1")),
    column(4, plotlyOutput("outp2"))
  )
)

server <- function(input, output, session) {
  
  hd <- highlight_key(df, ~names)
  
  output$outp1 <- renderPlotly({
    plot_ly(
      hd,
      x = ~names,
      y = ~x,
      type = "bar",
      source = "p1"
    ) %>% highlight(on = "plotly_click", off = "plotly_doubleclick")
  })
  
  output$outp2 <- renderPlotly({
    plot_ly(
      hd,
      x = ~names,
      y = ~y,
      type = "bar",
      source = "p2"
    ) %>% highlight(on = "plotly_click", off = "plotly_doubleclick")
  })
  
}

shinyApp(ui, server)
4

1 回答 1

0

据此添加layout(barmode = "overlay")到情节对象似乎可以解决问题。

server <- function(input, output, session) {
  
  hd <- highlight_key(df, ~names)
  
  output$outp1 <- renderPlotly({
    plot_ly(
      hd,
      x = ~names,
      y = ~x,
      type = "bar"
    ) %>% layout(barmode = "overlay") %>% highlight(on = "plotly_click", off = "plotly_doubleclick")
  })
  
  output$outp2 <- renderPlotly({
    plot_ly(
      hd,
      x = ~names,
      y = ~y,
      type = "bar"
    ) %>% layout(barmode = "overlay") %>% highlight(on = "plotly_click", off = "plotly_doubleclick")
  })
  
}
于 2021-10-08T14:19:31.317 回答