2017-12-08 8 views
0

私はプロットプロットからclickイベントをキャプチャする、光沢のあるインタラクティブに表示されたテキストを持っています。何もクリックされなければ、デフォルトのテキストが表示され、ポイントがクリックされると、対応する値が表示されます。光沢のあるラジオボタンの選択時にイベントデータをプロットする

しかし、プロットに描かれているものを選択するラジオボタンもあります。問題は、選択したラジオボタンを変更すると、対話的に表示されるテキストが正しく表示されないことです。これは、プロットが変更され、キャプチャされないためです。 したがって、ラジオボタンで別のオプションを選択するたびに、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) 

答えて

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) 
関連する問題