r语言 - 如何创建向下钻取表



我想根据用户想要显示的内容创建一个向下钻取表。我在从下拉框中对用户选择进行整洁评估时遇到问题,因此向下钻取表不显示相关结果。

代码在这里:

library("shiny")
library("DT")

ui <- fluidPage(
  uiOutput("endoDoc_documentqual"),
  dataTableOutput("summary")
  , dataTableOutput("drilldown")
)

server <- function(input, output){

  output$endoDoc_documentqual<-renderUI({
    selectInput("endoDoc_documentqualChoose", label = h4("Choose teh column of interest"),
                choices = colnames(iris) ,selected = 1
    )
  })
  # display the data that is available to be drilled down
  output$summary <- DT::renderDataTable(# create a summary table
    summary_iris <- group_by(iris, !!rlang::sym(input$endoDoc_documentqualChoose)) %>%
      dplyr::summarise(Count = n())
  )
  # 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!")
    )    
   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)

在分配summary_iris时,您需要使用 <<- 而不是 <-

    summary_iris <<- group_by(iris, !!rlang::sym(input$endoDoc_documentqualChoose)) %>%
  dplyr::summarise(Count = n())

但在底部还有另一个问题。仅选择"$Species"列。但是,它仅适用于selectInput内的物种输入。

更新:

如果我没有理解错你,这些代码会为iris输出提供用户选择。

selected_species <- summary_iris[as.integer(input$summary_rows_selected), ]
variables <-    c(t(selected_species[,1]))
mycolname <- colnames(selected_species)[1]
iris[iris[, mycolname] %in%  variables ,]

最新更新