【问题标题】:Resetting plotly event_data upon radio button select in shiny在闪亮的单选按钮选择时重置 plotly event_data
【发布时间】:2018-05-22 22:08:16
【问题描述】:

我有一个交互式显示的闪亮文本,它从情节图中捕获点击事件。如果没有点击,则会显示默认文本,一旦点击某个点,就会显示其对应的值。

但是,我也有一个单选按钮来选择图中所描绘的内容。问题是,当我更改选定的单选按钮时,交互式显示的文本不再正确,因为情节发生了变化并且没有被捕获,如下面的简化示例所示。 因此,每当我在单选按钮中选择不同的选项时,我都希望重置 event_data(并因此显示默认文本)。

我知道有一些方法可以创建一个单独的“重置”按钮(例如,使用 shinyjs 包,请参阅 here),但我想知道是否有可能以某种方式将此重置功能与单选按钮耦合。

library(ggplot2)      
library(shiny)          
library(shinydashboard) 
library(plotly)     

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(plotlyOutput("first"),
          radioButtons("radbut", "Choose:", c("Sepal" = "sepal","Petal" = 
"petal")) 
      ),
      box(textOutput("second"))
    )
  )
)

server <- function(input, output, session) {
  output$first <- renderPlotly({
    if (input$radbut == "sepal") {
      gp <- ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) +
        geom_point()
    } else {
      gp <- ggplot(data = iris, aes(x = Petal.Width, y = Petal.Length)) +
        geom_point()  
    }
    ggplotly(gp, source = "select")
  })
  output$second <- renderText({
    clicked <- event_data("plotly_click", source = "select")
    if (is.null(clicked)) {
      text = "Select a value"
    } else {
      text = paste("You clicked:", input$radbut, 
                   clicked[[3]],",", clicked[[4]], sep = " ")
    }
    text
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny plotly shinyjs


    【解决方案1】:

    这是使用反应值的可能解决方案(解决方法)。我认为必须有一个更优雅的解决方案,但找不到它。

    希望这会有所帮助!

        library(ggplot2)      
        library(shiny)          
        library(shinydashboard) 
        library(plotly)     
    
        ui <- dashboardPage(
                dashboardHeader(),
                dashboardSidebar(),
                dashboardBody(
                        fluidRow(
                                box(plotlyOutput("first"),
                                    radioButtons("radbut", "Choose:", c("Sepal" = "sepal","Petal" = 
                                                                                "petal")) 
                                ),
                                box(textOutput("second"))
                        )
                )
        )
    
        server <- function(input, output, session) {
                defaults <- reactiveValues(part = "Sepal", text = "")
                output$first <- renderPlotly({
                        defaults$text = "Select a value"
    
                        if (input$radbut == "sepal") {
                                defaults$part = "Sepal"
    
                                gp <- ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) +
                                        geom_point()
                        } else {
                                defaults$part = "Petal"
                                gp <- ggplot(data = iris, aes(x = Petal.Width, y = Petal.Length)) +
                                        geom_point()  
                        }
                        ggplotly(gp, source = "select")
                })
                clicked <- reactive({
                        event_data("plotly_click", source = "select")
                })
                observeEvent(clicked(), {
                        defaults$text = paste("You clicked:", defaults$part, 
                                     clicked()[[3]],",", clicked()[[4]], sep = " ")
                })
                output$second <- renderText({
                        defaults$text
                })
        }
    
        shinyApp(ui, server)
    

    【讨论】:

    • 我之前观察到这样的点击:observe({barSource 图表数据出现任何变化。clicked 和 observeEvent(clicked(), { 的组合解决了这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2015-09-19
    • 2020-06-20
    • 2021-01-26
    相关资源
    最近更新 更多