2017-08-22 46 views
0

我在闪亮的仪表板中有两个剧情阴谋。当我点击第一个情节图时,交互式事件工作正常。但是当我在第二块图上进行同样的操作时,这个窗口会自动关闭。R闪亮的错误:如何处理两个剧情图的点击事件?

您是否遇到过带有多个阴谋情节的闪亮仪表盘?如果是的话,如何处理不同情节剧情的点击事件?


我准备重复使用的用例。很快我会发布它。

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) 

以上代码生成: enter image description here

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

第一个图用于迭代点击事件。当我点击y=1一个点,它产生的第二个情节 enter image description here

但是当我点击堆叠栏上,第二个情节变得缺少 (我在原来的方案中,窗口关闭,不可见的。要使用应用程序,我需要重新运行应用程序)。 enter image description here

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

回答

0

我用plotly_click事件来说,要做到的方式,它是一个soucre参数添加到该地块

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