【问题标题】:Plotly and Shiny: onclick switch tabs to created plotPlotly 和 Shiny:onclick 切换选项卡以创建情节
【发布时间】:2019-12-02 20:22:54
【问题描述】:

我创建了一个简单的绘图,当用户单击一个点时,它会在第二个选项卡上生成另一个绘图,Page_2 - 是否可以添加一些自定义 JS,以便当用户单击该点时它们会自动重新路由到 Page_2 标签页?

library(shiny)
library(plotly)
library(tidyverse)

# ui with two panes
# when you click on the outlier in the first plot
# you are routed to the second "point explorer" page
ui <- navbarPage("Plotly On Click Switch Pane",
                 tabPanel("Page_1",
                          mainPanel(plotlyOutput("plot"),
                                    tableOutput("text"))),
                 tabPanel("Page_2",
                          mainPanel(plotlyOutput("ind_plot"))
                  ))


server <- function(input, output) {

  # plot on first page
   output$plot <- renderPlotly({
     ggplotly(source = "sub_iris",
       ggplot(iris, aes(x = Species, y = Petal.Width)) +
         geom_boxplot()
     )
   })

   # create reactive for subset plot on second tab
   s <- reactive({ event_data("plotly_click", source = "sub_iris") })

   # plot text on first page (test)
   output$text <- renderTable(event_data("plotly_click", source = "sub_iris"))

   # this is the correct plot, but I want to re-route the user here when they click on a point
   output$ind_plot <- renderPlotly({
     iris_ind <- subset(iris)[subset(s(), curveNumber == 0)$pointNumber + 1,]
       ggplotly(
         ggplot(iris_ind, aes(x = Species, y = Sepal.Length)) +
         geom_bar(stat = "identity")
       )
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: shiny plotly shiny-reactivity ggplotly


    【解决方案1】:

    在为您的navbarPage 提供id 后,您可以使用updateNavbarPage

    library(shiny)
    library(plotly)
    library(tidyverse)
    
    # ui with two panes
    # when you click on the outlier in the first plot
    # you are routed to the second "point explorer" page
    ui <- navbarPage("Plotly On Click Switch Pane", id = "navbarID",
                     tabPanel("Page_1",
                              mainPanel(plotlyOutput("plot"),
                                        tableOutput("text"))),
                     tabPanel("Page_2",
                              mainPanel(plotlyOutput("ind_plot"))
                     ))
    
    
    server <- function(input, output, session) {
    
      # plot on first page
      output$plot <- renderPlotly({
        ggplotly(source = "sub_iris",
                 ggplot(iris, aes(x = Species, y = Petal.Width)) +
                   geom_boxplot()
        )
      })
    
      # create reactive for subset plot on second tab
      s <- reactive({
        event_data("plotly_click", source = "sub_iris")
        })
    
      observeEvent(s(), {
        updateNavbarPage(session, inputId = "navbarID", selected = "Page_2")
      })
    
      # plot text on first page (test)
      output$text <- renderTable(req(s()))
    
      # this is the correct plot, but I want to re-route the user here when they click on a point
      output$ind_plot <- renderPlotly({
        req(s())
        iris_ind <- subset(iris)[subset(s(), curveNumber == 0)$pointNumber + 1,]
        ggplotly(
          ggplot(iris_ind, aes(x = Species, y = Sepal.Length)) +
            geom_bar(stat = "identity")
        )
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      相关资源
      最近更新 更多