本文介绍了在R SHINY中创建向下钻取报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示列数为‘n’的数据表,如下所示
Begin Date | EndDate | Month | Year | Count of Students
2/1/2014 | 1/31/2015 | Jan | 2014 | 10
3/1/2014 | 2/28/2015 | Feb | 2014 | 20
4/1/2014 | 3/31/2015 | Mar | 2014 | 30
5/1/2014 | 4/30/2015 | Apr | 2014 | 40
我希望通过启用向下钻取/钻取功能使该数据表具有交互性,在该功能中,用户可以单击"学生人数"字段中的每个值,以查看数字10、20、30和40背后的底层原始数据。例如,如果用户点击"10",他/她应该能够看到该计数后面的学生原始数据。这类似于EXCEL中的透视表概念,用户可以在其中看到透视表背后的基础数据。有没有办法用R SHINY做同样的事情?推荐答案
是,使用DT
包捕获所选行并子集主集。以下是使用iris
集合的示例:
library("dplyr")
library("shiny")
library("DT")
# create a summary table
summary_iris <- group_by(iris, Species) %>%
summarise(Count = n())
ui <- fluidPage(
dataTableOutput("summary")
, dataTableOutput("drilldown")
)
server <- function(input, output){
# display the data that is available to be drilled down
output$summary <- DT::renderDataTable(summary_iris)
# subset the records to the row that was clicked
drilldata <- reactive({
shiny::validate(
need(length(input$summary_rows_selected) > 0, "Select rows to drill down!")
)
# subset the summary table and extract the column to subset on
# if you have more than one column, consider a merge instead
# NOTE: the selected row indices will be character type so they
# must be converted to numeric or integer before subsetting
selected_species <- summary_iris[as.integer(input$summary_rows_selected), ]$Species
iris[iris$Species %in% selected_species, ]
})
# display the subsetted data
output$drilldown <- DT::renderDataTable(drilldata())
}
shinyApp(ui, server)
这篇关于在R SHINY中创建向下钻取报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!