ztable Update

Introduction

If you are unfamiliar to ztable, please read the ztable vignette: https://CRAN.R-project.org/package=ztable/vignettes/ztable.html

Installation

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")
devtools::install_github("cardiomoon/ztable")

Make table from a data.frame

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")
z=ztable(head(iris),caption="Table 1. Basic Table")
z
Table 1. Basic Table
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.10 3.50 1.40 0.20 setosa
2 4.90 3.00 1.40 0.20 setosa
3 4.70 3.20 1.30 0.20 setosa
4 4.60 3.10 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

Use background and font color

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") 
Table 2. Table with desired background and font colors
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.10 3.50 1.40 0.20 setosa
2 4.90 3.00 1.40 0.20 setosa
3 4.70 3.20 1.30 0.20 setosa
4 4.60 3.10 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

Conditional Formatting

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") 
Table 3. Conditinoal Formatting: Sepal.Width >= 3.5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.10 3.50 1.40 0.20 setosa
2 4.90 3.00 1.40 0.20 setosa
3 4.70 3.20 1.30 0.20 setosa
4 4.60 3.10 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
ztable(head(mtcars),caption="Table 4. Cars with mpg > 21 ") %>%
    addCellColor(condition=mpg>21,cols=1:2,bg="cyan",color="red") 
Table 4. Cars with mpg > 21
  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
Mazda RX4 Wag 21.00 6.00 160.00 110.00 3.90 2.88 17.02 0.00 1.00 4.00 4.00
Datsun 710 22.80 4.00 108.00 93.00 3.85 2.32 18.61 1.00 1.00 4.00 1.00
Hornet 4 Drive 21.40 6.00 258.00 110.00 3.08 3.21 19.44 1.00 0.00 3.00 1.00
Hornet Sportabout 18.70 8.00 360.00 175.00 3.15 3.44 17.02 0.00 0.00 3.00 2.00
Valiant 18.10 6.00 225.00 105.00 2.76 3.46 20.22 1.00 0.00 3.00 1.00

Use of color palette

You can use color palettes from RColorBrewer packages. You can extract colors from palette by using palette2colors() function.

require(RColorBrewer)
Loading required package: RColorBrewer

reds=palette2colors("Reds")
reds
[1] "#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)
Table 5. Use of color palette
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.10 3.50 1.40 0.20 setosa
2 4.90 3.00 1.40 0.20 setosa
3 4.70 3.20 1.30 0.20 setosa
4 4.60 3.10 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

ztable(head(mtcars),caption="Table 6. Use of color palette(2)") %>%
    addRowColor(bg=palette2colors("Set3"))
Table 6. Use of color palette(2)
  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
Mazda RX4 Wag 21.00 6.00 160.00 110.00 3.90 2.88 17.02 0.00 1.00 4.00 4.00
Datsun 710 22.80 4.00 108.00 93.00 3.85 2.32 18.61 1.00 1.00 4.00 1.00
Hornet 4 Drive 21.40 6.00 258.00 110.00 3.08 3.21 19.44 1.00 0.00 3.00 1.00
Hornet Sportabout 18.70 8.00 360.00 175.00 3.15 3.44 17.02 0.00 0.00 3.00 2.00
Valiant 18.10 6.00 225.00 105.00 2.76 3.46 20.22 1.00 0.00 3.00 1.00

Make a flextable from a ztable

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)
Loading required package: officer
require(flextable)
Loading required package: flextable

ft=regulartable(head(iris))
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.

cgroup=c("Sepal","Petal","Species")
n.cgroup=c(2,2,1)
z <- ztable(head(iris),caption="Table 9. Use of column groups") %>%
    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
Table 9. Use of column groups
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
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

fit <- lm(mpg ~ cyl + disp + wt + drat + am, data=mtcars)
z=ztable(fit,caption="Table 10. Results of Multiple Regression Analysis ")
z
Table 10. Results of Multiple Regression Analysis
  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)
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).

z1=z %>% addSigColor
z1
Table 10. Results of Multiple Regression Analysis
  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)
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.

z2= z %>% addSigColor(level=0.01,bg="yellow",color="red")
z2
Table 10. Results of Multiple Regression Analysis
  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)
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.