【问题标题】:DT export without format R ShinyDT 导出无格式 R Shiny
【发布时间】:2020-10-28 21:09:17
【问题描述】:

我正在使用DT 的按钮扩展名导出数据。点击copyexcelpdf时,格式也会被导出。

DT 0.14 版包含与旧版 0.5 相反的格式,这是所需的行为。

可重现的示例,Sepal.Width 列,表中的单位为米,我不想导出:

library(shiny)
library(DT)

ui <- fluidPage(
    dataTableOutput("dt_table")
)

server <- function(input, output) {

    output$dt_table <- renderDataTable({
        datatable(
            iris,
            options = list(dom = 'lBfrtip', buttons = c('copy','excel', 'pdf')),
            extensions = 'Buttons'
        ) %>% formatString(
            columns = 'Sepal.Width',
            suffix = " meters"
        )
    })
}

shinyApp(ui = ui, server = server)

你知道导出数据集而不格式化的任何论据吗?

感谢您的帮助。

【问题讨论】:

  • 可能在formatString 函数中使用选项appendTo = "rowCallback"
  • 在出口工作,但我失去了闪亮的单位。关闭

标签: r shiny datatables dt


【解决方案1】:

这是一种方法:

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("dt_table")
)

render <- c(
  "function(data, type, row, meta){",
  "  if(type === 'display'){",
  "    return data + ' meters';",
  "  }else{",
  "    return data;",
  "  }",
  "}"
)

server <- function(input, output) {
  
  output$dt_table <- renderDT({
    datatable(
      iris,
      options = list(
        dom = 'lBfrtip', 
        buttons = list(
          list(
            extend = "copy",
            exportOptions = list(orthogonal = "export")
          ),
          list(
            extend = "excel",
            exportOptions = list(orthogonal = "export")
          ),
          list(
            extend = "pdf",
            exportOptions = list(orthogonal = "export")
          )
        ),
        columnDefs = list(
          list(targets = 2, render = JS(render))
        )
      ),
      extensions = 'Buttons'
    )
  })
  
}

shinyApp(ui = ui, server = server)

编辑

另一种方式:

tplString2 <- function(prefix, suffix, ...) {
  sprintf(
    "type === 'display' ? DTWidget.formatString(data, %s, %s) : data;",
    DT:::jsValues(prefix), DT:::jsValues(suffix)
  )
}

formatString2 <- function(table, columns, prefix = '', suffix = '') {
  DT:::formatColumns(table, columns, tplString2, prefix, suffix)
}

tplDate2 <- function(method, params, ...) {
  params = if (length(params) > 0) paste(',', jsonlite::toJSON(params)) else ''
  sprintf(
    "type === 'display' ? DTWidget.formatDate(data, %s%s) : data;", 
    DT:::jsValues(method), params
  )
}

formatDate2 <- function(table, columns, method = 'toDateString', params = NULL) {
  if (!inherits(table, 'datatables'))
    stop("Invalid table argument; a table object created from datatable() was expected")
  x = table$x
  if (x$filter != 'none') {
    if (inherits(columns, 'formula')) columns = all.vars(columns)
    colnames = base::attr(x, 'colnames', exact = TRUE)
    rownames = base::attr(x, 'rownames', exact = TRUE)
    if (is.null(params)) params = list()
    cols = sprintf("%d", DT:::name2int(columns, colnames, rownames))
    x$filterDateFmt = as.list(x$filterDateFmt)
    for (col in cols) x$filterDateFmt[[col]] = list(
      method = method, params = jsonlite::toJSON(params)
    )
    table$x = x
  }
  DT:::formatColumns(table, columns, tplDate2, method, params)
}

library(shiny)
library(DT)

server <- function(input, output) {
  
  output$dt_table <- renderDT({
    datatable(
      iris,
      options = list(
        dom = 'lBfrtip', 
        buttons = list(
          list(
            extend = "copy",
            exportOptions = list(orthogonal = "export")
          ),
          list(
            extend = "excel",
            exportOptions = list(orthogonal = "export")
          ),
          list(
            extend = "pdf",
            exportOptions = list(orthogonal = "export")
          )
        )
      ),
      extensions = 'Buttons'
    ) %>% 
     formatString2(columns = 1, suffix = " meters")
  })
  
}

【讨论】:

  • @Clemsang 对不起。我刚刚编辑了我的答案。现在应该可以了。
  • 工作感谢。我有不同的单位,如果您知道继续使用它的方法,formatString 会更容易(我的代码中也有 formatDate)。
  • @Clemsang 查看我的编辑。使用formatString2formatDate2。您还必须像我一样设置buttons 选项(使用exportOptions)。
  • 在没有 js 的纯 R DT 中拥有它会很棒,但还没有。重载函数似乎有点矫枉过正,但它会起作用。非常感谢您的宝贵时间。
  • @Clemsang 我建议DT 作者将这些修改包含在DT 的下一个版本中。如果他们接受,您将不需要使用这些自定义函数。但他们似乎在度假。
猜你喜欢
  • 2021-12-13
  • 2018-04-13
  • 2018-02-06
  • 1970-01-01
  • 2016-01-15
  • 2021-08-04
  • 2019-12-19
  • 1970-01-01
  • 2018-07-02
相关资源
最近更新 更多