以交互方式从R Shiny中的小节中检索源数据信息 [英] Retrieving source data information from a barplot in R shiny interactively

查看:123
本文介绍了以交互方式从R Shiny中的小节中检索源数据信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R闪亮的小样.我要关注:

I have a barplot in R shiny. I want following:

当用户单击该条形图中的一个条"时,将弹出另一个框,并显示使用的datadframe中的数据信息,显示哪些数据点有助于创建该条".

When user clicks one "bar" in that barplot another box will pop up and show the data information from the used datadframe showing which data points contributed to create that "bar".

代码:

ui <- fluidPage(
  sidebarLayout(sidebarPanel(),
                mainPanel(plotOutput("p",click = "plot_click"),
                textOutput("info"))))

server <- function(input,output) {
    output$p <- renderPlot(ggplot(mtcars,aes(x=factor(carb),y=mpg))+
                         geom_bar(stat="summary",fun.y="mean"))
    output$info <- renderText(
      paste0("x=", input$plot_click$x, "\n",
           "y=", input$plot_click$y))
}
shinyApp(ui, server)

当我单击一个条时,它显示(x,y)坐标值.但我想检索相应的因子(碳水化合物). 如果单击栏,如何找回源信息. 理想的情况是:当用户单击carb = 4的条时,应显示carb = 4的mtcars数据集的源信息.但是我仍然停留在如何从该条形图交互地获取"carb = 4"信息的问题上.

When I'm clicking on a bar, it is showing the (x,y) co-ordinate value. But I want to retrieve the corresponding factor(carb). How to get back source information back if I click on a bar. The ideal case would be: When user clicks on a bar which has carb=4, it should show the source information of the mtcars dataset with carb=4. But I'm stuck how to get the "carb=4" information interactively from that bar plot.

推荐答案

进行了编辑,使其更加适用于分类x轴条形图.

您需要将点击值转换为所需的表.我将点击后的x值取整以将mtcars数据框作为子集:

You need to translate the click value into the table you want. I've rounded the x value from the click to subset the mtcars data frame:

library(ggplot2)
library(shiny)

ui <- fluidPage(
  sidebarLayout(sidebarPanel(),
                mainPanel(plotOutput("p",click = "plot_click"),
                          textOutput("info"),
                          tableOutput("table"))))

server <- function(input,output) {
  output$p <- renderPlot(ggplot(mtcars,aes(x=factor(carb),y=mpg))+
                           geom_bar(stat="summary",fun.y="mean"))
  output$info <- renderText(
    paste0("x=", input$plot_click$x, "\n",
           "y=", input$plot_click$y))

  output$table <- renderTable({
    ##magic happens here##
    output$table <- renderTable({
     carb <- unique(mtcars$carb)
     carb <- carb[order(carb)]
     x <- carb[round(input$plot_click$x)]
     mtcars[mtcars$carb == x,]
   })

  })
}
shinyApp(ui, server)

这篇关于以交互方式从R Shiny中的小节中检索源数据信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆