├── .Rbuildignore ├── .gitattributes ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── R ├── stylecode.R └── tablecode.R ├── README.md ├── data-raw ├── data.R ├── paintings.R └── paintings.csv ├── data └── paintings.rda ├── man ├── stylecode.Rd └── tablecode.Rd ├── simpletable.Rproj └── vignettes ├── simpletable_III.Rmd ├── simpletable_IV.Rmd ├── simpletable_intro.Rmd └── simpletable_introII.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^data-raw$ 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | inst/doc 5 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: simpletable 2 | Title: Makes simple tables in RMarkdown 3 | Version: 0.0.0.9000 4 | Description: Enables custom style tables to be easily made in RMarkdown 5 | Authors: James P. Curley 6 | Maintainer: James P. Curley 7 | Depends: R (>= 3.1.2) 8 | License: >GPL-3 9 | LazyData: true 10 | Suggests: knitr 11 | URL: https://github.com/jalapic/musicnotationR 12 | BugReports: https://github.com/jalapic/musicnotationR 13 | VignetteBuilder: knitr 14 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2 (4.1.0): do not edit by hand 2 | 3 | export(stylecode) 4 | export(tablecode) 5 | -------------------------------------------------------------------------------- /R/stylecode.R: -------------------------------------------------------------------------------- 1 | #' Defines the css style for a table 2 | #' 3 | #' @param name The unique id for the style to be referenced by \code{tablecode()} 4 | #' @param colh The color of the header 5 | #' @param colh.text The color of the text in the header 6 | #' @param col The background color 7 | #' @param col.text The color of text 8 | #' @param border.style The style of border. Options 9 | #' are \code{none}, \code{hidden}, 10 | #' \code{solid}, \code{dotted}, \code{double}, \code{dashed}, 11 | #' \code{groove}, \code{ridge}, \code{inset}, \code{outset} 12 | #' @param border.col Border color 13 | #' @param border.wt Border weight - needs to be an integer 14 | #' @param font.size a vector of font sizes for i) header ii) cells 15 | #' @param lineheight The height of rows 16 | #' @param hfont.wt The weight of the font in the header 17 | #' options are \code{normal}, \code{bold}, \code{bolder}, \code{lighter}, 18 | #' or can be a number (usually between 100-900, default is 200). 19 | #' @param htext.align The alignment of the header text 20 | #' are \code{left}, \code{center}, 21 | #' \code{right}, \code{justify} 22 | #' @param text.align The alignment of the cell text 23 | #' are \code{left}, \code{center}, 24 | #' \code{right}, \code{justify} 25 | #' @param borderh.style The style of border at bottom of header. Options 26 | #' are same as for \code{border.style} 27 | #' @param borderh.col Border at bottom of header color 28 | #' @param borderh.wt Border at bottom of header weight - needs to be an 29 | #' integer 30 | #' @param borderb.style The style of border at the bottom of cells. Options 31 | #' are same as for \code{border.style} 32 | #' @param borderb.col Border color at bottom of rows 33 | #' @param borderb.wt Border weight at bottom of rows 34 | #' @param pad The padding around borders of cells. 35 | #' @param padh The padding around borders of header. 36 | #' @param collapse The border collapse style - can be 37 | #' \code{collapse}, \code{separate}, \code{initial} 38 | #' @return A css style 39 | #' @examples 40 | #' stylecode(name="uniqueid") 41 | #' tablecode(paintings, tabletype="uniqueid", width="80%") 42 | #' @export 43 | 44 | 45 | stylecode<-function(name, colh="#B8B8B8", colh.text="#111111", col="#F0F0F0", col.text="#111111", 46 | border.col="gray55", border.wt=1, border.style="solid", font.size=c(18,16), lineheight=1.3, 47 | hfont.wt=200, borderh.wt=2, borderh.col="#CC0000", borderh.style="none", text.align="left", 48 | htext.align="left", borderb.wt=1, borderb.col="#CCCCCC", borderb.style="none", 49 | pad=c(3,3,12,3), padh=c(3,3,12,3), collapse="collapse") 50 | { 51 | 52 | cat( 53 | noquote( 54 | paste( 55 | "" 120 | ) 121 | ) 122 | ) 123 | } 124 | -------------------------------------------------------------------------------- /R/tablecode.R: -------------------------------------------------------------------------------- 1 | #' Returns a Simple Table in RMarkdown HTML output 2 | #' 3 | #' @param df The dataframe to turn into a table 4 | #' @param tabletype the name of the table style. The options currently 5 | #' available are \code{minimal}, \code{onecol}, \code{box}, \code{hoverTable}, \code{zebra}, 6 | #' \code{zebra1}, \code{another}, \code{simple}, \code{gridtable} 7 | #' @param width the width of the table 8 | #' @return A table of the desired style 9 | #' @examples 10 | #' tablecode(paintings, tabletype="minimal") 11 | #' @export 12 | 13 | 14 | tablecode <- function(df, tabletype="tabletype", width = "80%"){ 15 | tmp <- noquote(apply(df, 1, function(x) paste0("", x, "", collapse=" "))) 16 | 17 | 18 | if(tabletype=="minimal"){ 19 | 20 | 21 | cat( 22 | c( 23 | noquote(""), 29 | 30 | noquote(paste0(" ")), 31 | noquote(paste0("", paste0("", collapse=" "), "")), 32 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 33 | noquote("
", sprintf("%s",colnames(df)), "
") 34 | ) 35 | ) 36 | } 37 | 38 | ### 39 | 40 | 41 | if(tabletype=="box"){ 42 | 43 | 44 | cat( 45 | c( 46 | noquote(""), 52 | 53 | noquote(paste0(" ")), 54 | noquote(paste0("", paste0("", collapse=" "), "")), 55 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 56 | noquote("
", sprintf("%s",colnames(df)), "
") 57 | ) 58 | ) 59 | } 60 | 61 | ### 62 | 63 | if(tabletype=="onecol"){ 64 | 65 | 66 | cat( 67 | c( 68 | noquote(""), 75 | 76 | noquote(paste0(" ")), 77 | noquote(paste0("", paste0("", collapse=" "), "")), 78 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 79 | noquote("
", sprintf("%s",colnames(df)), "
") 80 | ) 81 | ) 82 | } 83 | 84 | ### 85 | 86 | if(tabletype=="simple"){ 87 | 88 | 89 | cat( 90 | c( 91 | noquote(""), 98 | 99 | noquote(paste0(" ")), 100 | noquote(paste0("", paste0("", collapse=" "), "")), 101 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 102 | noquote("
", sprintf("%s",colnames(df)), "
") 103 | ) 104 | ) 105 | } 106 | 107 | ### 108 | 109 | if(tabletype=="gridtable"){ 110 | 111 | 112 | cat( 113 | c( 114 | noquote(""), 119 | 120 | noquote(paste0(" ")), 121 | noquote(paste0("", paste0("", collapse=" "), "")), 122 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 123 | noquote("
", sprintf("%s",colnames(df)), "
") 124 | ) 125 | ) 126 | } 127 | 128 | 129 | ### 130 | 131 | if(tabletype=="hoverTable"){ 132 | 133 | 134 | cat( 135 | c( 136 | noquote(""), 143 | 144 | noquote(paste0(" ")), 145 | noquote(paste0("", paste0("", collapse=" "), "")), 146 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 147 | noquote("
", sprintf("%s",colnames(df)), "
") 148 | ) 149 | ) 150 | } 151 | 152 | 153 | 154 | ### 155 | 156 | if(tabletype=="flattable3"){ 157 | 158 | 159 | cat( 160 | c( 161 | noquote(""), 172 | 173 | noquote(paste0(" ")), 174 | noquote(paste0("", paste0("", collapse=" "), "")), 175 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 176 | noquote("
", sprintf("%s",colnames(df)), "
") 177 | ) 178 | ) 179 | } 180 | 181 | 182 | ### 183 | 184 | if(tabletype=="another"){ 185 | 186 | 187 | cat( 188 | c( 189 | noquote(""), 195 | 196 | noquote(paste0(" ")), 197 | noquote(paste0("", paste0("", collapse=" "), "")), 198 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 199 | noquote("
", sprintf("%s",colnames(df)), "
") 200 | ) 201 | ) 202 | } 203 | 204 | ### 205 | 206 | if(tabletype=="zebra"){ 207 | 208 | 209 | cat( 210 | c( 211 | noquote(""), 218 | 219 | noquote(paste0(" ")), 220 | noquote(paste0("", paste0("", collapse=" "), "")), 221 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 222 | noquote("
", sprintf("%s",colnames(df)), "
") 223 | ) 224 | ) 225 | } 226 | 227 | 228 | ### 229 | 230 | if(tabletype=="zebra1"){ 231 | 232 | 233 | cat( 234 | c( 235 | noquote(""), 242 | 243 | noquote(paste0(" ")), 244 | noquote(paste0("", paste0("", collapse=" "), "")), 245 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 246 | noquote("
", sprintf("%s",colnames(df)), "
") 247 | ) 248 | ) 249 | } 250 | 251 | 252 | if(tabletype!="zebra" & tabletype!="zebra1" & tabletype!="minimal" & tabletype!="onecol" & tabletype!="box" & 253 | tabletype!="simple" & tabletype!="hoverTable" & tabletype!="another" & tabletype!="gridtable"){ 254 | 255 | cat( 256 | c( 257 | noquote(paste0(" ")), 258 | noquote(paste0("", paste0("", collapse=" "), "")), 259 | noquote(unlist(lapply(tmp, function(x) paste0("", x, "")))), 260 | noquote("
", sprintf("%s",colnames(df)), "
") 261 | ) 262 | ) 263 | 264 | } 265 | 266 | #end 267 | } 268 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simpletable 2 | Making simple tables in RMarkdown 3 | 4 | ### Installation 5 | 6 | ``` 7 | library(devtools) 8 | devtools::install_github("jalapic/simpletable") 9 | ``` 10 | 11 | See here for introduction - http://rpubs.com/jalapic/simpletable 12 | 13 | and here for examples with some larger dataframes - http://rpubs.com/jalapic/simpletable2 14 | 15 | combining with the broom package - http://rpubs.com/jalapic/simpletable4 16 | -------------------------------------------------------------------------------- /data-raw/data.R: -------------------------------------------------------------------------------- 1 | paintings <- read.csv("data-raw/paintings.csv", 2 | stringsAsFactors = FALSE) 3 | devtools::use_data(paintings, overwrite = TRUE) 4 | -------------------------------------------------------------------------------- /data-raw/paintings.R: -------------------------------------------------------------------------------- 1 | paintings <- data.frame( 2 | Painting = c("Nafea Faa Ipoipo", "The Card Players", "Le Reve", "Three Studies of Lucian Freud", "No. 5, 1948"), 3 | Artist = c("Paul Gauguin", "Paul Cezanne", "Pablo Picasso", "Francis Bacon", "Jackson Pollock"), 4 | Year = c(1892, 1892, 1932, 1969, 1948), 5 | Price = c("$300M", "$259M", "$155M", "$142M", "$140M") 6 | ) 7 | 8 | -------------------------------------------------------------------------------- /data-raw/paintings.csv: -------------------------------------------------------------------------------- 1 | "Painting","Artist","Year","Price" 2 | "Nafea Faa Ipoipo","Paul Gauguin",1892,"$300M" 3 | "The Card Players","Paul Cezanne",1892,"$259M" 4 | "Le Reve","Pablo Picasso",1932,"$155M" 5 | "Three Studies of Lucian Freud","Francis Bacon",1969,"$142M" 6 | "No. 5, 1948","Jackson Pollock",1948,"$140M" 7 | -------------------------------------------------------------------------------- /data/paintings.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalapic/simpletable/251a46a347a622ab848c09be667f8ff49596b04f/data/paintings.rda -------------------------------------------------------------------------------- /man/stylecode.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/stylecode.R 3 | \name{stylecode} 4 | \alias{stylecode} 5 | \title{Defines the css style for a table} 6 | \usage{ 7 | stylecode(name, colh = "#B8B8B8", colh.text = "#111111", col = "#F0F0F0", 8 | col.text = "#111111", border.col = "gray55", border.wt = 1, 9 | border.style = "solid", font.size = c(18, 16), lineheight = 1.3, 10 | hfont.wt = 200, borderh.wt = 2, borderh.col = "#CC0000", 11 | borderh.style = "none", text.align = "left", htext.align = "left", 12 | borderb.wt = 1, borderb.col = "#CCCCCC", borderb.style = "none", 13 | pad = c(3, 3, 12, 3), padh = c(3, 3, 12, 3), collapse = "collapse") 14 | } 15 | \arguments{ 16 | \item{name}{The unique id for the style to be referenced by \code{tablecode()}} 17 | 18 | \item{colh}{The color of the header} 19 | 20 | \item{colh.text}{The color of the text in the header} 21 | 22 | \item{col}{The background color} 23 | 24 | \item{col.text}{The color of text} 25 | 26 | \item{border.col}{Border color} 27 | 28 | \item{border.wt}{Border weight - needs to be an integer} 29 | 30 | \item{border.style}{The style of border. Options 31 | are \code{none}, \code{hidden}, 32 | \code{solid}, \code{dotted}, \code{double}, \code{dashed}, 33 | \code{groove}, \code{ridge}, \code{inset}, \code{outset}} 34 | 35 | \item{font.size}{a vector of font sizes for i) header ii) cells} 36 | 37 | \item{lineheight}{The height of rows} 38 | 39 | \item{hfont.wt}{The weight of the font in the header 40 | options are \code{normal}, \code{bold}, \code{bolder}, \code{lighter}, 41 | or can be a number (usually between 100-900, default is 200).} 42 | 43 | \item{borderh.wt}{Border at bottom of header weight - needs to be an 44 | integer} 45 | 46 | \item{borderh.col}{Border at bottom of header color} 47 | 48 | \item{borderh.style}{The style of border at bottom of header. Options 49 | are same as for \code{border.style}} 50 | 51 | \item{text.align}{The alignment of the cell text 52 | are \code{left}, \code{center}, 53 | \code{right}, \code{justify}} 54 | 55 | \item{htext.align}{The alignment of the header text 56 | are \code{left}, \code{center}, 57 | \code{right}, \code{justify}} 58 | 59 | \item{borderb.wt}{Border weight at bottom of rows} 60 | 61 | \item{borderb.col}{Border color at bottom of rows} 62 | 63 | \item{borderb.style}{The style of border at the bottom of cells. Options 64 | are same as for \code{border.style}} 65 | 66 | \item{pad}{The padding around borders of cells.} 67 | 68 | \item{padh}{The padding around borders of header.} 69 | 70 | \item{collapse}{The border collapse style - can be 71 | \code{collapse}, \code{separate}, \code{initial}} 72 | } 73 | \value{ 74 | A css style 75 | } 76 | \description{ 77 | Defines the css style for a table 78 | } 79 | \examples{ 80 | stylecode(name="uniqueid") 81 | tablecode(paintings, tabletype="uniqueid", width="80\%") 82 | } 83 | 84 | -------------------------------------------------------------------------------- /man/tablecode.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/tablecode.R 3 | \name{tablecode} 4 | \alias{tablecode} 5 | \title{Returns a Simple Table in RMarkdown HTML output} 6 | \usage{ 7 | tablecode(df, tabletype = "tabletype", width = "80\%") 8 | } 9 | \arguments{ 10 | \item{df}{The dataframe to turn into a table} 11 | 12 | \item{tabletype}{the name of the table style. The options currently 13 | available are \code{minimal}, \code{onecol}, \code{box}, \code{hoverTable}, \code{zebra}, 14 | \code{zebra1}, \code{another}, \code{simple}, \code{gridtable}} 15 | 16 | \item{width}{the width of the table} 17 | } 18 | \value{ 19 | A table of the desired style 20 | } 21 | \description{ 22 | Returns a Simple Table in RMarkdown HTML output 23 | } 24 | \examples{ 25 | tablecode(paintings, tabletype="minimal") 26 | } 27 | 28 | -------------------------------------------------------------------------------- /simpletable.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /vignettes/simpletable_III.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "simpletable III" 3 | author: "James P. Curley" 4 | date: "Tuesday, April 14, 2015" 5 | output: html_document 6 | --- 7 | 8 |
9 |
10 | 11 | ## Making custom tables 12 | 13 | My R package `simpletable` has a number of default table types that you can turn your dataframe into when using HTML output in RMarkdown. There are several examples [here](http://rpubs.com/jalapic/simpletable) - that link also covers the basics of the `tablecode` function. 14 | 15 | An example, using the `paintings` dataframe: 16 | 17 |
18 | 19 | ```{r, results='asis'} 20 | 21 | library(simpletable) 22 | 23 | tablecode(paintings, tabletype="minimal") 24 | 25 | ``` 26 | 27 |
28 |
29 | 30 | 31 | Now, you may want to do more and actually make your own customized table. You may want to change colors or font size or line heights, or borders etc. My main suggestion would be to learn a bit of css as that way you will be able to get really nice control over all features of your table. However, there are many people who don't want to have to learn css just to make a pretty table. The following how-to guide is probably for you. Here, I am introducing a function called `stylecode` that makes it easy for the average R user to generate customized tables. If you're interested in changing lots about your table the parameters could get quite lengthy! 32 | 33 |
34 |
35 |
36 | 37 | Last thing before we get going - I have very little artistic ability and so my tables don't look that pretty. Admittedly, this is partly because I'm demoing how things can be changed on the fly and so I'm changing things in a strange order. Nevertheless, if anybody manages to come up with some very attractive tables by manipulating the various parameters then please let me know. I'd love to add them to the default styles. 38 | 39 |

40 | 41 | 42 | #### Every style needs a unique identifier 43 | 44 | The function `stylecode` is the function that enables the user to choose how their table will look. By changing the parameters of this function, a user can store for later use a particular table style. The style needs to be named using the `name=` argument. The name given must be unique (i.e. not used elsewhere in the RMarkdown document to define a different style with different parameters). 45 | 46 | 47 | Once the function has been used once, it can be referred to as many times as the user wants within the `tablecode` function. This is particularly useful if wanting to make many tables all with the same style. 48 | 49 | Here is the simplest example. All the parameters of `stylecode` are left to be equal to their default. I have named these defaults as `uniqueid` and then I direct `tablecode` to produce a table of the style `uniqueid`. 50 | 51 |
52 | 53 | ```{r, results='asis'} 54 | 55 | stylecode(name="uniqueid") 56 | tablecode(paintings, tabletype="uniqueid") 57 | 58 | 59 | ``` 60 | 61 | 62 |

63 | 64 | Let's say I wanted to change some things. Here I am changing the color of the header (`colh`), the color of the text in the header (`colh.text`), the background color of all other cells (`col`), the color of the text in the table (`col.text`), the font size of the header and the rest of the table (`font.size`) and the line height of cells (`lineheight`). 65 | 66 | As a result of making all of these changes, I'm giving these sets of parameters a different name `uniqueid1`. 67 | 68 |
69 | 70 | 71 | ```{r, results='asis'} 72 | 73 | stylecode(name="uniqueid1", colh="#FFFF99", colh.text="#000000", col="#FFFFCC", col.text="#111111", border.col="#FFFF99", font.size=c(15,12), lineheight=1) 74 | tablecode(paintings, tabletype="uniqueid1") 75 | 76 | 77 | ``` 78 | 79 |
80 | 81 | You will probably have noticed that I have used [HTML colors](http://www.w3schools.com/html/html_colors.asp) rather than [R Colors](http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf). I think that for the most part, R color names will work, but I have run into some issues with colors not looking right or flat out being ignored (particularly the grays). I recommend using HTML color names/ids. 82 | 83 |
84 |
85 | 86 | 87 | **Remove Border** To remove a border you can change the border style to 'none': 88 | 89 |
90 | 91 | 92 | ```{r, results='asis'} 93 | 94 | stylecode(name="uniqueid2", border.style="none") 95 | tablecode(paintings, tabletype="uniqueid2") 96 | 97 | ``` 98 | 99 |

100 | 101 | 102 | **Add border underneath header** To add a border you can change the border style to e.g. 'solid', 'double', 'dotted', etc. You should also define the color and weight of the border: 103 | 104 |
105 | 106 | 107 | ```{r, results='asis'} 108 | 109 | stylecode(name="uniqueid3", border.style="none", borderh.style="solid") 110 | tablecode(paintings, tabletype="uniqueid3") 111 | 112 | ``` 113 | 114 |

115 | 116 | 117 | Here's another example taken from my [`apportR` package](http://rpubs.com/jalapic/apportRvig) which looks at the apportionment of seats in the House of Representatives: 118 | 119 | 120 | ```{r} 121 | 122 | library(apportR) 123 | 124 | ham <- hamilton(usa1790, 120) #seats per state according to Hamilton's method 125 | jef <- jefferson(usa1790,120)[names(ham)] #seats per state according to Jefferson's method 126 | 127 | df1790 <- data.frame(State=names(usa1790), Population=usa1790, Hamilton = ham, Jefferson = jef) 128 | df1790 129 | ``` 130 | 131 |
132 | 133 | In the following table, I'm reducing the final width to 25%, I'm centering both the header and main text and I'm using [rgba colors](http://www.w3schools.com/cssref/css_colors_legal.asp). These are colors whose first three numbers refer to the RGB color codes, and the fourth number refers to the opacity of the color. Smaller fourth numbers mean more transparency. 134 | 135 | I've also given the whole table a bizarre double border, increased the weight of the text in the header, changed the padding around borders and added a bottom border to each row. 136 | 137 | 138 | 139 | 140 | 141 | ```{r, results='asis'} 142 | 143 | stylecode(name="uniqueid4", colh="rgba(0,0,255,0.2)", colh.text="#000000", col="rgba(0,0,255,0.1)", col.text="#000000", font.size=c(14,12), hfont.wt=900, lineheight=1.5, border.style="double", border.col="#3399FF", borderh.style="solid", borderh.wt=2, borderh.col="#3399FF", htext.align="center", text.align="center", borderb.style="solid", pad=c(3,1,3,3)) 144 | 145 | tablecode(df1790, tabletype="uniqueid4", width="25%") 146 | 147 | ``` 148 | 149 |
150 | 151 | If you can be bothered to make all of these changes, then you might want to think about just using css directly as it gives more precision. Nevertheless, there's a lot that can be done with this function to make nice looking custom tables. 152 | 153 | 154 |


155 | 156 | 157 | ### To add to this vignette: 158 | - vertical alignment 159 | - hovering 160 | - zebra stripes 161 | - column borders 162 | -------------------------------------------------------------------------------- /vignettes/simpletable_IV.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: simpletable intro IV 3 | author: "James P. Curley" 4 | date: "Wednesday, April 15, 2015" 5 | output: html_document 6 | --- 7 | 8 | ### Using simpletable and broom together 9 | 10 | The [`broom`](https://github.com/dgrtwo/broom) package is a great R package for neatly summarizing the results of statistical tests and models. The output of `broom` is always a data.frame and rownames are always included as the first column. This makes them work very seamlessly with `simpletable` if we want to produce an attractive looking table in a RMarkdown document. 11 | 12 |
13 | 14 | For background information on `simpletable` see the introductory vignette [here](https://github.com/dgrtwo/broom) or the github page [here](https://github.com/jalapic/simpletable). 15 | 16 | 17 |
18 |
19 | 20 | 21 | ### Load packages 22 | 23 | ```{r} 24 | library(broom) 25 | library(simpletable) 26 | ``` 27 | 28 | 29 |

30 |
31 | 32 | ### Some examples 33 | 34 | I will first use the `tidy` function in `broom` to make some summary tables of a `lm` and `glm` model. 35 | 36 |

37 | 38 | ```{r} 39 | lmfit <- lm(mpg ~ wt, mtcars) 40 | lmfit 41 | ``` 42 | 43 |
44 | 45 | ```{r} 46 | tidy(lmfit) 47 | ``` 48 | 49 |
50 |
51 | This is how we can make it look using `simpletable` using two default styles: 52 | 53 |
54 |
55 | ```{r, results='asis'} 56 | tablecode(tidy(lmfit), tabletype="minimal") 57 | ``` 58 | 59 |
60 | 61 |
62 | 63 | ```{r, results='asis'} 64 | tablecode(tidy(lmfit), tabletype="box") 65 | ``` 66 |
67 | 68 |
69 | 70 | **Here is another example:** 71 | 72 | 73 | ```{r} 74 | glmfit <- glm(am ~ wt, mtcars, family="binomial") 75 | glmfit 76 | tidy(glmfit) 77 | ``` 78 | 79 |
80 | 81 | 82 | ```{r, results='asis'} 83 | tablecode(tidy(glmfit), tabletype="minimal") 84 | ``` 85 | 86 |
87 | 88 | 89 | ```{r, results='asis'} 90 | tablecode(tidy(glmfit), tabletype="box") 91 | ``` 92 | 93 | 94 |

95 | 96 | #### We can also look at the fitted values and residuals with `augment` 97 | 98 |
99 | 100 | ```{r} 101 | lmfit.a <-augment(lmfit) 102 | lmfit.a[,4:10] <- round(lmfit.a[,4:10],3) 103 | ``` 104 | 105 |
106 | 107 | which can give.. 108 | 109 |
110 |
111 | ```{r, results='asis'} 112 | tablecode(lmfit.a, tabletype="gridtable") 113 | ``` 114 | 115 |
116 |

117 | 118 | 119 | #### Customizing the output 120 | 121 | It's still a work in process, but I'm also enabling the user to customize output of tables to change all sorts of features. This can be done with the `stylecode` function. A vignette is in the works. Here is a brief example: 122 | 123 | 124 | ```{r,results='asis'} 125 | 126 | stylecode(name="uniqueid", colh="rgba(0,0,255,0.2)", colh.text="#000000", col="rgba(0,0,255,0.1)", col.text="#000000", font.size=c(14,12), hfont.wt=900, lineheight=1.5, border.style="none", borderh.style="solid", borderh.wt=2, borderh.col="#3399FF", htext.align="center", text.align="center", borderb.style="solid", pad=c(3,1,3,3)) 127 | 128 | tablecode(lmfit.a, tabletype="uniqueid", width="35%") 129 | 130 | ``` 131 |
132 |
133 | 134 | I still have some features to add to this function such as the ability to manipulate the width of the first column independently of the other columns, but this gives you an idea of what's possible. 135 | 136 |


137 | -------------------------------------------------------------------------------- /vignettes/simpletable_intro.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to simpletable" 3 | author: "James P. Curley" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Vignette Title} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | \usepackage[utf8]{inputenc} 10 | --- 11 | 12 | 13 | ### Background 14 | 15 |
16 | 17 | This is a small R package that makes it easy for the user to produce a simple attractive looking table in a RMarkdown document. It is only intended for use when the output of the RMarkdown document is HTML. 18 | 19 |
20 | 21 | There are many table packages out there that do lots of wonderful things. They all have merits. I think [formattable](https://github.com/renkun-ken/formattable) by Kun Ren is going to be my favorite. I wrote this package to help me make some quick tables in RMarkdown that I found attractive and were better to look at for my collaborators than standard R output. Because it seems others are also interested in table output, then I thought I'd share. 22 | 23 |
24 | 25 | Currently, I only have included one function `tablecode` that enables you to turn any dataframe into a table. Obviously, some common sense is needed in that your dataframe should be a reasonable number of columns and rows such that it would work in a table. There is no point trying to make a table out of 1000 columns and 1000 rows. I would recommend looking at packages like [DT](http://rstudio.github.io/DT/) 26 | 27 | 28 |
29 | 30 | 31 | ### Future Plans 32 | 33 | Currently you can just make simple tables (i.e. no nested tables or split cells). I may extend it to be able to make those kind of tables. Also, the current option is just to pick one of the default themes. I have nearly finished making it such that you can modify the colors, borders, cell heights, font sizes etc. as you like to make tables that you like best. 34 | 35 | 36 |
37 |
38 | 39 | 40 | ### Installation 41 | 42 | You can install from github with `devtools`: 43 | 44 | ```{r, eval=FALSE} 45 | library(devtools) 46 | devtools::install_github("jalapic/simpletable") 47 | ``` 48 | 49 | 50 |
51 |
52 | 53 | 54 | ### Using simpletable 55 | 56 | Once you have a dataframe that you'd like to make into a table, simply use the function: 57 | 58 |
59 | 60 | `tablecode(df, tabletype="minimal")` 61 | 62 |
63 | 64 | You can pick from one of several default tabletypes - they are shown below. The critical thing is that this function must be written inside of a R chunk and you must have `results='asis'` as an option in your R chunk. 65 | 66 |
67 | 68 | Current examples are shown below - note that some change color / font weight as you hover over them: 69 | 70 |
71 | 72 | 73 | ```{r, results='asis'} 74 | 75 | library(simpletable) 76 | 77 | tablecode(paintings, tabletype="minimal") 78 | 79 | ``` 80 | 81 | 82 |
83 |
84 | 85 | ```{r, results='asis'} 86 | tablecode(paintings, tabletype="hoverTable") 87 | ``` 88 | 89 |
90 |
91 | 92 | ```{r, results='asis'} 93 | tablecode(paintings, tabletype="box") 94 | ``` 95 | 96 |
97 |
98 | 99 | ```{r, results='asis'} 100 | tablecode(paintings, tabletype="onecol") 101 | ``` 102 | 103 |
104 |
105 | 106 | ```{r, results='asis'} 107 | tablecode(paintings, tabletype="flattable3") 108 | ``` 109 | 110 |
111 |
112 | 113 | ```{r, results='asis'} 114 | tablecode(paintings, tabletype="zebra") 115 | ``` 116 | 117 |
118 |
119 | 120 | ```{r, results='asis'} 121 | tablecode(paintings, tabletype="zebra1") 122 | ``` 123 |
124 |
125 | 126 | ```{r, results='asis'} 127 | tablecode(paintings, tabletype="gridtable") 128 | ``` 129 |
130 |
131 | 132 | ```{r, results='asis'} 133 | tablecode(paintings, tabletype="another") 134 | ``` 135 | 136 |
137 |
138 | 139 | ```{r, results='asis'} 140 | tablecode(paintings, tabletype="simple") 141 | ``` 142 | 143 |
144 |
145 | 146 | 147 | ### Feedback 148 | 149 | I'd welcome feedback or comments. I don't have much time to work on this - I'm really just doing it for fun, but I'd like to hear from anyone who likes/dislikes it. 150 | 151 | My email is jc3181 AT columbia DOT edu but it might be easier to get in touch on [twitter here](https://twitter.com/jalapic) or on my [github page here](https://github.com/jalapic). 152 | 153 |
154 | -------------------------------------------------------------------------------- /vignettes/simpletable_introII.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to simpletable II" 3 | author: "James P. Curley" 4 | date: "Tuesday, April 14, 2015" 5 | output: html_document 6 | --- 7 | 8 | ### Background 9 | 10 | The first guide to this package can be [found here](http://rpubs.com/jalapic/simpletable). 11 | 12 |
13 |
14 | 15 | 16 | ### Installation 17 | 18 | You can install from github with `devtools`: 19 | 20 | ```{r, eval=FALSE} 21 | library(devtools) 22 | devtools::install_github("jalapic/simpletable") 23 | ``` 24 | 25 | 26 |
27 |
28 | 29 | ### Further Introductory Examples 30 | 31 |
32 | Rownames 33 |
34 | 35 | The rownames of a dataframe won't automatically be included as the first column. To get them to show you need to do something like this: 36 | 37 |
38 |
39 | 40 | 41 | ```{r, echo=F} 42 | library(simpletable) 43 | ``` 44 | 45 | 46 | ```{r, results='asis'} 47 | tablecode(cbind(car=rownames(mtcars), mtcars), tabletype="minimal") 48 | 49 | ``` 50 | 51 |

52 | 53 | 54 | 55 | 56 | ```{r, results='asis'} 57 | tablecode(cbind(car=rownames(mtcars), mtcars), tabletype="gridtable") 58 | ``` 59 | 60 | 61 | 62 |

63 | 64 | ### Soccer Example ! 65 | 66 | Here's an example using data from my [engsoccerdata](https://github.com/jalapic/engsoccerdata). I'm using a function in that package called `maketable` which converts raw soccer scores into an end of season final league standings table. I've happened to pick the final standings for the 1994/95 season for the top tier of English soccer. I then turn that into a more attractive table with `tablecode` from simpletable: 67 | 68 |
69 | 70 | 71 | ```{r, results='asis', warning=FALSE, message=FALSE} 72 | library(engsoccerdata) 73 | library(dplyr) 74 | engsoccerdata2 %>% 75 | filter(Season==1994 & tier==1) %>% 76 | maketable() %>% 77 | as.data.frame() %>% 78 | tablecode(tabletype="another") 79 | 80 | ``` 81 | 82 |


83 | 84 | 85 | 86 | --------------------------------------------------------------------------------