gpt4 book ai didi

r - nearPoints 无法从 Shiny 的坐标信息中自动推断出 `xvar`

转载 作者:行者123 更新时间:2023-12-04 10:56:32 33 4
gpt4 key购买 nike

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

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)

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

Error: nearPoints: not able to automatically infer xvar from coordinfo

enter image description here

有什么建议吗?

最佳答案

这就是你想要我想的。您正在“打印”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 中靠近鼠标点击的点:

enter image description here

关于r - nearPoints 无法从 Shiny 的坐标信息中自动推断出 `xvar`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570553/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com