R Flink 错误:如何处理两个情节的点击事件?

scyqe7ek  于 2023-05-11  发布在  Flink
关注(0)|答案(3)|浏览(92)

我在一个闪亮的 Jmeter 板上有两个plotly情节。当我点击第一个plotly情节,互动事件是工作正常。但是当我对第二个图(堆叠条形图)执行相同的操作时,窗口将自动关闭。
你有没有遇到过闪亮的 Jmeter 板与一个以上的plotly情节?如果是,如何处理不同地块上的点击事件?

  • 我正在准备可重复的用例。我很快就会发布的 *
library(shinydashboard)
library(plotly)
library(shiny)
library(dplyr)
library(ggplot2)

tg <- ToothGrowth
tg$dose <- factor(tg$dose)

skin <- Sys.getenv("DASHBOARD_SKIN")
skin <- tolower(skin)
if (skin == "")
  skin <- "blue"

sidebar <- dashboardSidebar(
  sidebarSearchForm(label = "Search...", "searchText", "searchButton"),
  sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
  )
)

body <- dashboardBody(
  tabItems(
    tabItem("dashboard",
            fluidRow(
              box(
                title = "Distribution",
                status = "primary",
                plotlyOutput("plot1", height = "auto"),
                height = 500,
                width = 7
              ),
              box(
                height = 500, width = 5,
                title = "Dist",
                         plotlyOutput("click", height = 430)

              )
            )
  )
))

header <- dashboardHeader(
  title = "My Dashboard"
)

ui <- dashboardPage(header, sidebar, body, skin = skin)

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

  output$plot1 <- renderPlotly({

    p <- ggplot(data = tg, aes(x=len, y=dose, col=supp, key=supp)) + geom_point()
    ggplotly(p)

  })

  output$click <- renderPlotly({
    d <- event_data("plotly_click")
    if (is.null(d)){
      "Click events appear here (double-click to clear)"
    } else {

      gSel <- tg %>% filter(dose %in% d$y) %>% group_by(supp) %>%  mutate(newLen=floor(len)) %>% 
        ggplot(aes(x=supp, fill=as.factor(newLen))) + geom_bar()
      ggplotly(gSel)

    }
  })

}

shinyApp(ui, server)

上面的代码产生:

**如何避免上图中的可用错误?**打印输出区域中的文本打印。

第一个图用于迭代点击事件。当我单击y=1上的一个点时,它会生成第二个图

但是当我点击堆叠栏时,第二个图就不见了(在我最初的场景中,窗口关闭并且不可见。要使用该应用程序,我需要重新运行该应用程序)。

如何接收点击事件并检查它是来自第一个图还是第二个图?

bakd9h0s

bakd9h0s1#

我也使用了plotly_click事件,方法是在图中添加一个源参数

p <- plot_ly(source = paste('plotlyplot', plot.list, sep = ''))

观察点击事件并在那里分配数据

observeEvent(event_data("plotly_click", source = "plot1"), { 
     values$plot.click.results <- event_data("plotly_click", source = "plot1") 
})

对于基于第一个图中的单击事件渲染第二个图的场景:如果你试图在点击事件数据为零时绘制一个图,并且你在你的例子中试图绘制一条文本消息,那么R不能用文本绘制一个图是有道理的。而是以这样的方式构建它:如果单击事件数据为NULL,则输出为renderText,如果不是NULL,则输出renderPlotly

pbwdgjma

pbwdgjma2#

只是为了错误抑制问题:-在你的ui部分输入这个

tags$style(type="text/css",
         ".shiny-output-error { visibility: hidden; }",
         ".shiny-output-error:before { visibility: hidden; }"


关于图的问题。我也有

yk9xbfzb

yk9xbfzb3#

这是一个例子:

library(shiny)
library(plotly)

ui <- fluidPage(
  fluidRow(
    column(width = 6, plotlyOutput("plot1")),
    column(width = 6, plotlyOutput("plot2"))
  ),
  fluidRow(
    column(width = 6, verbatimTextOutput("selected")),
    column(width = 6, verbatimTextOutput("selected2"))
  )
)

server <- function(input, output, session) {
  nms <- row.names(mtcars)
  output$plot1 <- renderPlotly({
    plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms
            ,source = "plot1")%>% 
      layout(dragmode = "select") %>%
      event_register("plotly_selecting")
  })
  
  output$plot2 <- renderPlotly({
    plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms,source = "plot2")%>% 
      layout(dragmode = "select") %>%
      event_register("plotly_selecting")
  })
  output$selected <- renderPrint({
    d <- event_data("plotly_selected",source = "plot1")
    if (is.null(d)) "Brushed points appear here (double-click to clear)" else d
  })
  output$selected2 <- renderPrint({
    d <- event_data("plotly_selected", source = "plot2")
    if (is.null(d)) "Brushed points appear here (double-click to clear)" else d
  })
}
shinyApp(ui, server, options = list(display.mode = "showcase"))

相关问题