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
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)
<- 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)
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()
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)
For more options of flextable, please read the flextable vignette at https://davidgohel.github.io/flextable/index.html.