R 如何在绘图上保留闪亮的点击输入?

R 如何在绘图上保留闪亮的点击输入?,r,ggplot2,shiny,shiny-server,R,Ggplot2,Shiny,Shiny Server,我在R的闪亮包装中遇到了一个问题。我想在点击一个变量的基础上给某些变量上色。下面是一个相当做作的例子,使用mtcars根据圆柱体高亮显示。最初的着色效果很好,但很快就消失了 服务器端: #server. R library(shiny) library(ggplot2) library(stringr) library(dplyr) library(datasets) data(mtcars) function(input, output) { output$plot <- reac

我在R的闪亮包装中遇到了一个问题。我想在点击一个变量的基础上给某些变量上色。下面是一个相当做作的例子,使用mtcars根据圆柱体高亮显示。最初的着色效果很好,但很快就消失了

服务器端:

#server. R
library(shiny)
library(ggplot2)
library(stringr)
library(dplyr)
library(datasets)
data(mtcars)

function(input, output)
{


output$plot <- reactivePlot(function()
{
 if (is.null(input$clicker) == TRUE){
  graph <- ggplot()+
    theme_bw()+
    geom_point(data = mtcars, 
               aes(x = disp, y = cyl))+
    guides(color=FALSE)+
    theme(legend.position = "none")+
    labs(x = "Displacement",
         y = "Cylinders")
  return(graph)
}
else{
  x <-reactive({ 
    x <- nearPoints(mtcars, input$clicker, threshold = 10, maxpoints = 1,
                    addDist = TRUE, xvar = "disp", yvar = "cyl")[1,2]
  })

  print(x())

  mtcars$colour <- NA
  myval <- x()
  mtcars$colour[which(mtcars$cyl == myval)] <- "var"
  plot <- ggplot()+
    theme_bw()+
    geom_point(data = mtcars[mtcars$cyl != myval,], 
               aes(x = disp, y = cyl, color = colour))+
    geom_point(data = mtcars[mtcars$cyl == myval,], 
               aes(x = disp, y = cyl, color = colour), size = 4)+
    guides(color=FALSE)+
    theme(legend.position = "none")+
    labs(x = "Displacement",
         y = "Cylinders")
  return(plot)
}
})
 }

单击选择中是否有我遗漏的变量以保留该变量?我已经在示例中使用了它,它还可以,所以我猜这与实际的绘图渲染有关?为愚蠢的问题提前道歉。

u应该将它们存储在
reactiveValue()
中。有关示例,请参见此处:@BigDataScientist谢谢您。我一直在拼凑示例,所以假设这是我的一个笨拙之处。不,代码看起来不错。“它已经回答了你的问题吗?遗憾的是没有,”BigDataScientist(或者至少我是如何回答的)。同样的情况也会发生,我单击一个变量,它会高亮显示,然后在大约一秒钟后返回默认值。不管怎样,谢谢你。编辑并共享你的代码,然后我们可以看一看
shinyUI(fluidPage(

 sidebarLayout(position = "right",
            sidebarPanel(
              h3("Instructions"),
              helpText("Lorem Ipsum.")
              ),
            mainPanel(
              plotOutput('plot', click = "clicker")
            )
)
)
)