【问题标题】:nearPoints not able to automatically infer `xvar` from shiny coordinfonearPoints 无法从闪亮的坐标信息中自动推断“xvar”
【发布时间】:2016-06-04 21:03:48
【问题描述】:

我正在尝试使用nearPoints 函数在鼠标单击附近的ggplot 中查找点,但它不起作用。我使用下面的代码为diamonds data.frame 创建了带有两个绘图的闪亮应用程序:

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) 

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

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

有什么建议吗?

【问题讨论】:

    标签: r plot ggplot2 shiny


    【解决方案1】:

    我想这就是你想要的。您正在“打印”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) 
    

    产生这个 - 注意 data.frame 输出是diamonds 中靠近鼠标点击的点:

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 2018-02-18
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多