如何通过在shiny中单击从条形图中获取类别(x轴信息)。请看下面的应用程序。我希望当用户单击绘图中的某个条形图时,output$cut
会显示cut
类型。
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
plotOutput('diamonds', click = "click"),
textOutput('cut')
)
server <- function(input, output, session) {
output$diamonds <- renderPlot({
diamonds %>%
ggplot() +
geom_bar(aes(cut))
})
output$cut <- renderText({
req(input$click)
# This isn't meaningful
x <- round(input$click$x, 2)
y <- round(input$click$y, 2)
cat("[", x, ", ", y, "]", sep = "")
# and this doesn't work either
# nearPoints(diamonds, input$click, xvar = "cut")
})
}
shinyApp(ui, server)
发布于 2021-10-10 23:12:31
您从input$click$x
获得的x值对应于所单击的因子。需要将其四舍五入为整数,并可用于获取因子名称。
因子名称可用于筛选diamonds
数据集。
x <- round(input$click$x) ## this will return a whole number corresponding to the factor that was clicked on.
##filter for the selected cut by using levels to get the name of the factor.
selected_cut <- diamonds %>% filter(cut == levels(diamonds$cut)[x])
https://stackoverflow.com/questions/69519497
复制相似问题