If you are unfamiliar to ztable, please read the ztable vignette: https://CRAN.R-project.org/package=ztable/vignettes/ztable.html
You can install R package “ztable” from CRAN. Current version is 0.1.8.
install.packages("ztable")
You can install the developmental version of ztable from github. Current github version is 0.1.9.
if(!require(devtools)) install.packages("devtools")
::install_github("cardiomoon/ztable") devtools
Package “ztable” make everything possible about table. Basically, An object of “ztable” made from a data.frame. The default output format of ztable is RStudio::viewer or web-browser format(type=“viewer”). So if you want to use ztable in a “html” format, you should change the parameter ztable.type to “html”. If you want to use ztable in latex format, you should change the parameter ztable.type to “latex”.
library(ztable)
library(magrittr)
options(ztable.type="html")
=ztable(head(iris),caption="Table 1. Basic Table")
z z
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa |
You can change background color and font color with bg and color arguments in addRowColor(), addColColor() and addCellColor() functions.
ztable(head(iris),caption="Table 2. Table with desired background and font colors") %>%
addRowColor(rows=1,bg="#C90000",color="white") %>%
addCellColor(rows=3,cols=c(4,6), bg="cyan",color="red")
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa |
You can select rows with logical expression. You can select cols with column name.
ztable(head(iris),caption="Table 3. Conditinoal Formatting: Sepal.Width >= 3.5") %>%
addRowColor(rows=1,bg="#C90000",color="white") %>%
addCellColor(condition=Sepal.Width>=3.5,cols=Sepal.Width,color="red")
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa |
ztable(head(mtcars),caption="Table 4. Cars with mpg > 21 ") %>%
addCellColor(condition=mpg>21,cols=1:2,bg="cyan",color="red")
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 | 2.62 | 16.46 | 0.00 | 1.00 | 4.00 | 4.00 |
You can use color palettes from RColorBrewer packages. You can extract colors from palette by using palette2colors() function.
require(RColorBrewer)
: RColorBrewer
Loading required package=palette2colors("Reds")
reds
reds1] "#FFF5F0" "#FEE0D2" "#FCBBA1" "#FC9272" "#FB6A4A" "#EF3B2C" "#CB181D"
[8] "#A50F15" "#67000D" [
You can use the extracted colors to your ztable.
ztable(head(iris),caption="Table 5. Use of color palette") %>%
addColColor(bg=reds)
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa |
ztable(head(mtcars),caption="Table 6. Use of color palette(2)") %>%
addRowColor(bg=palette2colors("Set3"))
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 | 2.62 | 16.46 | 0.00 | 1.00 | 4.00 | 4.00 |
You can use ztable for html and latex output. But it is impossible to use ztable in Microsoft Word
or Microsoft Powerpoint
output directly. The officer
package by David Gohel makes it possible to access and manipulate Microsoft Word
or Microsoft Powerpoint
document. You can insert a flextable object office documents with officer package.
require(officer)
: officer
Loading required packagerequire(flextable)
: flextable
Loading required package=regulartable(head(iris))
ft ft
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 |
You can make a ‘Microsoft Word’ document with this flextable.
read_docx() %>%
body_add_flextable(ft) %>%
print(target = "flextable.docx")
You can convert an object of class ztable to a flextable object.
=c("Sepal","Petal","Species")
cgroup=c(2,2,1)
n.cgroup<- ztable(head(iris),caption="Table 9. Use of column groups") %>%
z addcgroup(cgroup=cgroup,n.cgroup=n.cgroup,color=c("red","green","blue")) %>%
spanRow(col=4,from=2,to=3,bg="cyan") %>%
spanCol(row=5,from=2,to=3,bg="cyan",color="blue")
z
Sepal | Petal | Species | |||||
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |||
---|---|---|---|---|---|---|---|
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa | ||
ztable2flextable(z)
Sepal | Petal | Species | |||
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa |
2 | 4.90 | 3.00 | 0.20 | setosa | |
3 | 4.70 | 3.20 | 1.30 | 0.20 | setosa |
4 | 4.60 | 1.50 | 0.20 | setosa | |
5 | 5.00 | 3.60 | 1.40 | 0.20 | setosa |
6 | 5.40 | 3.90 | 1.70 | 0.40 | setosa |
<- lm(mpg ~ cyl + disp + wt + drat + am, data=mtcars)
fit =ztable(fit,caption="Table 10. Results of Multiple Regression Analysis ")
z z
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | 41.2964 | 7.5384 | 5.48 | < 0.0001 |
ztable2flextable(z)
Table 10. Results of Multiple Regression Analysis | ||||
rowname | Estimate | Std. Error | t value | Pr(>|t|) |
(Intercept) | 41.2964 | 7.5384 | 5.48 | < 0.0001 |
cyl | -1.7940 | 0.6505 | -2.76 | 0.0105 |
disp | 0.0074 | 0.0123 | 0.60 | 0.5546 |
wt | -3.5870 | 1.2105 | -2.96 | 0.0064 |
drat | -0.0936 | 1.5488 | -0.06 | 0.9523 |
am | 0.1730 | 1.5300 | 0.11 | 0.9109 |
Call: lm(formula = mpg ~ cyl + disp + wt + drat + am, data = mtcars) |
You can change the color of rows in which p value is below the desired level(default value is 0.05).
=z %>% addSigColor
z1 z1
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | 41.2964 | 7.5384 | 5.48 | < 0.0001 |
ztable2flextable(z1) %>% autofit()
Table 10. Results of Multiple Regression Analysis | ||||
rowname | Estimate | Std. Error | t value | Pr(>|t|) |
(Intercept) | 41.2964 | 7.5384 | 5.48 | < 0.0001 |
cyl | -1.7940 | 0.6505 | -2.76 | 0.0105 |
disp | 0.0074 | 0.0123 | 0.60 | 0.5546 |
wt | -3.5870 | 1.2105 | -2.96 | 0.0064 |
drat | -0.0936 | 1.5488 | -0.06 | 0.9523 |
am | 0.1730 | 1.5300 | 0.11 | 0.9109 |
Call: lm(formula = mpg ~ cyl + disp + wt + drat + am, data = mtcars) |
You can change the significant level and background and font color.
= z %>% addSigColor(level=0.01,bg="yellow",color="red")
z2 z2
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | 41.2964 | 7.5384 | 5.48 | < 0.0001 |
ztable2flextable(z2)
Table 10. Results of Multiple Regression Analysis | ||||
rowname | Estimate | Std. Error | t value | Pr(>|t|) |
(Intercept) | 41.2964 | 7.5384 | 5.48 | < 0.0001 |
cyl | -1.7940 | 0.6505 | -2.76 | 0.0105 |
disp | 0.0074 | 0.0123 | 0.60 | 0.5546 |
wt | -3.5870 | 1.2105 | -2.96 | 0.0064 |
drat | -0.0936 | 1.5488 | -0.06 | 0.9523 |
am | 0.1730 | 1.5300 | 0.11 | 0.9109 |
Call: lm(formula = mpg ~ cyl + disp + wt + drat + am, data = mtcars) |
For more options of flextable, please read the flextable vignette at https://davidgohel.github.io/flextable/index.html.