R DT datatable - 格式化文本字段并垂直对齐另一个字段 [英] R DT datatable - format text field and vertically align another field
问题描述
DT
datatable 显示>。我也希望(1)格式化该文本字段,以便显示换行符,重点放在某些文本块上;和(2)垂直对齐剩余的字段,以便将其值推送到顶部。 请考虑以下示例:
库(DT)
L< - 10
datatable(
data.frame(
var1 = sapply(1:L,function(x)
paste(< X>,paste0(x,
letters,
LETTERS,
\\\
,
collapse =))),
var2 = round(rnorm(L),2)
)
)
如您所见,输出忽略 \\\
。我也想使
< X>
粗体。我尝试使用HTML标签,如< br>
,但是似乎没有什么可行,因为 var1
中的文本是逃脱了(可以通过 datatable
选项实现)不是一个好主意,因为(实际)文本包含特殊字符。我也希望 var2
的值被推到最上方。
以防万一有所作为我想在Shiny网络应用程序中使用输出。
有没有人有任何关于如何实现我正在寻找的建议?
非常感谢提前。
在DT单元格中实现换行符您需要使用< br />
而不是\\\
和escape(它将以HTML的形式读取)具有转义参数的列。
要更改单元格中的值的对齐方式,可以在datatable函数的选项中使用columnDefs参数。问题是您只能对齐水平(左,右和中)
使用rowCallback函数,我可以手动将第二列中的每个值设置为顶部对齐。但是这不是一个理想的解决方案。
图书馆(DT)
L < - 10
数据集< - 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'});}))
)
)
I have a dataset with a text field which I would like to display using the function datatable
from the package DT
. 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; and (2) vertically align the remaining fields so that their values are pushed to the top.
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
. I would also like to make <X>
bold. I've tried using HTML tags such as <br>
but nothing seems to work as the text inside var1
is escaped. Unescaping it (which can be achieved via datatable
options) is not a good idea as the (actual) text contains special characters. I would also like the values of var2
to be pushed to the top.
Just in case it make a difference, I would like to use the outputs in a Shiny web-app.
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.
To change the alignment of a value in a cell you can use the columnDefs argument within the options of datatable function. 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. 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'});}"))
)
)
这篇关于R DT datatable - 格式化文本字段并垂直对齐另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!