简体   繁体   English

以交互方式从R Shiny中的小节检索源数据信息

[英]Retrieving source data information from a barplot in R shiny interactively

I have a barplot in R shiny. 我在R Shiny中有一个小图。 I want following: 我想要以下内容:

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". 当用户单击该条形图中的一个“条”时,将弹出另一个框,并显示来自使用的datadframe的数据信息,其中显示了哪些数据点有助于创建该“条”。

Code: 码:

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)

When I'm clicking on a bar, it is showing the (x,y) co-ordinate value. 当我单击一个条时,它显示(x,y)坐标值。 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. 理想的情况是:当用户单击carb = 4的条时,应显示carb = 4的mtcars数据集的源信息。 But I'm stuck how to get the "carb=4" information interactively from that bar plot. 但是我仍然停留在如何从该条形图交互地获取“ carb = 4”信息的问题上。

Edited to be more specific to categorical x axis bar charts. 编辑以更特定于分类x轴条形图。

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: 我已经将点击的x值取整以将mtcars数据框作为子集:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM