2017-01-23 3 views
1

ポイントの色がマウスポインタ(ホバー)に依存する、Rmarkdownドキュメントに光沢のあるプロットを作成したいと思います。しかし、入力リストのホバー要素が再びNULLに設定されるまで、必要なプロットは数秒で表示されます。どうしてこれなの?Rのホバーに基づくポイントカラーshinyとRmarkdown

例:あなたは、プロットの色の部分を削除した場合

--- 
title: "Untitled" 
runtime: shiny 
output: html_document 
--- 

```{r,echo=FALSE} 
library(ggplot2) 
x <- rnorm(100) 
y <- rnorm(100) 

dfr <- data.frame(x, y) 

give_col <- function(){ 
    if(is.null(input$hover$y)){ 
    rep(2, length(x)) 
    }else{ 
     (input$hover$y < dfr$y) + 1 
    }} 

imageOutput("image_id", hover = "hover") 
textOutput("text_id") 

output$image_id <- renderPlot({ 
    plot(dfr$x, dfr$y, col = factor(give_col())) 
    # plot(dfr$x, dfr$y) # Without col the give_col() element remains 
         # as can be seen in the output text 
}) 
output$text_id <- reactive({paste(give_col())}) 
``` 

、テキスト出力が期待通りに動作するので、私はそれがpch代わりのcolかとのために(同じ自体プロットで何かでなければならないと思いますplot()の代わりにggplot())。

ご協力いただければ幸いです。

答えて

2

新しい色でプロットすると、色を再初期化するホバーイベントが送信されるため、コードが機能しません。

あなたは、イベントが表示されたときに値を格納するためにobserveEventと一緒reactiveValueを使用することができます。

--- 
title: "Untitled" 
runtime: shiny 
output: html_document 
--- 

```{r,echo=FALSE} 
library(ggplot2) 
x <- rnorm(100) 
y <- rnorm(100) 

dfr <- data.frame(x, y) 
give <- reactiveValues(col=rep(2, length(x))) 
observeEvent(input$hover,{give$col <- (input$hover$y < dfr$y) + 1}) 

imageOutput("image_id", hover = "hover") 
textOutput("text_id") 

output$image_id <- renderPlot({ 
    plot(dfr$x, dfr$y, col = factor(give$col)) 
    # plot(dfr$x, dfr$y) # Without col the give_col() element remains 
         # as can be seen in the output text 
}) 
output$text_id <- reactive({paste(give$col)}) 
``` 
+0

おかげで、素早く簡単かつへのポイントの答えのために! – user1965813

+0

なぜ 'input $ hover'を変更する再プロットが' observeEvent'を引き起こさないのでしょうか? (これは、 'observeEvent'が単純な無効化とは異なる動作をすることを意味します) 答えが見つかりました[ここ](https://shiny.rstudio.com/reference/shiny/latest/observeEvent.html) – user1965813

関連する問題