【问题标题】:R shiny datatable link to another tabR闪亮的数据表链接到另一个选项卡
【发布时间】:2018-09-16 22:40:36
【问题描述】:

单击第一个数据表中的一行时,我正在尝试导航到 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();
    });
}'))
))))

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

谢谢,

【问题讨论】:

  • 在浏览器中打开应用,打开开发者工具/控制台并检查错误。
  • 数据表错误:未使用的参数(initComplete = JS(callback = "function(table)..

标签: r datatable hyperlink shiny


【解决方案1】:

我对@9​​87654321@ 的参与并不深,但是这个 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2018-10-06
    • 2016-03-22
    • 1970-01-01
    相关资源
    最近更新 更多