不要在kable函数中删除空格-例如 [英] Do not remove spaces in kable function - with example
问题描述
我正在汇总数据并在Shiny应用程序中创建一个表.我的基本问题是,我想在级联字符串的元素之间添加一些额外的空格,以使其更易读,但是添加的空格似乎消失了.我认为paste()函数适当地在"+/-"符号周围添加了额外的空间.符号,但是使用kable或kableExtra创建表会删除空格.
I'm summarizing data and creating a table within a Shiny application. My basic problem is that I would like to add some additional spaces in between elements in a concatenated string so that it is more readable, but the spaces added seem to disappear. I think that the paste() function is appropriately adding the extra space around the "+/-" symbol, but that using either kable or kableExtra to create a table deletes the spaces.
我想我问的是与这个人相同的问题,但该个人没有提供完整的可复制示例.我试图在下面用虹膜数据集创建一个reprex.
I think I'm asking the same question as this person did, but that individual did not provide a full reproducible example. I've attempted to create a reprex below with the iris dataset.
我有3次尝试使用mutate()创建带有"+/-"符号的新变量.象征.我最大的理解是,这并不是真正的问题.我希望能够在此符号的两侧添加任意数量的空格,以提高可读性.
I have three attempts to use mutate() to create a new variable with the "+/-" symbol. My best understanding is that this is not really the problem. I would like to be able to add in any amount of blank spaces on either side of this symbol for readability.
library(tidyverse)
library(kableExtra)
data(iris)
data.summary = iris %>%
group_by(Species) %>%
summarise(N = n(),
avg.sepal.width = round(mean(Sepal.Width),2),
sd.sepal.width = round(sd(Sepal.Width),2)) %>%
# mutate(table.name = paste(avg.sepal.width, '\u00B1', sd.sepal.width)) %>%
# mutate(table.name = paste(avg.sepal.width, '\u00B1', sd.sepal.width, sep= " ")) %>%
mutate(table.name = paste(avg.sepal.width, ' ', '\u00B1',' ', sd.sepal.width, sep= " ")) %>%
knitr::kable(caption = "Iris Avg Sepal Width \u00B1 Standard Deviation ", align = "c") %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F, position = "left")
data.summary
这是在RStudio中查看的结果表的屏幕快照.使用Shiny时,结果是相同的.不管我添加多少空格,在此表中格式化时它们都会消失
Here is a screenshot of the resulting table-viewed in RStudio. When using Shiny, the results are the same. No matter how many spaces I add, they all go away when formatted in this table
推荐答案
这不是kable
问题.这就是html的行为方式.如果要在文本中添加多余的空格,则需要在文本中添加" "
.将其设置为粘贴分隔符,并确保在kable
内旋转escape = FALSE
:
This is not a kable
problem. This is how html behaves. If you want to add extra spaces in text you need to add " "
to your text. Make this your paste separator, and ensure you turn escape = FALSE
inside kable
:
library(tidyverse)
library(kableExtra)
data(iris)
data.summary = iris %>%
group_by(Species) %>%
summarise(N = n(),
avg.sepal.width = round(mean(Sepal.Width),2),
sd.sepal.width = round(sd(Sepal.Width),2)) %>%
mutate(table.name = paste(avg.sepal.width, '\u00B1',
sd.sepal.width, sep = " ")) %>%
knitr::kable(caption = "Iris Avg Sepal Width \u00B1 Standard Deviation ",
align = "c", escape = FALSE) %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = F,
position = "left")
data.summary
这篇关于不要在kable函数中删除空格-例如的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!