简体   繁体   English

Shiny 数据表超链接栏目

[英]Shiny datatable hyperlink column

I am trying to add an interactive feature in one/multiple columns of a Shiny datatable.我正在尝试在 Shiny 数据表的一个/多个列中添加交互功能。 Basically, I am trying to create a hyperlink to all the values of a column (say, mpg) for the datatable in tab 1 and get that row data displayed on tab 2. For instance, for the first row of the datatable, the mpg value (21) would be a hyperlink and when you click on 21, it would redirect to tab 2 that would display all the other column values corresponding to that row clicked.基本上,我正在尝试为选项卡 1 中的数据表创建一个指向列的所有值(例如 mpg)的超链接,并在选项卡 2 上显示该行数据。例如,对于数据表的第一行,mpg value (21) 将是一个超链接,当您单击 21 时,它将重定向到选项卡 2,该选项卡将显示与单击的行对应的所有其他列值。 Here is a working version of my code.这是我的代码的工作版本。

library(shiny)
library(DT)

ui <- fluidPage(
    titlePanel("reprex1")
    ,fluidRow(tabBox(
        tabPanel('Tab1', dataTableOutput("dt1")),
              tabPanel('Tab2'))
        
    )
)

server <- function(input, output) {
    output$dt1 <- renderDataTable({
        mtlocal <- mtcars
        for(n in 1:nrow(mtlocal)){
            mtlocal$actionbutton[[n]] <- as.character(
                actionButton(
                    paste0("buttonpress",n), label = paste0("buttonpress",n)
                )
            )
        }
        datatable(
            mtlocal
            , escape = FALSE
            , selection = "none"
            , options = list(
                preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
            )
        )
    }, server = FALSE)
    
    lapply(
        1:nrow(mtcars),function(x){
            observeEvent(
                input[[paste0("buttonpress",x)]],{
                    showModal(
                        modalDialog(
                            h2(paste0("You clicked on button ",x,"!"))
                        )
                    )
                }
            )       
        }
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

You can use actionLink to create the appropriate link in the datatable .您可以使用actionLink在数据表datatable创建适当的链接。 Then all you have to do is to listen to this actionLink and fill a reactive upon clicking which holds the corresponding row of the data.然后你所要做的就是听这个actionLink并在点击时填充一个reactive ,其中包含相应的数据行。 You render this reactive on your second tab and use updateTabsetPanel to jump to the second tab.您在第二个选项卡上呈现此reactive ,并使用updateTabsetPanel跳转到第二个选项卡。

Code speaks a thousand words so here it goes:代码说了一千个单词,所以这里是:

library(shiny)
library(DT)
library(dplyr)
library(tibble)
library(purrr)
ui <- fluidPage(
   titlePanel("Link in Datatable"),
   tabsetPanel(
      tabPanel("Table",
               dataTableOutput("tab")),
      tabPanel("Details",
               dataTableOutput("details")),
      id = "ts"
   )
)

server <- function(input, output, session) {
   orig_data <- reactive({
      mtcars %>%
         rownames_to_column("id")
   })
   
   details <- reactiveVal(NULL)
   
   output$tab <- renderDataTable({
      orig_data() %>% 
         mutate(mpg = imap_chr(mpg,
                               function(mpg, idx) {
                                  actionLink(paste0("to_details_", idx),
                                             mpg) %>% 
                                     as.character()
                               })
         ) %>% 
         datatable(escape = FALSE, 
                   selection = "none", 
                   rownames = FALSE,
                   options = list(
                      preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                      drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                   ))
   })
   
   output$details <- renderDataTable({
      details() %>% 
         datatable()
   })
   
   obs <- lapply(1:nrow(orig_data()), function(idx) {
      observeEvent(input[[paste0("to_details_", idx)]], {
         details(orig_data() %>% 
                    slice(idx))
         updateTabsetPanel(session, "ts", selected = "Details")
      })
   })
}

shinyApp(ui, server)

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

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