메뉴 건너뛰기

웹에서 하는 R 통계

ztable 패키지 업데이트 안내

2018.05.13 21:14

cardiomoon 조회 수:55

안녕하세요? 문건웅입니다. 제가 만든 ztable 패키지를 업데이트하였습니다. 다음은 vignette 내용입니다.


ztable Update

Keon-Woong Moon

2018-05-13

Introduction

If you are unfamiliar to ztable, please read the ztable vignette: https://cran.r-project.org/web/packages/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)
Welcome to package ztable ver 0.1.9
library(magrittr)
options(ztable.type="html")
z=ztable(head(iris),caption="Table 1. Basic Table")
z
Table 1. Basic Table
 Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.103.501.400.20setosa
24.903.001.400.20setosa
34.703.201.300.20setosa
44.603.101.500.20setosa
55.003.601.400.20setosa
65.403.901.700.40setosa

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.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.103.501.400.20setosa
24.903.001.400.20setosa
34.703.201.300.20setosa
44.603.101.500.20setosa
55.003.601.400.20setosa
65.403.901.700.40setosa

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.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.103.501.400.20setosa
24.903.001.400.20setosa
34.703.201.300.20setosa
44.603.101.500.20setosa
55.003.601.400.20setosa
65.403.901.700.40setosa
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
 mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.006.00160.00110.003.902.6216.460.001.004.004.00
Mazda RX4 Wag21.006.00160.00110.003.902.8817.020.001.004.004.00
Datsun 71022.804.00108.0093.003.852.3218.611.001.004.001.00
Hornet 4 Drive21.406.00258.00110.003.083.2119.441.000.003.001.00
Hornet Sportabout18.708.00360.00175.003.153.4417.020.000.003.002.00
Valiant18.106.00225.00105.002.763.4620.221.000.003.001.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.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.103.501.400.20setosa
24.903.001.400.20setosa
34.703.201.300.20setosa
44.603.101.500.20setosa
55.003.601.400.20setosa
65.403.901.700.40setosa

ztable(head(mtcars),caption="Table 6. Use of color palette(2)") %>%
    addRowColor(bg=palette2colors("Set3"))
Table 6. Use of color palette(2)
 mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.006.00160.00110.003.902.6216.460.001.004.004.00
Mazda RX4 Wag21.006.00160.00110.003.902.8817.020.001.004.004.00
Datsun 71022.804.00108.0093.003.852.3218.611.001.004.001.00
Hornet 4 Drive21.406.00258.00110.003.083.2119.441.000.003.001.00
Hornet Sportabout18.708.00360.00175.003.153.4417.020.000.003.002.00
Valiant18.106.00225.00105.002.763.4620.221.000.003.001.00

Make a flextable from a ztable

You can use ztable for html nad 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.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

You can make a ‘Microsoft Word’ document with this flextable.

read_docx() %>%
    body_add_flextable(ft) %>%
    print(target = "flextable.docx")
[1] "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.LengthSepal.Width Petal.LengthPetal.Width Species
15.103.501.400.20setosa
24.903.000.20setosa
34.703.201.300.20setosa
44.601.500.20setosa
55.003.601.400.20setosa
65.403.901.700.40setosa
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
 EstimateStd. Errort valuePr(>|t|)
(Intercept)41.29647.53845.48< 0.0001
cyl-1.79400.6505-2.760.0105
disp0.00740.01230.600.5546
wt-3.58701.2105-2.960.0064
drat-0.09361.5488-0.060.9523
am0.17301.53000.110.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
 EstimateStd. Errort valuePr(>|t|)
(Intercept)41.29647.53845.48< 0.0001
cyl-1.79400.6505-2.760.0105
disp0.00740.01230.600.5546
wt-3.58701.2105-2.960.0064
drat-0.09361.5488-0.060.9523
am0.17301.53000.110.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
 EstimateStd. Errort valuePr(>|t|)
(Intercept)41.29647.53845.48< 0.0001
cyl-1.79400.6505-2.760.0105
disp0.00740.01230.600.5546
wt-3.58701.2105-2.960.0064
drat-0.09361.5488-0.060.9523
am0.17301.53000.110.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.



번호 제목 글쓴이 날짜 조회 수
공지 웹에서 하는 R 메타분석 [1] cardiomoon 2015.11.11 4010
공지 웹에서 하는 R 통계 게시판입니다. cardiomoon 2015.03.26 1049
82 moonBook 패키지 업데이트 안내 cardiomoon 2018.05.26 102
81 Make a Heatmap Table using ztable cardiomoon 2018.05.23 30
80 moonBook 패키지 업데이트 안내 cardiomoon 2018.05.18 1515
» ztable 패키지 업데이트 안내 cardiomoon 2018.05.13 55
78 pptxList: A Shiny Module for Reproducible Research cardiomoon 2018.03.07 163
77 Nightingale's Rose Plot file cardiomoon 2017.11.16 237
76 Plot for distribution of common statistics and p-value cardiomoon 2017.11.11 322
75 R package gglotAssist cardiomoon 2017.11.11 392
74 설문조사데이터 cardiomoon 2017.10.14 579
73 R에서 데이터 편집을 하자 - editData 패키지 [1] cardiomoon 2017.09.25 961
72 dplyrAssist 패키지 : RStudio Addin으로 dplyr을 쉽게 배우기 [1] cardiomoon 2017.09.03 259
71 "틀리지않는법" 강의슬라이드(문건웅) cardiomoon 2017.08.31 843
70 연속형변수를 기준으로 같은 크기를 갖는 여러 개의 그룹으로 나누기 cardiomoon 2017.06.19 430
69 3D visualization of multiple regression analysis file cardiomoon 2017.06.15 297
68 Visualize multiple regression model cardiomoon 2017.06.12 517
67 Longest common substring cardiomoon 2017.05.29 468
66 파일을 업로드 했는데 예제데이터에서 대체가 안됩니다. [2] guriguribangbang 2017.05.16 595
65 샤이니 앱 : interactive ggplot [1] file cardiomoon 2017.02.28 1183
64 Procedural Programming vs Functional Programming(I) cardiomoon 2017.02.27 138
63 함수곡선 아래의 면적 구하기 cardiomoon 2016.12.22 567