R闪亮的数据表链接到另一个选项卡

时间:2018-04-06 20:34:06

标签: r datatable hyperlink shiny

我正在尝试在单击第一个数据表中的行时导航到R shiny中的另一个数据表选项卡。 我在这里看到类似的例子。 但是我无法使用它们,因为我将数据从数据库直接提取到数据表中。

Hyperlink from one DataTable to another in Shiny

你能指导我如何完成它吗?

        output$TbTable <- DT::renderDataTable(datatable(sqlOutput(),style = 'bootstrap', class = 'table-striped table-hover table-condensed',
                                                    extensions = c("FixedColumns","Scroller"),
                                                    filter = 'top',
                                                    options = list(
                                                      # dom = 't',
                                                      # deferRender = TRUE,
                                                      searching = TRUE,
                                                      autoWidth = TRUE,
                                                      # scrollCollapse = TRUE,
                                                      rownames = FALSE,
                                                      scroller = TRUE,
                                                      scrollX = TRUE,
                                                      scrollY = "500px",
                                                      #fixedHeader = TRUE,
                                                      class = 'cell-border stripe',
                                                      fixedColumns = list(
                                                        leftColumns = 3,
                                                        heightMatch = 'none', escape = FALSE,
            options = list(initComplete = JS(
              'function(table) {
    table.on("click.dt", "tr", function() {
    Shiny.onInputChange("rows", table.row( this ).index());
    tabs = $(".tabbable .nav.nav-tabs li a");
    $(tabs[1]).click();
    });
}'))
))))

你能帮我解决这个问题吗?

谢谢,

1 个答案:

答案 0 :(得分:3)

我没有深入参与DT,但这个JS回调函数有效:

function(settings, json) {
  var table = this.DataTable();
  table.on("click.dt", "tr", function() {
    Shiny.onInputChange("rows", table.row( this ).index());
    var tabs = $(".tabbable .nav.nav-tabs li a");
    $(tabs[1]).click();
  });
}

<强> MRE:

library(shiny)
library(ggplot2)  # for the diamonds dataset
library(htmlwidgets)

ui <- fluidPage(
  title = "Examples of DataTables",
  sidebarLayout(
    sidebarPanel(
      conditionalPanel(
        'input.dataset === "diamonds"',
        checkboxGroupInput("show_vars", "Columns in diamonds to show:",
                           names(diamonds), selected = names(diamonds))
      ),
      conditionalPanel(
        'input.dataset === "mtcars"',
        helpText("Click the column header to sort a column.")
      ),
      conditionalPanel(
        'input.dataset === "iris"',
        helpText("Display 5 records by default.")
      )
    ),
    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("diamonds", DT::dataTableOutput("mytable1")),
        tabPanel("mtcars", DT::dataTableOutput("mytable2")),
        tabPanel("iris", DT::dataTableOutput("mytable3"))
      )
    )
  )
)


jss <- '
function(settings, json) {
  var table = this.DataTable();
  table.on("click.dt", "tr", function() {
    Shiny.onInputChange("rows", table.row( this ).index());
    var tabs = $(".tabbable .nav.nav-tabs li a");
    $(tabs[1]).click();
  });
}'

server <- function(input, output) {

  # choose columns to display
  diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]
  output$mytable1 <- DT::renderDataTable({
    DT::datatable(diamonds2[, input$show_vars, drop = FALSE], options = list(initComplete = JS(jss)))
  })

  # sorted columns are colored now because CSS are attached to them
  output$mytable2 <- DT::renderDataTable({
    DT::datatable(mtcars, options = list(orderClasses = TRUE))
  })

  # customize the length drop-down menu; display 5 rows per page by default
  output$mytable3 <- DT::renderDataTable({
    DT::datatable(iris, options = list(initComplete = JS(jss)))})
}

shinyApp(ui, server)