Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

r - 在knitr :: kable()中设置自适应col.names(Set adaptive col.names in knitr::kable())

Please consider the following.

(请考虑以下内容。)

I started writing reproducible documents in with R markdown and want some output for a report.

(我开始用R降价来编写可复制的文档,并希望输出一些报告。)

As I am working with more than one data.frame and their column names are not very informative or pretty I would like to make use of the col.names argument in knitr::kable() .

(由于我正在使用多个data.frame并且它们的列名不是很有用,所以我想在knitr::kable()使用col.names参数。)


Problem: Since the data.frame is fairly big and I want to display only specific columns throughout the report I would like the new column names to appear automatically depending on the columns I choose.

(问题:由于data.frame相当大,并且我只想在整个报表中显示特定的列,因此我希望新的列名根据选择的列自动显示。)

I can do this by hand like in the following example:

(我可以手动执行此操作,如以下示例所示:)

library(knitr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

knitr::kable(iris %>% head(),
             col.names = c("Sepal length", "Sepal width", "Petal length",
                           "Petal width", "Species"))

| Sepal length| Sepal width| Petal length| Petal width|Species |
|------------:|-----------:|------------:|-----------:|:-------|
|          5.1|         3.5|          1.4|         0.2|setosa  |
|          4.9|         3.0|          1.4|         0.2|setosa  |
|          4.7|         3.2|          1.3|         0.2|setosa  |
|          4.6|         3.1|          1.5|         0.2|setosa  |
|          5.0|         3.6|          1.4|         0.2|setosa  |
|          5.4|         3.9|          1.7|         0.4|setosa  |

But when I reduce this data.frame to display only certain columns, I have to manually set the col.names again (here deleting the col.names I don't need anymore) to not receive an error message:

(但是,当我减少此data.frame以仅显示某些列时,我必须再次手动设置col.names (此处删除不再需要的col.names ),以不会收到错误消息:)

knitr::kable(iris %>% filter(Species == "setosa") %>% 
           select(Sepal.Length, Sepal.Width, Species) %>% head(),
         col.names = c("Sepal length", "Sepal width", "Species"))

| Sepal length| Sepal width|Species |
|------------:|-----------:|:-------|
|          5.1|         3.5|setosa  |
|          4.9|         3.0|setosa  |
|          4.7|         3.2|setosa  |
|          4.6|         3.1|setosa  |
|          5.0|         3.6|setosa  |
|          5.4|         3.9|setosa  |

Question: Is there a way to overcome this with for example using switch and specifying only once that "Sepal.Length" = "Sepal length" etc.?

(问题:是否可以通过例如使用switch并仅指定一次"Sepal.Length" = "Sepal length"等来克服此问题 ?)

This should also take into account any new columns I create for instance through dplyr::mutate() by either keeping the newly added column name as is or by also specifying it at the beginning of the document without throwing back an error every time this column is not (yet) existing.

(这还应考虑到我通过dplyr::mutate()例如创建的任何新列,方法是保持新添加的列名dplyr::mutate() ,或者在文档开始时也指定该名称,而不必每次此列都抛出错误还不存在。)

  ask by Frederick translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can modify the column names to data.frames and use column number to specify columns in select()

(您可以将列名称修改为data.frames并使用列号在select()指定列)

Such as:

(如:)

library(dplyr)
colnames(iris) <- c("Sepal length", "Sepal width", "Petal length",
                           "Petal width", "Species")

knitr::kable(iris %>% head() %>% select(1,2,5), format = "markdown")    

| Sepal length| Sepal width|Species |
|------------:|-----------:|:-------|
|          5.1|         3.5|setosa  |
|          4.9|         3.0|setosa  |
|          4.7|         3.2|setosa  |
|          4.6|         3.1|setosa  |
|          5.0|         3.6|setosa  |
|          5.4|         3.9|setosa  |

Created on 2018-08-21 by the reprex package (v0.2.0.9000).

(由reprex包 (v0.2.0.9000)创建于2018-08-21。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...