简体   繁体   English

R DT 数据表 - 格式化文本字段并垂直对齐另一个字段

[英]R DT datatable - format text field and vertically align another field

I have a dataset with a text field which I would like to display using the function datatable from the package DT .我有一个带有文本字段的数据集,我想使用包DT的函数datatable显示它。 I would also like to (1) format that text field so that the line breaks are displayed and emphasis is placed on certain chunks of text;我还想(1)格式化该文本字段,以便显示换行符并将重点放在某些文本块上; and (2) vertically align the remaining fields so that their values are pushed to the top. (2)垂直对齐其余字段,以便将它们的值推到顶部。

Consider the below example:考虑下面的例子:

library(DT)
L <- 10
datatable(
  data.frame(
    var1 = sapply(1:L, function(x) 
      paste("<X>",paste0(x,
                         letters, 
                         LETTERS, 
                         "\n", 
                         collapse=" "))),
    var2 = round(rnorm(L),2)
  ) 
)

输出

As you can see, the output ignores \\n .如您所见,输出忽略了\\n I would also like to make <X> bold.我还想让<X>加粗。 I've tried using HTML tags such as <br> but nothing seems to work as the text inside var1 is escaped.我试过使用 HTML 标签,例如<br>但似乎没有任何效果,因为var1的文本被转义了。 Unescaping it (which can be achieved via datatable options) is not a good idea as the (actual) text contains special characters.它反向转义(可以通过实现datatable的(实际)的文本包含特殊字符选项)是不是一个好主意。 I would also like the values of var2 to be pushed to the top.我还希望将var2的值推到顶部。

Just in case it make a difference, I would like to use the outputs in a Shiny web-app.以防万一,我想在 Shiny 网络应用程序中使用输出。

Does anyone have any suggestions on how to achieve what I'm looking for?有没有人对如何实现我正在寻找的东西有任何建议?

Many thanks in advance.非常感谢提前。

To implement the line break in the DT cells you need to use <br/> instead of \\n and escape(it will be read as HTML) the column with the escape argument.要在 DT 单元格中实现换行符,您需要使用<br/>而不是 \\n 并转义(它将被读取为 HTML)带有转义参数的列。

To change the alignment of a value in a cell you can use the columnDefs argument within the options of datatable function.要更改单元格中值的对齐方式,您可以使用 datatable 函数选项中的 columnDefs 参数。 The problem is that you only can align horizontal.( left, right and center) With the rowCallback function I can manually set every value in the second column to top alignment.问题是你只能水平对齐。(左、右和中心)使用 rowCallback 函数,我可以手动将第二列中的每个值设置为顶部对齐。 But this is not an ideal solution.但这并不是一个理想的解决方案。

library(DT)
L <- 10

dataset <- data.frame(
  var1 = sapply(1:L, function(x) 
    paste("<X>",paste0(x,
                       letters, 
                       LETTERS, 
                       "<br/>", 
                       collapse=" "))),
  var2 = round(rnorm(L),2)
)

datatable(dataset, escape = 1,  
  options = list(
    columnDefs = list(list(className = 'dt-center', targets = 2)),
    rowCallback=JS(paste0("function(row, dataset) {var value=dataset[2]; if (value!==null) $(this.api().cell(row,2).node()).css({'vertical-align': 'text-top'});}"))
  )
)

To vertically align your column content you can do this:垂直对齐列内容,您可以执行以下操作:

T1 <- datatable(dataset)

 T1 %>%
  formatStyle(c('column_1','column_2'), 'vertical-align'='top') %>% 
  formatStyle(c('column_1','column_2'), 'text-align' = 'left') 

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

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