我正在尝试使用ggplot
功能在鼠标点击附近找到nearPoints
中的点,但它无效。我使用下面的代码创建了闪亮的应用程序,其中包含两个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
任何建议都会受到赞赏吗?
答案 0 :(得分: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)
产生这一点 - 注意data.frame输出是diamonds
中靠近鼠标点击的点: