【问题标题】:Interactive hovering Shiny graphs交互式悬停闪亮图表
【发布时间】:2015-08-27 14:22:31
【问题描述】:

是否可以创建一个交互式闪亮图表,当您将鼠标悬停在图表上时,会出现后续图表?现在我在同一个面板中有两个图表。

这是我能找到的最接近的逻辑example,尽管后续(缩放)图已经存在(而我只希望在鼠标悬停在主图上时出现后续图):

【问题讨论】:

    标签: r shiny interactive shinydashboard


    【解决方案1】:

    您可以在ui.RplotOutput 中使用hover 选项,并将其与条件面板结合使用,以便仅在用户将鼠标悬停在第一个图上时显示第二个图。

    我通过这些更改修改了您发布的示例:

    library(ggplot2)
    library(Cairo)   # For nicer ggplot2 output when deployed on Linux
    library(shiny)
    
    ui <- fluidPage(
            fluidRow(
                    column(width = 8, class = "well",
                           h4("Left plot controls right plot"),
                           fluidRow(
                                   column(width = 6,
                                          plotOutput("plot2", height = 300,
                                                     brush = brushOpts(
                                                             id = "plot2_brush",
                                                             resetOnNew = TRUE
                                                     ),
                                                     #add the hover options
                                                     hover = hoverOpts(
                                                             id = "plot2_hover",
                                                             nullOutside = TRUE
                                                     )
                                          )
                                   ),
                                   column(width = 6,
                                          #the second plot will be hidden if the user's mouse is not on the first one
                                          conditionalPanel(
                                                  condition = "input.plot2_hover != null",
                                                     plotOutput("plot3", height = 300)
                                          )
                                   )
                           )
                    )
    
            )
    )
    
    server <- function(input, output) {
            ranges2 <- reactiveValues(x = NULL, y = NULL)
    
            output$plot2 <- renderPlot({
                    ggplot(mtcars, aes(wt, mpg)) +
                            geom_point()
            })
    
            output$plot3 <- renderPlot({
                    ggplot(mtcars, aes(wt, mpg)) +
                            geom_point() +
                            coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
            })
    
            # When a double-click happens, check if there's a brush on the plot.
            # If so, zoom to the brush bounds; if not, reset the zoom.
            observe({
                    brush <- input$plot2_brush
                    print(input$plot2_hover)
                    if (!is.null(brush)) {
                            ranges2$x <- c(brush$xmin, brush$xmax)
                            ranges2$y <- c(brush$ymin, brush$ymax)
    
                    } else {
                            ranges2$x <- NULL
                            ranges2$y <- NULL
                    }
            })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2021-10-14
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多