我想在 Rmarkdown 文档中创建一个闪亮的图,其中点的颜色取决于鼠标指针(悬停)。NULL
但是,在输入列表的悬停元素再次设置为之前,所需的绘图仅出现几分之一秒。为什么是这样?
例子:
---
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
或ggplot()
代替代替相同plot()
)。
任何帮助,将不胜感激。