nearPoints无法从闪亮的coordinfo中自动推断出`xvar`

shi*_*iny 3 plot r ggplot2 shiny

我试图ggplot使用该nearPoints功能在鼠标点击附近找到点,但它无法正常工作.我使用下面的代码创建了带有两个diamondsdata.frame 图的闪亮app :

library(shiny)
library(ggplot2)
ui <- fluidPage(
mainPanel(
uiOutput("tb")
)
)
server <- function(input,output){

   output$diamonds1 <- renderPlot({

        print(ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + 
                                geom_point(alpha=0.5)+ facet_wrap(~color, scales="free"))
   })
   output$diamonds2 <- renderPlot({

        print(ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + 
                               geom_point(alpha=0.5)+ facet_wrap(~cut, scales="free"))
   }) 

   output$info <- renderPrint({

        nearPoints(diamonds, input$plot_click, threshold = 10, maxpoints = 1,
                   addDist = TRUE)
   })

output$tb <- renderUI({
tabsetPanel(tabPanel("First plot", 
                     plotOutput("diamonds1")),
            tabPanel("Second plot", 
                     plotOutput("diamonds2", click = "plot_click"), 
                     verbatimTextOutput("info"))) 
})
}
shinyApp(ui = ui, server = server) 
Run Code Online (Sandbox Code Playgroud)

我在第二个情节中不断收到此错误

错误:nearPoints:无法xvar从coordinfo 自动推断

在此输入图像描述

任何建议,将不胜感激?

Mik*_*ise 6

这就是你想要的.你正在"打印"ggplot,这显然令人困惑nearPoints:

library(shiny)
library(ggplot2)
ui <- fluidPage(
  mainPanel(
    uiOutput("tb")
  )
)
server <- function(input,output){

  output$diamonds1 <- renderPlot({

    print(ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + 
                            geom_point(alpha=0.5)+ facet_wrap(~color, scales="free"))
  })
  output$diamonds2 <- renderPlot({

    ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + 
                     geom_point(alpha=0.5)+ facet_wrap(~cut, scales="free")
  }) 

  output$info <- renderPrint({
    nearPoints(diamonds,input$plot_click,threshold = 10, maxpoints = 1,addDist = TRUE)
  })

  output$tb <- renderUI({
    tabsetPanel(tabPanel("First plot", 
                         plotOutput("diamonds1")),
                tabPanel("Second plot", 
                         plotOutput("diamonds2", click = "plot_click"), 
                         verbatimTextOutput("info"))) 
  })
}
shinyApp(ui = ui, server = server) 
Run Code Online (Sandbox Code Playgroud)

产生这一点 - 请注意,data.frame输出是diamonds鼠标点击附近的点:

在此输入图像描述