ztable Update

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = NA
)

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

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") 

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") 
ztable(head(mtcars),caption="Table 4. Cars with mpg > 21 ") %>%
    addCellColor(condition=mpg>21,cols=1:2,bg="cyan",color="red") 

Use of color palette

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

require(RColorBrewer)

reds=palette2colors("Reds")
reds

You can use the extracted colors to your ztable.

ztable(head(iris),caption="Table 5. Use of color palette") %>% 
    addColColor(bg=reds)

ztable(head(mtcars),caption="Table 6. Use of color palette(2)") %>%
    addRowColor(bg=palette2colors("Set3"))

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)
require(flextable)

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

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

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
ztable2flextable(z1) %>% autofit()

You can change the significant level and background and font color.

z2= z %>% addSigColor(level=0.01,bg="yellow",color="red")
z2
ztable2flextable(z2) 

For more options of flextable, please read the flextable vignette at https://davidgohel.github.io/flextable/index.html.



Try the ztable package in your browser

Any scripts or data that you put into this service are public.

ztable documentation built on Oct. 23, 2020, 5:58 p.m.