【问题标题】:nearPoints gets confused by .data: xvar not in names of inputnearPoints 被 .data 弄糊涂了:xvar 不在输入名称中
【发布时间】:2021-05-17 09:49:54
【问题描述】:

以下代码不起作用:

library(ggplot2)
library(shiny)
shinyApp(ui=fluidPage(
            plotOutput("plot", click="clicked"),
            tableOutput("near")),
         server=function(input, output, session) {
           df <- data.frame(x=rnorm(100), y=rnorm(100))
           output$plot <- renderPlot({
             ggplot(df, aes(x=.data$x, y=.data$y)) + geom_point()
           })

           output$near <- renderTable({
             nearPoints(df, input$clicked)
           })    
         })

当点击绘图上的一个点时,会出现以下错误:

nearPoints: `xvar` ('.data$x')  not in names of input

如果改为上面的ggplot语句,则使用下面的语句

ggplot(df, aes(x=x, y=y)) + geom_point()

一切都按预期进行。

我之所以在aes() 中使用data$x(或data[["x"]],也会报错)是因为这段代码在一个包内,而data[["x"]] 用于消除R 包检查警告(如programming with dplyr 中所述)。我可以在绘图函数中而不是在我正在处理的闪亮应用程序中规避问题(例如,通过定义虚拟变量 x 和 y),但首先,这并不优雅,其次,如果有一天我有一个绘图函数怎么办来自另一个使用类似技巧的包?

我的问题:如何在不修改 ggplot 语句的情况下使上述代码在闪亮的中工作?

【问题讨论】:

  • 你不能用aes_string(x = "x", y = "y")吗?否则也可以在nearPoints中指定xvaryvar,即nearPoints(df, input$clicked, xvar="x", yvar="y")
  • @StéphaneLaurent 啊,就是这样!我的意思是后一种解决方案。谢谢!请作为答案提交。

标签: r shiny shiny-reactivity


【解决方案1】:

可以在nearPoints函数中指定xvaryvar,即

nearPoints(df, input$clicked, xvar="x", yvar="y")

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2019-01-12
    • 1970-01-01
    • 2014-08-14
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2011-08-02
    相关资源
    最近更新 更多