r - 在 R Shiny 中创建深入分析报告

标签 r shiny shiny-server shinydashboard

我正在尝试显示一个包含“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)

enter image description here

关于r - 在 R Shiny 中创建深入分析报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263879/

相关文章:

安装软件包时出现 R 错误(summarytools)

r - 使用列来控制 Shiny 仪表板中的 tabBox 内容

r - 允许用户修改数据的 Shiny R 应用程序

python - 在 R Shiny 应用程序中接收推送数据

R:tuneRF 函数的行为不明确(randomForest 包)

r - 有没有办法使用 read.csv 从字符串值而不是 R 中的文件中读取?

r - ggplot误差条位置多因素问题

javascript - R Shiny – 使用 CSS 滚动时让 wellPanel 弹出窗口跟随

r - 从R Shiny App中的 react 性data()调用变量

r - 如何定期更新 Shiny 应用程序中的数据?