首页 > 解决方案 > 主数据表中的另一个数据表

问题描述

当用户单击该行时,是否有办法获取数据表。基本上,当用户单击任何行时,该特定行的最后 4 列应以模式显示(以数据表格式)

library(shiny)

ui <- fluidPage(
  DT::dataTableOutput("mydatatable")
)

server <- function(input, output, session) {
  mycars = head(mtcars)
  output$mydatatable = DT::renderDataTable(mycars, selection = 'single',  
                                           rownames = FALSE, options = list(dom = 't'))
  
  observeEvent(input$mydatatable_rows_selected,
               {
                 showModal(modalDialog(
                   title = "You have selected a row!",
                   mycars[input$mydatatable_rows_selected,]
                 ))
               })
  
}

shinyApp(ui, server)

标签: rshiny

解决方案


你可以renderDataTable在里面使用modalDialog

library(shiny)
library(DT)

ui <- fluidPage(
  DT::dataTableOutput("mydatatable")
)

server <- function(input, output, session) {
  mycars = head(mtcars)
  output$mydatatable = DT::renderDataTable(mycars, selection = 'single',  
                                           rownames = FALSE, options = list(dom = 't'))
  
  observeEvent(input$mydatatable_rows_selected,
               {
                 showModal(modalDialog(
                   title = "You have selected a row!",
                   DT::renderDataTable({
                     DT::datatable(mycars[input$mydatatable_rows_selected, (ncol(mycars) - 3):ncol(mycars)])
                     })
                 ))
               })
  
}

shinyApp(ui, server)

在此处输入图像描述


推荐阅读