如何从闪亮的点击中获取x类别
Posted
技术标签:
【中文标题】如何从闪亮的点击中获取x类别【英文标题】:How to get x category from click in shiny 【发布时间】:2021-11-29 19:24:03 【问题描述】:如何通过单击闪亮的条形图从条形图中获取类别(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)
【问题讨论】:
【参考方案1】:您从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])
【讨论】:
以上是关于如何从闪亮的点击中获取x类别的主要内容,如果未能解决你的问题,请参考以下文章