├── .gitignore ├── tests ├── testthat.R └── testthat │ ├── test_star_get_lhs_names.R │ ├── test_star_table_notes.R │ ├── test_star_ncol.R │ ├── test_star_panel.R │ ├── test_lhs_functions.R │ └── test_rhs_functions.R ├── .Rbuildignore ├── vignettes ├── pdf │ └── starpolishr-intro.pdf └── starpolishr-intro.Rmd ├── man ├── is.text.Rd ├── getmode.Rd ├── is.latex.Rd ├── star_tex_start.Rd ├── star_tex_end.Rd ├── starpolishr-package.Rd ├── star_asterisks.Rd ├── star_ncol.Rd ├── get_star_lines.Rd ├── star_sidewaystable.Rd ├── get_star_line_numbers.Rd ├── star_tex_write.Rd ├── star_write_and_compile_pdf.Rd ├── dot-star_rhs_replace.Rd ├── star_si_col.Rd ├── star_notes_tex.Rd ├── star_insert_row.Rd ├── star_get_lhs_names.Rd ├── star_change_felm_rhs_names.Rd ├── star_add_column_numbers.Rd ├── star_rhs_order.Rd ├── star_panel.Rd ├── star_rhs_names.Rd └── star_lhs_names.Rd ├── starpolishr.Rproj ├── R ├── starpolishr_main.R ├── star_write_and_compile_pdf.R ├── star_get_lhs_names.R ├── star_change_felm_rhs_names.R ├── utils.R ├── star_tex_write.R ├── star_table_notes.R ├── star_rhs_order.R ├── star_rhs_names.R ├── star_panel.R ├── star_lhs_names.R └── star_cleaning_functions.R ├── DESCRIPTION ├── NAMESPACE ├── README.md ├── README.Rmd └── README.html /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | *~ 5 | inst/doc 6 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(starpolishr) 3 | 4 | test_check("starpolishr") 5 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | vignettes/pdf 4 | ^README\.Rmd$ 5 | ^README-.*\.png$ 6 | READM* -------------------------------------------------------------------------------- /vignettes/pdf/starpolishr-intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChandlerLutz/starpolishr/HEAD/vignettes/pdf/starpolishr-intro.pdf -------------------------------------------------------------------------------- /man/is.text.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{is.text} 4 | \alias{is.text} 5 | \title{Check if stargazer output is in text form} 6 | \usage{ 7 | is.text(star) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | } 12 | \description{ 13 | Check if stargazer output is in text form 14 | } 15 | -------------------------------------------------------------------------------- /man/getmode.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_lhs_names.R 3 | \name{getmode} 4 | \alias{getmode} 5 | \title{Mode function} 6 | \usage{ 7 | getmode(v) 8 | } 9 | \arguments{ 10 | \item{v}{the vector} 11 | } 12 | \value{ 13 | the mode 14 | } 15 | \description{ 16 | https://www.tutorialspoint.com/r/r_mean_median_mode.htm 17 | } 18 | -------------------------------------------------------------------------------- /man/is.latex.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{is.latex} 4 | \alias{is.latex} 5 | \title{Check if stargazer output is in latex form} 6 | \usage{ 7 | is.latex(star) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | } 12 | \description{ 13 | Check if stargazer output is in latex form 14 | } 15 | -------------------------------------------------------------------------------- /man/star_tex_start.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_tex_write.R 3 | \name{star_tex_start} 4 | \alias{star_tex_start} 5 | \title{Create the start of a tex} 6 | \usage{ 7 | star_tex_start(file) 8 | } 9 | \arguments{ 10 | \item{file}{a \code{string} with the file name} 11 | } 12 | \description{ 13 | Create the start of a tex 14 | } 15 | -------------------------------------------------------------------------------- /starpolishr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | Encoding: UTF-8 9 | 10 | AutoAppendNewline: Yes 11 | StripTrailingWhitespace: Yes 12 | 13 | BuildType: Package 14 | PackageUseDevtools: Yes 15 | PackageInstallArgs: --no-multiarch --with-keep.source 16 | PackageRoxygenize: rd,collate,namespace 17 | -------------------------------------------------------------------------------- /man/star_tex_end.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_tex_write.R 3 | \name{star_tex_end} 4 | \alias{star_tex_end} 5 | \title{Create the end of a tex document} 6 | \usage{ 7 | star_tex_end(file, append = TRUE) 8 | } 9 | \arguments{ 10 | \item{file}{a \code{character} string with the file name} 11 | 12 | \item{append}{append to the end of a tex file; defaults to TRUE} 13 | } 14 | \description{ 15 | Create the end of a tex document 16 | } 17 | -------------------------------------------------------------------------------- /man/starpolishr-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/starpolishr_main.R 3 | \docType{package} 4 | \name{starpolishr-package} 5 | \alias{starpolishr} 6 | \alias{starpolishr-package} 7 | \title{Polish and manipulate stargazer output for latex files} 8 | \description{ 9 | starpolish allows you to manipulate stargazer output for latex files 10 | } 11 | \author{ 12 | \strong{Maintainer}: Chandler Lutz \email{chandler.lutz@gmail.com} 13 | 14 | } 15 | -------------------------------------------------------------------------------- /man/star_asterisks.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_cleaning_functions.R 3 | \name{star_asterisks} 4 | \alias{star_asterisks} 5 | \title{A function to replace asterisks with latex compatible values} 6 | \usage{ 7 | star_asterisks(star) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | } 12 | \value{ 13 | updated stargazer output with latex compaitble 14 | asterisks 15 | } 16 | \description{ 17 | A function to replace asterisks with latex compatible values 18 | } 19 | -------------------------------------------------------------------------------- /R/starpolishr_main.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/starpolishr_main.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | #' Polish and manipulate stargazer output for latex files 8 | #' 9 | #' starpolish allows you to manipulate stargazer output for latex files 10 | #' @importFrom magrittr %>% 11 | "_PACKAGE" 12 | 13 | ##For magrittr dot. From https://github.com/tidyverse/magrittr/issues/29 14 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) 15 | 16 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: starpolishr 2 | Title: polish stargazer (latex) files 3 | Version: 0.0.0.9007 4 | Authors@R: person("Chandler", "Lutz", email = "chandler.lutz@gmail.com", role = c("aut", "cre")) 5 | Description: A set of useful functions for polishing stargazer (latex) files. 6 | Depends: 7 | R (>= 3.3.2) 8 | License: GPL-2 9 | Encoding: UTF-8 10 | LazyData: true 11 | Imports: 12 | stargazer, 13 | magrittr, 14 | stringr 15 | RoxygenNote: 7.3.2 16 | Suggests: 17 | testthat, 18 | knitr, 19 | rmarkdown, 20 | lfe, 21 | tools 22 | VignetteBuilder: knitr 23 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(star_add_column_numbers) 4 | export(star_asterisks) 5 | export(star_change_felm_rhs_names) 6 | export(star_get_lhs_names) 7 | export(star_insert_row) 8 | export(star_lhs_names) 9 | export(star_ncol) 10 | export(star_notes_tex) 11 | export(star_panel) 12 | export(star_rhs_names) 13 | export(star_rhs_order) 14 | export(star_si_col) 15 | export(star_sidewaystable) 16 | export(star_tex_end) 17 | export(star_tex_start) 18 | export(star_tex_write) 19 | export(star_write_and_compile_pdf) 20 | importFrom(magrittr,"%>%") 21 | -------------------------------------------------------------------------------- /man/star_ncol.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_cleaning_functions.R 3 | \name{star_ncol} 4 | \alias{star_ncol} 5 | \title{get the number of stargazer columns} 6 | \usage{ 7 | star_ncol(star) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | } 12 | \value{ 13 | the number of columns in the \code{stargazer} output 14 | } 15 | \description{ 16 | get the number of stargazer columns 17 | } 18 | \examples{ 19 | library(stargazer) 20 | data(mtcars) 21 | star.out <- stargazer(mtcars) 22 | star_ncol(star.out) 23 | } 24 | -------------------------------------------------------------------------------- /man/get_star_lines.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{get_star_lines} 4 | \alias{get_star_lines} 5 | \title{get the star lines from \code{stargazer} output} 6 | \usage{ 7 | get_star_lines(star, reg) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} ouput} 11 | 12 | \item{reg}{(logical) set to TRUE for a regression} 13 | } 14 | \value{ 15 | a list with the \code{start.lines}, 16 | \code{main.lines}, and \code{end.lines} of the \code{stargazer} 17 | output 18 | } 19 | \description{ 20 | This function will return a list 21 | } 22 | -------------------------------------------------------------------------------- /man/star_sidewaystable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_cleaning_functions.R 3 | \name{star_sidewaystable} 4 | \alias{star_sidewaystable} 5 | \title{Turn a latex table into a \code{sidewaystable} latex table} 6 | \usage{ 7 | star_sidewaystable(star) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | } 12 | \value{ 13 | the latex table turned sideways 14 | } 15 | \description{ 16 | Turn a latex table into a \code{sidewaystable} latex table 17 | } 18 | \examples{ 19 | library(stargazer) 20 | data(mtcars) 21 | star.out <- stargazer(mtcars) 22 | star_sidewaystable(star.out) 23 | } 24 | -------------------------------------------------------------------------------- /man/get_star_line_numbers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{get_star_line_numbers} 4 | \alias{get_star_line_numbers} 5 | \title{Get \code{stargazer} line numbers by section} 6 | \usage{ 7 | get_star_line_numbers(star, reg = TRUE) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | 12 | \item{reg}{if the stargazer is a regression} 13 | } 14 | \value{ 15 | a list with the line number for the \code{start.lines}, 16 | \code{main.lines}, and \code{end.lines} of the \code{stargazer} 17 | output 18 | } 19 | \description{ 20 | To get the start line numbers, main line numbers, and end line numbers 21 | of \code{stargazer} output 22 | } 23 | -------------------------------------------------------------------------------- /tests/testthat/test_star_get_lhs_names.R: -------------------------------------------------------------------------------- 1 | library(stargazer); 2 | 3 | library(stargazer); library(magrittr) 4 | 5 | data(mtcars) 6 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 7 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 8 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 9 | 10 | expect_equal(star_get_lhs_names(stargazer(mod.mtcars.1)), "mpg") 11 | expect_equal(star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.2)), 12 | "\\multicolumn{2}{c}{mpg}") 13 | expect_equal(star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3)), 14 | c("\\multicolumn{2}{c}{mpg}", "hp")) 15 | expect_equal(star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.3, mod.mtcars.2)), 16 | c("mpg", "hp", "mpg")) 17 | 18 | 19 | -------------------------------------------------------------------------------- /man/star_tex_write.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_tex_write.R 3 | \name{star_tex_write} 4 | \alias{star_tex_write} 5 | \title{Write a stargazer output to a tex file} 6 | \usage{ 7 | star_tex_write(..., starlist = NULL, file, headers = FALSE, append = FALSE) 8 | } 9 | \arguments{ 10 | \item{...}{stargazer ouptut} 11 | 12 | \item{starlist}{a list of stargazer output to write to a file} 13 | 14 | \item{file}{where the file will be saved} 15 | 16 | \item{headers}{(logical) to include the start of a tex afile} 17 | 18 | \item{append}{logical to append at the end of a tex file; defaults 19 | to \code{FALSE}} 20 | } 21 | \description{ 22 | Write a stargazer output to a tex file 23 | } 24 | -------------------------------------------------------------------------------- /man/star_write_and_compile_pdf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_write_and_compile_pdf.R 3 | \name{star_write_and_compile_pdf} 4 | \alias{star_write_and_compile_pdf} 5 | \title{Write to a file and compile to pdf} 6 | \usage{ 7 | star_write_and_compile_pdf(..., starlist = NULL, file) 8 | } 9 | \arguments{ 10 | \item{...}{stargazer ouptut} 11 | 12 | \item{starlist}{a list of stargazer output to write to a file} 13 | 14 | \item{file}{where the file will be saved} 15 | } 16 | \description{ 17 | \code{star_write_and_pdf_compile} will compile the table as a pdf 18 | and then write using \code{star_tex_write} without headers, so that 19 | table can be directly imported into a latex file 20 | } 21 | \seealso{ 22 | \code{star_tex_write} 23 | } 24 | -------------------------------------------------------------------------------- /man/dot-star_rhs_replace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_rhs_names.R 3 | \name{.star_rhs_replace} 4 | \alias{.star_rhs_replace} 5 | \title{Helper function for star_rhs_names} 6 | \usage{ 7 | .star_rhs_replace(star, pattern, line1, line2) 8 | } 9 | \arguments{ 10 | \item{star}{the stargazer output} 11 | 12 | \item{pattern}{the regex pattern} 13 | 14 | \item{line1}{the first line for the variable name} 15 | 16 | \item{line2}{the second line for the variable name} 17 | } 18 | \value{ 19 | the update stargazer output 20 | } 21 | \description{ 22 | A helper function to replace the names of the stargazer variables 23 | and possibly add a second line on the standard error. 24 | This function works with regression only 25 | } 26 | -------------------------------------------------------------------------------- /tests/testthat/test_star_table_notes.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/tests/testthat/test_star_table_notes.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-21 6 | 7 | 8 | context("test star_notes_caption() function") 9 | 10 | ##Clear the workspace 11 | rm(list = ls()) 12 | 13 | ##For star_table notes 14 | 15 | 16 | 17 | library(stargazer) 18 | data(mtcars) 19 | ##First set up models without weight 20 | mod.mtcars.1 <- lm(mpg ~ hp, mtcars) 21 | mod.mtcars.2 <- lm(mpg ~ hp + cyl, mtcars) 22 | star.out <- capture.output(stargazer(mod.mtcars.1, mod.mtcars.2)) 23 | star.out <- star_notes_tex(star.out, note.type = "caption", "custom note") 24 | 25 | test_that("star_notes_tex() works", { 26 | expect_is(star_notes_tex(star.out, note.type = "caption", "custom note"), "character") 27 | }) 28 | 29 | -------------------------------------------------------------------------------- /man/star_si_col.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_cleaning_functions.R 3 | \name{star_si_col} 4 | \alias{star_si_col} 5 | \title{A function to for scientific unit columns from the latex 6 | \code{siunitx} package} 7 | \usage{ 8 | star_si_col(table.format, rep.times = 1) 9 | } 10 | \arguments{ 11 | \item{table.format}{the latex \code{siunitx}. For example \code{3.2} 12 | is three integers and two decimals. \code{table.format} can be a 13 | string or numeric.} 14 | 15 | \item{rep.times}{the number of times to repeat the given column} 16 | } 17 | \description{ 18 | The latex \code{siunitx} package has to be loaded in the preamble. 19 | See this answer on tex stackexchange: 20 | \url{http://tex.stackexchange.com/a/2747} This function is useful 21 | with \code{paste0} when updating column to work \code{siunitx} and 22 | aligning decimals. 23 | } 24 | -------------------------------------------------------------------------------- /tests/testthat/test_star_ncol.R: -------------------------------------------------------------------------------- 1 | 2 | context("star_ncol function") 3 | 4 | ##Clear the workspace 5 | rm(list = ls()) 6 | 7 | library(stargazer); library(magrittr) 8 | 9 | data(mtcars) 10 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 11 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 12 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 13 | ##latex example 14 | star.out.latex <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 15 | type = "latex") 16 | 17 | ##the original, expected number of latex columns 18 | star.ncol.expected <- 4 19 | 20 | test_that("star_ncol returns the correct number of columns", 21 | expect_equal(star_ncol(star.out.latex), star.ncol.expected) 22 | ) 23 | 24 | ##Add an ampersand (&) side to one of the labels 25 | star.out.latex <- star.out.latex %>% 26 | sub("Constant", "Intercept \\\\& Constant", x = .) 27 | 28 | test_that( 29 | "star_ncol returns correct number of cols when there is an escaped & in the table", 30 | expect_equal(star_ncol(star.out.latex), star.ncol.expected) 31 | ) 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /man/star_notes_tex.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_table_notes.R 3 | \name{star_notes_tex} 4 | \alias{star_notes_tex} 5 | \title{\code{stargazer} tex table notes} 6 | \usage{ 7 | star_notes_tex(star, note.type = c("caption", "threeparttable"), note) 8 | } 9 | \arguments{ 10 | \item{star}{\code{stargazer} output} 11 | 12 | \item{note.type}{the type of latex note to be used. Options include 13 | \code{"caption"} for use with the latex caption package and 14 | \code{"threeparttable"} for use with the latex threeparttable 15 | package. Only the first element of type will be used} 16 | 17 | \item{note}{the note to be added to the latex table} 18 | } 19 | \description{ 20 | \code{stargazer} tex table notes 21 | } 22 | \examples{ 23 | ## -- Regression Example -- ## 24 | library(stargazer) 25 | data(mtcars) 26 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 27 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 28 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 29 | ##latex example 30 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 31 | type = "latex") 32 | print(star.out) 33 | ##use latex caption package 34 | star_notes_tex(star.out, note = "my table notes") 35 | ##use latex caption package 36 | star_notes_tex(star.out, note.type = "caption", note = "my table notes") 37 | ##use latex threeparttable package 38 | star_notes_tex(star.out, note.type = "threeparttable", note = "my table notes") 39 | } 40 | -------------------------------------------------------------------------------- /tests/testthat/test_star_panel.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/tests/testthat/test_star_panel.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-21 6 | 7 | 8 | context("test star_panel() function") 9 | 10 | library(stargazer) 11 | data(mtcars) 12 | ##First set up models without weight 13 | mod.mtcars.1 <- lm(mpg ~ hp, mtcars) 14 | mod.mtcars.2 <- lm(mpg ~ hp + cyl, mtcars) 15 | star.out.1 <- capture.output(stargazer(mod.mtcars.1, mod.mtcars.2, keep.stat = "n")) 16 | ##Second set of models with weight as a regressor 17 | mod.mtcars.3 <- lm(mpg ~ hp + wt, mtcars) 18 | mod.mtcars.4 <- lm(mpg ~ hp + cyl + wt, mtcars) 19 | star.out.2 <- capture.output(stargazer(mod.mtcars.1, mod.mtcars.2, keep.stat = "n")) 20 | 21 | 22 | 23 | ##stargazer panel 24 | star.panel.out <- star_panel(star.out.1, star.out.2, 25 | panel.names = c("Without Weight", "With Weight") 26 | ) 27 | 28 | test_that("star_panel() function works", { 29 | expect_is(star.panel.out, "character") 30 | }) 31 | 32 | 33 | ## Test that star_panel() works with a list 34 | star.panel.out <- star_panel(starlist = list(star.out.1, star.out.2), 35 | panel.names = c("Without Weight", "With Weight") 36 | ) 37 | 38 | test_that("star_panel() function works iwth a list", { 39 | expect_is(star.panel.out, "character") 40 | }) 41 | -------------------------------------------------------------------------------- /man/star_insert_row.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_cleaning_functions.R 3 | \name{star_insert_row} 4 | \alias{star_insert_row} 5 | \title{Insert a row in a stargazer table} 6 | \usage{ 7 | star_insert_row(star, string, insert.after) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | 12 | \item{string}{the string of text to insert into the table} 13 | 14 | \item{insert.after}{insert \code{string} after 15 | \code{inster.after}. \code{insert.after} can be a vector, so 16 | you can insert multiple strings at one time. If 17 | \code{insert.after} has length 1, then all elements of 18 | \code{string} are insterted after \code{insert.after}.} 19 | } 20 | \value{ 21 | the updated \code{stargazer} output 22 | } 23 | \description{ 24 | Insert a row in a stargazer table 25 | } 26 | \examples{ 27 | ## -- Regression Example -- ## 28 | library(stargazer) 29 | data(mtcars) 30 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 31 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 32 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 33 | ##latex example 34 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 35 | type = "latex") 36 | print(star.out) 37 | star_insert_row(star.out, "Controls? & No & No & No \\\\\\\\", insert.after = 27) 38 | star_insert_row(star.out, 39 | c("Controls? & No & No & No \\\\\\\\", 40 | "Linear Model? & Yes & Yes & Yes \\\\\\\\"), 41 | insert.after = c(27, 32)) 42 | } 43 | -------------------------------------------------------------------------------- /R/star_write_and_compile_pdf.R: -------------------------------------------------------------------------------- 1 | 2 | #' Write to a file and compile to pdf 3 | #' 4 | #' \code{star_write_and_pdf_compile} will compile the table as a pdf 5 | #' and then write using \code{star_tex_write} without headers, so that 6 | #' table can be directly imported into a latex file 7 | #' 8 | #' @seealso \code{star_tex_write} 9 | #' @param ... stargazer ouptut 10 | #' @param starlist a list of stargazer output to write to a file 11 | #' @param file where the file will be saved 12 | #' @export 13 | star_write_and_compile_pdf <- function(..., starlist = NULL, file) { 14 | 15 | ##Get all of the output in a list 16 | starlist <- c(list(...), starlist) 17 | 18 | ## https://stackoverflow.com/a/54082833/1317443 19 | ## https://stackoverflow.com/a/32259986/1317443 20 | tex.dir <- gsub("(.*/)[^/]+$", "\\1", file) 21 | 22 | file.no.path <- gsub(".*/([^/]+)$", "\\1", file) 23 | file.no.path.or.ext <- gsub(".tex", "", file.no.path) 24 | 25 | ## Write with headers so we can compile to pdf 26 | star_tex_write(starlist = starlist, file = file, headers = TRUE) 27 | 28 | ##Get the original working directory so we can reset it later 29 | orig.wd <- getwd() 30 | 31 | setwd(tex.dir) 32 | tools::texi2pdf(file, quiet = FALSE) 33 | ##remove the extra tex file 34 | files.to.remove <- paste0(file.no.path.or.ext, c(".log", ".aux")) 35 | file.remove(files.to.remove) 36 | setwd(orig.wd) 37 | 38 | ## Write without headers so the table can be inserted directly 39 | ## into a latex document 40 | star_tex_write(starlist = starlist, file = file, headers = FALSE) 41 | 42 | return() 43 | } 44 | 45 | -------------------------------------------------------------------------------- /man/star_get_lhs_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_get_lhs_names.R 3 | \name{star_get_lhs_names} 4 | \alias{star_get_lhs_names} 5 | \title{Extract Left-Hand Side Variable Names from a Stargazer Table} 6 | \usage{ 7 | star_get_lhs_names(star) 8 | } 9 | \arguments{ 10 | \item{star}{A character vector representing the lines of a LaTeX table 11 | generated by `stargazer`. This is typically obtained by reading the 12 | output of `stargazer` into R using `readLines()`.} 13 | } 14 | \value{ 15 | A character vector containing the names of the left-hand side 16 | variables. 17 | } 18 | \description{ 19 | This function extracts the names of the left-hand side (dependent) variables 20 | from a `stargazer` generated LaTeX table. It assumes the default 21 | `stargazer` table format where the dependent variable names are listed 22 | below the "Dependent variable" line. 23 | } 24 | \details{ 25 | This function relies on the specific formatting of default 26 | `stargazer` tables. If you have customized the table's appearance 27 | (e.g., by adding or removing rows, or changing the labels), this function 28 | may not work correctly. It also assumes that the table is a regression 29 | table and not another type of table. It will only work if the table 30 | includes the line "Dependent variable". 31 | } 32 | \examples{ 33 | library(stargazer) 34 | 35 | data(mtcars) 36 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 37 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 38 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 39 | 40 | star_get_lhs_names(stargazer(mod.mtcars.1)) 41 | star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.2)) 42 | star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3)) 43 | } 44 | -------------------------------------------------------------------------------- /man/star_change_felm_rhs_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_change_felm_rhs_names.R 3 | \name{star_change_felm_rhs_names} 4 | \alias{star_change_felm_rhs_names} 5 | \title{To change the RHS variable names of an felm model} 6 | \usage{ 7 | star_change_felm_rhs_names(felm.model, old, new) 8 | } 9 | \arguments{ 10 | \item{felm.model}{An \code{felm} model from the \code{lfe} package} 11 | 12 | \item{old}{A character vector of the original rhs names} 13 | 14 | \item{new}{A character vector of the new rhs names} 15 | } 16 | \value{ 17 | the \code{felm} model with the rhs names 18 | } 19 | \description{ 20 | To change the right-hand-side (rhs) variable names of an felm 21 | model. This is often useful when you want display instrumental 22 | variable regressions on the same row as ols regressions (see 23 | examples). 24 | } 25 | \examples{ 26 | library(stargazer); library(lfe) 27 | data(mtcars) 28 | 29 | # -- Trivial example -- # 30 | mod <- felm(mpg ~ hp + wt, data = mtcars) 31 | stargazer(mod, type = "text") 32 | # Rename the rhs variables and re-print the stargazer table 33 | mod <- star_change_felm_rhs_names(mod, old = c("hp", "wt"), new = c("x1", "x2")) 34 | stargazer(mod, type = "text") 35 | 36 | # -- Combining OLS and IV on a single line using stargazer -- # 37 | mod.ols <- felm(mpg ~ hp, data = mtcars) 38 | mod.iv <- felm(mpg ~ 1 | 0 | (hp ~ cyl), data = mtcars) 39 | 40 | # Original stargazer output 41 | # Notice how the two `hp` coefficients are on different lines 42 | stargazer(mod.ols, mod.iv, type = "text", keep.stat = "n") 43 | 44 | # In the IV model, replace the RHS var name to just "hp" 45 | mod.iv <- star_change_felm_rhs_names(mod.iv, old = "`hp(fit)`", new = "hp") 46 | # Reprint the stargazer output, notice how the ols and iv rhs variables 47 | # are now in the same row 48 | stargazer(mod.ols, mod.iv, type = "text", keep.stat = "n") 49 | } 50 | -------------------------------------------------------------------------------- /man/star_add_column_numbers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_cleaning_functions.R 3 | \name{star_add_column_numbers} 4 | \alias{star_add_column_numbers} 5 | \title{Insert column numbrers in a table} 6 | \usage{ 7 | star_add_column_numbers( 8 | star, 9 | insert.after, 10 | skip.col.1 = TRUE, 11 | add.space = FALSE, 12 | multicol = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{star}{the \code{stargazer} output} 17 | 18 | \item{insert.after}{insert the column numbers after 19 | \code{inster.after}} 20 | 21 | \item{skip.col.1}{skip the first column? If \code{TRUE} no column 22 | number will be placed after the first column} 23 | 24 | \item{add.space}{add space before the previous column? Defaults to \code{FALSE}} 25 | 26 | \item{multicol}{if not \code{NULL}, multicolumn labels will be 27 | used. Options are "l" for left, "c" for center, and "r" for 28 | right. A character vector can be used for different alignments 29 | across different columns. If a single character is used, that 30 | alignment will be used for all elements in pattern. If a latex 31 | multicol is already applied by stargazer, \code{multicol} will 32 | be ignored for that variable.} 33 | } 34 | \value{ 35 | an updated version of the stargazer table with column numbers 36 | } 37 | \description{ 38 | Currently this only works for latex. This function is helpful when 39 | creating a stargazer table from matrix output as stargazer does not 40 | automatically use column numbers for matrices 41 | } 42 | \examples{ 43 | library(stargazer) 44 | data(mtcars) 45 | star.out <- stargazer(as.matrix(head(mtcars))) 46 | print(star.out) 47 | ##Insert column number using default values 48 | star_add_column_numbers(star.out, insert.after = 10) 49 | ##Do not skip the first column when creating column numbers 50 | star_add_column_numbers(star.out, insert.after = 10, skip.col.1 = FALSE) 51 | ##Add a space before the previous column 52 | star_add_column_numbers(star.out, insert.after = 10, add.space = TRUE) 53 | ##Use multicolumns in latex. Could also pass a vector of columns 54 | star_add_column_numbers(star.out, insert.after = 10, multicol = "c") 55 | ##Use multicolumn and don't skip the first column 56 | star_add_column_numbers(star.out, insert.after = 10, skip.col.1 = FALSE, multicol = "c") 57 | } 58 | -------------------------------------------------------------------------------- /R/star_get_lhs_names.R: -------------------------------------------------------------------------------- 1 | #' Extract Left-Hand Side Variable Names from a Stargazer Table 2 | #' 3 | #' This function extracts the names of the left-hand side (dependent) variables 4 | #' from a `stargazer` generated LaTeX table. It assumes the default 5 | #' `stargazer` table format where the dependent variable names are listed 6 | #' below the "Dependent variable" line. 7 | #' 8 | #' @param star A character vector representing the lines of a LaTeX table 9 | #' generated by `stargazer`. This is typically obtained by reading the 10 | #' output of `stargazer` into R using `readLines()`. 11 | #' 12 | #' @return A character vector containing the names of the left-hand side 13 | #' variables. 14 | #' 15 | #' @details This function relies on the specific formatting of default 16 | #' `stargazer` tables. If you have customized the table's appearance 17 | #' (e.g., by adding or removing rows, or changing the labels), this function 18 | #' may not work correctly. It also assumes that the table is a regression 19 | #' table and not another type of table. It will only work if the table 20 | #' includes the line "Dependent variable". 21 | #' 22 | #' @examples 23 | #' library(stargazer) 24 | #' 25 | #' data(mtcars) 26 | #' mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 27 | #' mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 28 | #' mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 29 | #' 30 | #' star_get_lhs_names(stargazer(mod.mtcars.1)) 31 | #' star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.2)) 32 | #' star_get_lhs_names(stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3)) 33 | #' @export 34 | star_get_lhs_names <- function(star) { 35 | 36 | if (!is.latex(star)) 37 | stop("Error: `star_get_lhs_names` only works on stargazer regression latex tables") 38 | 39 | ## Assume that the lhs vars are right below the "Dependent variable" line 40 | dep.var.row <- grep("Dependent variable", star) 41 | 42 | lhs.vars <- star[c(dep.var.row + 2)] %>% 43 | gsub("\\\\\\\\", "", x = .) %>% 44 | sub("\\[-1.8ex\\]", "", x = .) %>% 45 | strsplit(x = ., "&") %>% 46 | unlist(.) %>% 47 | stringr::str_trim() %>% 48 | .[2:length(.)] 49 | 50 | if (length(lhs.vars) > (star_ncol(star) - 1)) 51 | stop("Error: In `star_get_lhs_names`, the length of the LHS variables greater than `star_ncol(star) - 1`. Are you sure you are passing a default stargazer latex regression table to `star_get_lhs_names`") 52 | 53 | return(lhs.vars) 54 | 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | starpolishr 4 | =========== 5 | 6 | This package allows for easy post polishing of latex [stargazer](https://cran.r-project.org/web/packages/stargazer/index.html) output. See [this vignette](https://github.com/ChandlerLutz/starpolishr/blob/master/vignettes/pdf/starpolishr-intro.pdf) for a quick introduction. For examples, see the `Tables` section of my papers [here](https://chandlerlutz.github.io/pdf/california-foreclosure-prevention-laws.pdf) and [here](https://chandlerlutz.github.io/pdf/local-labor-markets-canada-us.pdf). All of these tables were generated exclusively within R using `stargazer` and `starpolishr`. 7 | 8 | Installation 9 | ------------ 10 | 11 | You can install starpolishr from github with: 12 | 13 | ``` r 14 | # install.packages("devtools") 15 | devtools::install_github("ChandlerLutz/starpolishr") 16 | ``` 17 | 18 | Quick Tips 19 | ---------- 20 | 21 | - The first argument in all `starpolishr` functions is a `stargazer` table, allowing for easy compatibility with the [magrittr](https://cran.r-project.org/web/packages/magrittr/index.html) `%>%`. 22 | - All of the functions in the `starpolishr` package begin with `star_` for easy `tab` completion in emacs or rstudio. 23 | 24 | Key Functions 25 | ------------- 26 | 27 | Here is a list of key `starpolishr` functions in order of how often I use them. See their help files for more details. 28 | 29 | - `star_tex_notes` -- Adds custom notes using either the latex [caption](https://www.ctan.org/pkg/caption) or the latex [threeparttable](https://www.ctan.org/pkg/threeparttable). 30 | - `star_tex_write` -- writes latex tables to a file and optionally adds header files and common packages 31 | - `star_lhs_names` and `star_rhs_names` -- updates variable names using regular expressions. The advantage of these functions is that they allow for variable names to span more than one line. 32 | - `star_panel` -- panels multiple related `stargazer` tables into a single latex table. 33 | - `star_insert_row` -- insert a row after a given row in a `stargazer` table 34 | - `star_sidewaystable` -- converts a latex table to a latex `sidewaystable` 35 | - `star_add_column_numbers` -- if you pass a matrix to `stargazer`, it won't add column numbers. This function will add column numbers to a latex table. 36 | 37 | Authors 38 | ------- 39 | 40 | [Chandler Lutz](https://chandlerlutz.github.io/) 41 | 42 | License 43 | ------- 44 | 45 | GPL (>= 2) 46 | -------------------------------------------------------------------------------- /R/star_change_felm_rhs_names.R: -------------------------------------------------------------------------------- 1 | #' To change the RHS variable names of an felm model 2 | #' 3 | #' To change the right-hand-side (rhs) variable names of an felm 4 | #' model. This is often useful when you want display instrumental 5 | #' variable regressions on the same row as ols regressions (see 6 | #' examples). 7 | #' 8 | #' @param felm.model An \code{felm} model from the \code{lfe} package 9 | #' @param old A character vector of the original rhs names 10 | #' @param new A character vector of the new rhs names 11 | #' @return the \code{felm} model with the rhs names 12 | #' @examples 13 | #' library(stargazer); library(lfe) 14 | #' data(mtcars) 15 | #' 16 | #' # -- Trivial example -- # 17 | #' mod <- felm(mpg ~ hp + wt, data = mtcars) 18 | #' stargazer(mod, type = "text") 19 | #' # Rename the rhs variables and re-print the stargazer table 20 | #' mod <- star_change_felm_rhs_names(mod, old = c("hp", "wt"), new = c("x1", "x2")) 21 | #' stargazer(mod, type = "text") 22 | #' 23 | #' # -- Combining OLS and IV on a single line using stargazer -- # 24 | #' mod.ols <- felm(mpg ~ hp, data = mtcars) 25 | #' mod.iv <- felm(mpg ~ 1 | 0 | (hp ~ cyl), data = mtcars) 26 | #' 27 | #' # Original stargazer output 28 | #' # Notice how the two `hp` coefficients are on different lines 29 | #' stargazer(mod.ols, mod.iv, type = "text", keep.stat = "n") 30 | #' 31 | #' # In the IV model, replace the RHS var name to just "hp" 32 | #' mod.iv <- star_change_felm_rhs_names(mod.iv, old = "`hp(fit)`", new = "hp") 33 | #' # Reprint the stargazer output, notice how the ols and iv rhs variables 34 | #' # are now in the same row 35 | #' stargazer(mod.ols, mod.iv, type = "text", keep.stat = "n") 36 | #' @export 37 | star_change_felm_rhs_names <- function(felm.model, old, new) { 38 | 39 | if (class(felm.model) != "felm") 40 | stop("felm.model must be an felm model") 41 | 42 | if (is.null(old) || is.null(old)) 43 | stop("Both `old` and `new` must be set") 44 | 45 | if (!is.character(old) | !is.character(new)) 46 | stop("Both `old` and `new` must be character vectors") 47 | 48 | if (length(old) != length(new)) 49 | stop("`old` and `new` must have the same length") 50 | 51 | felm.x.var.names <- rownames(felm.model$beta) 52 | new.felm.x.var.names <- replace(felm.x.var.names, 53 | felm.x.var.names %in% old, new) 54 | rownames(felm.model$beta) <- new.felm.x.var.names 55 | rownames(felm.model$coefficients) <- new.felm.x.var.names 56 | 57 | return(felm.model) 58 | 59 | } 60 | -------------------------------------------------------------------------------- /man/star_rhs_order.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_rhs_order.R 3 | \name{star_rhs_order} 4 | \alias{star_rhs_order} 5 | \title{Manipulate the order of right-hand side variables using regex} 6 | \usage{ 7 | star_rhs_order(star, pattern, reg = TRUE) 8 | } 9 | \arguments{ 10 | \item{star}{the stargazer table} 11 | 12 | \item{pattern}{a vector of regular expressions which 13 | the order with which the right-hand-side variables will 14 | be used} 15 | 16 | \item{reg}{(logical) set to TRUE for a regression; defaults to 17 | \code{TRUE}} 18 | } 19 | \value{ 20 | character vector with stargazer output 21 | with the updated order 22 | } 23 | \description{ 24 | Use regular expressions to manipulate the right hand side 25 | variables in in a stargazer regrssion or table. 26 | This function is compatible works with \code{stargazer} output of type \code{latex} 27 | and \code{text}. 28 | } 29 | \examples{ 30 | ## -- Regression example -- ## 31 | library(stargazer) 32 | data(mtcars) 33 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 34 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 35 | ##latex example 36 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 37 | print(star.out) 38 | star.out <- star_rhs_order(star.out, c("cyl", "hp", "wt", "Constant")) 39 | print(star.out) 40 | ##text example 41 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 42 | print(star.out) 43 | star.out <- star_rhs_order(star.out, c("cyl", "hp", "wt", "Constant")) 44 | print(star.out) 45 | 46 | ## -- Summary stats example -- ## 47 | ##latex example 48 | star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "latex") 49 | print(star.out.summ) 50 | star.out.summ <- star_rhs_order(star.out.summ, c("hp", "mpg", "wt"), reg = FALSE) 51 | print(star.out.summ) 52 | ##text example 53 | star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text") 54 | print(star.out.summ) 55 | star.out.summ <- star_rhs_order(star.out.summ, c("hp", "mpg", "wt"), reg = FALSE) 56 | print(star.out.summ) 57 | 58 | ## -- Keeping a subset of all variables -- ## 59 | ##As star_rhs_order() matches based on regular expressions, 60 | ##it can also be used to show only a subset of all variables 61 | 62 | ##Latex example 63 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 64 | print(star.out) 65 | ##Only keep hp 66 | star.out <- star_rhs_order(star.out, c("hp")) 67 | print(star.out) 68 | 69 | ##text example 70 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 71 | print(star.out) 72 | ##Only keep hp 73 | star.out <- star_rhs_order(star.out, c("hp")) 74 | print(star.out) 75 | 76 | } 77 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, echo = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "README-" 12 | ) 13 | ``` 14 | 15 | # starpolishr 16 | 17 | This package allows for easy post polishing of latex [stargazer](https://cran.r-project.org/web/packages/stargazer/index.html) 18 | output. See 19 | [this vignette](https://github.com/ChandlerLutz/starpolishr/blob/master/vignettes/pdf/starpolishr-intro.pdf) for 20 | a quick introduction. For examples, see the `Tables` section of my 21 | papers [here](https://chandlerlutz.github.io/pdf/california-foreclosure-prevention-laws.pdf) and [here](https://chandlerlutz.github.io/pdf/local-labor-markets-canada-us.pdf). All of these tables were generated 22 | exclusively within R using `stargazer` and `starpolishr`. 23 | 24 | ## Installation 25 | 26 | You can install starpolishr from github with: 27 | 28 | ```{r gh-installation, eval = FALSE} 29 | # install.packages("devtools") 30 | devtools::install_github("ChandlerLutz/starpolishr") 31 | ``` 32 | 33 | ## Quick Tips 34 | 35 | * The first argument in all `starpolishr` functions is a 36 | `stargazer` table, allowing for easy compatibility with 37 | the 38 | [magrittr](https://cran.r-project.org/web/packages/magrittr/index.html) `%>%`. 39 | * All of the functions in the `starpolishr` package begin with 40 | `star_` for easy `tab` completion in emacs or rstudio. 41 | 42 | ## Key Functions 43 | 44 | Here is a list of key `starpolishr` functions in order of how often I 45 | use them. See their help files for more details. 46 | 47 | * `star_tex_notes` -- Adds custom notes using either the 48 | latex [caption](https://www.ctan.org/pkg/caption) or the 49 | latex [threeparttable](https://www.ctan.org/pkg/threeparttable). 50 | * `star_tex_write` -- writes latex tables to a file and optionally 51 | adds header files and common packages 52 | * `star_lhs_names` and `star_rhs_names` -- updates variable names 53 | using regular expressions. The advantage of these functions is that 54 | they allow for variable names to span more than one line. 55 | * `star_panel` -- panels multiple related `stargazer` tables into a 56 | single latex table. 57 | * `star_insert_row` -- insert a row after a given row in a `stargazer` 58 | table 59 | * `star_sidewaystable` -- converts a latex table to a latex 60 | `sidewaystable` 61 | * `star_add_column_numbers` -- if you pass a matrix to `stargazer`, it 62 | won't add column numbers. This function will add column numbers to a 63 | latex table. 64 | 65 | 66 | ## Authors 67 | 68 | [Chandler Lutz](https://chandlerlutz.github.io/) 69 | 70 | ## License 71 | 72 | GPL (>= 2) 73 | -------------------------------------------------------------------------------- /man/star_panel.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_panel.R 3 | \name{star_panel} 4 | \alias{star_panel} 5 | \title{To get a panel of stargazer output} 6 | \usage{ 7 | star_panel( 8 | ..., 9 | starlist = NULL, 10 | panel.names, 11 | reg = TRUE, 12 | same.lhs.vars = TRUE, 13 | same.summary.stats = TRUE, 14 | panel.label.fontface = "plain" 15 | ) 16 | } 17 | \arguments{ 18 | \item{...}{\code{stargazer} output} 19 | 20 | \item{starlist}{a list of \code{stargazer} output} 21 | 22 | \item{panel.names}{the names for the panels in the same order as 23 | the stargazer output} 24 | 25 | \item{reg}{(logical) set to TRUE if the panels are regression 26 | output; defualt is TRUE} 27 | 28 | \item{same.lhs.vars}{(logical) set to \code{TRUE} if all panels 29 | have the same left-hand-side variables. Currently, only 30 | \code{TRUE} is supported} 31 | 32 | \item{same.summary.stats}{(logical) set to \code{TRUE} if all 33 | panels have the same summary stats. Default is \code{TRUE}. If 34 | set to \code{FALSE}, different panels can have different 35 | summary stats. \code{same.summary.stats = FALSE} is only 36 | supported for regression and so \code{reg} must be set to 37 | \code{TRUE}} 38 | 39 | \item{panel.label.fontface}{The fontface for the panel 40 | labels. Options are "plain", "italic", "bold", 41 | "bold.italic". Default is "plain".} 42 | } 43 | \value{ 44 | the updated \code{stargazer} output 45 | } 46 | \description{ 47 | Currently only the same lhs variables and 48 | the same summary statistics across all panels are allowed 49 | } 50 | \examples{ 51 | ## -- Regressoin example -- ## 52 | library(stargazer) 53 | data(mtcars) 54 | ##First set up models without weight 55 | mod.mtcars.1 <- lm(mpg ~ hp, mtcars) 56 | mod.mtcars.2 <- lm(mpg ~ hp + cyl, mtcars) 57 | star.out.1 <- stargazer(mod.mtcars.1, mod.mtcars.2, keep.stat = "n") 58 | ##Second set of models with weight as a regressor 59 | mod.mtcars.3 <- lm(mpg ~ hp + wt, mtcars) 60 | mod.mtcars.4 <- lm(mpg ~ hp + cyl + wt, mtcars) 61 | star.out.2 <- stargazer(mod.mtcars.3, mod.mtcars.4, keep.stat = c("n", "rsq")) 62 | 63 | ##stargazer panel -- same summary statistics across panels. 64 | star.panel.out <- star_panel(star.out.1, star.out.2, 65 | panel.names = c("Without Weight", "With Weight") 66 | ) 67 | print(star.panel.out) 68 | ##write to a tex file and compile to check output 69 | \dontrun{ 70 | tex_write(star.panel.out, file = "my_tex_file.tex", headers = TRUE) 71 | } 72 | 73 | ##stargazer panel -- different summary statistics across panels. 74 | star.panel.out2 <- star_panel(star.out.1, star.out.2, 75 | same.summary.stats = FALSE, 76 | panel.names = c("Without Weight", "With Weight") 77 | ) 78 | print(star.panel.out2) 79 | ##write to a tex file and compile to check output 80 | \dontrun{ 81 | tex_write(star.panel.out2, file = "my_tex_file2.tex", headers = TRUE) 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/utils.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-21 6 | 7 | ##for some utility functions for the starpolish package:base 8 | 9 | #' Check if stargazer output is in latex form 10 | #' 11 | #' @param star the \code{stargazer} output 12 | is.latex <- function(star) grepl("tabular", star) %>% any 13 | 14 | #' Check if stargazer output is in text form 15 | #' 16 | #' @param star the \code{stargazer} output 17 | is.text <- function(star) grepl("==", star) %>% any 18 | 19 | #' Get \code{stargazer} line numbers by section 20 | #' 21 | #' To get the start line numbers, main line numbers, and end line numbers 22 | #' of \code{stargazer} output 23 | #' 24 | #' @param star the \code{stargazer} output 25 | #' @param reg if the stargazer is a regression 26 | #' @return a list with the line number for the \code{start.lines}, 27 | #' \code{main.lines}, and \code{end.lines} of the \code{stargazer} 28 | #' output 29 | get_star_line_numbers <- function(star, reg = TRUE) { 30 | 31 | latex <- is.latex(star) 32 | text <- is.text(star) 33 | 34 | ##Get the start and end of the table 35 | if (latex) { 36 | 37 | ##This uses that fact that \hlines in stargazer are symmetric 38 | hlines <- grep("hline", star) 39 | 40 | if (reg) { 41 | ##if the stargazer output is a regression 42 | ##remove if hlines has a element before or after it that is also 43 | ##an hline as the very start and end of the tables are both hlines. 44 | ##only keep the middle two elements of hlines 45 | while(length(hlines) > 2) 46 | hlines <- hlines[c(-1, -length(hlines))] 47 | } else { 48 | ##not a regression, only keep the last two elements of hlines 49 | hlines <- hlines[(length(hlines) - 1):length(hlines)] 50 | } 51 | 52 | 53 | } else if (text) { 54 | ##which lines only have dashes and no whitespace? 55 | ##these are the horizontal lines separating the sections 56 | hlines <- (grepl("---", star) & !grepl("\\s", star)) %>% 57 | which 58 | } 59 | 60 | 61 | ##The start, main ouput and the end lines 62 | start.lines <- 1:hlines[1] 63 | main.lines <- (hlines[1] + 1):(hlines[2] - 1) 64 | end.lines <- hlines[2]:length(star) 65 | 66 | return(list(start.lines = start.lines, 67 | main.lines = main.lines, 68 | end.lines = end.lines 69 | ) 70 | ) 71 | } 72 | 73 | #' get the star lines from \code{stargazer} output 74 | #' 75 | #' This function will return a list 76 | #' 77 | #' @param star the \code{stargazer} ouput 78 | #' @param reg (logical) set to TRUE for a regression 79 | #' @return a list with the \code{start.lines}, 80 | #' \code{main.lines}, and \code{end.lines} of the \code{stargazer} 81 | #' output 82 | get_star_lines <- function(star, reg) { 83 | 84 | star.line.numbers <- get_star_line_numbers(star, reg) 85 | 86 | star.lines <- list(start.lines = star[star.line.numbers$start.lines], 87 | main.lines = star[star.line.numbers$main.lines], 88 | end.lines = star[star.line.numbers$end.lines] 89 | ) 90 | return(star.lines) 91 | } 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /R/star_tex_write.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/tex_write.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-20 6 | 7 | ##for functions to write to a tex file 8 | 9 | 10 | #' Create the start of a tex 11 | #' 12 | #' @param file a \code{string} with the file name 13 | #' @export 14 | star_tex_start <- function(file) { 15 | 16 | ##the lines to write 17 | temp.lines <- paste0("%% ", file, "\n", 18 | "\\documentclass[a4paper,12pt]{article}", 19 | "\n", 20 | "\\usepackage{caption}\n", 21 | "\\usepackage{siunitx}\n", 22 | "\\usepackage{rotating}\n", 23 | "\\usepackage{natbib}\n", 24 | "\\usepackage[flushleft]{threeparttable}\n", 25 | "\\usepackage{amssymb}\n", 26 | "\\usepackage[a4paper, hmargin=0.5in,vmargin=0.5in]{geometry}\n", 27 | "\\begin{document}", 28 | "\n") 29 | ##write 30 | write(temp.lines, file, append = FALSE) 31 | return(invisible(NULL)) 32 | } 33 | 34 | #' Create the end of a tex document 35 | #' 36 | #' @param file a \code{character} string with the file name 37 | #' @param append append to the end of a tex file; defaults to TRUE 38 | #' @export 39 | star_tex_end <- function(file, append = TRUE) { 40 | 41 | ##the lines to write 42 | temp.lines <- c("\n\n \\end{document}") 43 | 44 | write(temp.lines, file, append = append) 45 | } 46 | 47 | 48 | #' Write a stargazer output to a tex file 49 | #' 50 | #' @param ... stargazer ouptut 51 | #' @param starlist a list of stargazer output to write to a file 52 | #' @param file where the file will be saved 53 | #' @param headers (logical) to include the start of a tex afile 54 | #' @param append logical to append at the end of a tex file; defaults 55 | #' to \code{FALSE} 56 | #' @export 57 | star_tex_write <- function(..., starlist = NULL, file, headers = FALSE, append = FALSE) { 58 | 59 | ##Get all of the output in a list 60 | starlist <- c(list(...), starlist) 61 | 62 | ##remove the information on the stargazer package author 63 | starlist <- lapply(starlist, function(x) x[!grepl("% Table created by", x)]) 64 | 65 | top.header <- paste0("%% ", file, "\n", 66 | "\\documentclass[a4paper,12pt]{article}", 67 | "\n", 68 | "\\usepackage{caption}\n", 69 | "\\usepackage{siunitx}\n", 70 | "\\usepackage{rotating}\n", 71 | "\\usepackage{natbib}\n", 72 | "\\usepackage[flushleft]{threeparttable}\n", 73 | "\\usepackage{amssymb}\n", 74 | "\\usepackage[a4paper, hmargin=0.5in,vmargin=0.5in]{geometry}\n", 75 | "\\begin{document}", 76 | "\n") 77 | 78 | bottom.header <- c("\n \\end{document}") 79 | 80 | if (headers == TRUE) write(top.header, file) 81 | 82 | if (!headers & !append) { 83 | ##Empty the text file 84 | write("", file) 85 | } 86 | 87 | ##Write the stargazer output to the file 88 | for (i in seq_along(starlist)) { 89 | 90 | write(paste0("%Tex File: ", getwd(), "/", file), file, append = TRUE) 91 | write(starlist[[i]], file, append = TRUE) 92 | write("\n\n", file, append = TRUE) 93 | } 94 | 95 | if (headers) { 96 | write(bottom.header, file, append = TRUE) 97 | } 98 | 99 | return(invisible(NULL)) 100 | } 101 | 102 | -------------------------------------------------------------------------------- /man/star_rhs_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_rhs_names.R 3 | \name{star_rhs_names} 4 | \alias{star_rhs_names} 5 | \title{To replace the names of the RHS variables in \code{stargazer} output} 6 | \usage{ 7 | star_rhs_names(star, pattern, line1, line2 = NULL) 8 | } 9 | \arguments{ 10 | \item{star}{the \code{stargazer} output} 11 | 12 | \item{pattern}{the regular expression pattern} 13 | 14 | \item{line1}{the variable name in the first line} 15 | 16 | \item{line2}{the optional variable name in the second line. The 17 | \code{line2} argument takes advantage of the notion that the standard 18 | error (for example) of a regression is printed on the line below 19 | the coefficient. NOTE: Text in \code{line2} will be inserted directly.} 20 | } 21 | \value{ 22 | a character vector with the updated stargazer output 23 | } 24 | \description{ 25 | To replace the names of the RHS variables in \code{stargazer}. 26 | The function also allows for an optional second line. 27 | } 28 | \details{ 29 | Note: the \code{pattern} and \code{line1} arguments must have the 30 | same length. If \code{line2} is not \code{NULL}, it must have the 31 | same length as \code{pattern} and \code{line1}. Also, \code{line2} 32 | will be inserted into \code{star} directly. 33 | } 34 | \examples{ 35 | ## -- Regression example -- ## 36 | library(stargazer) 37 | data(mtcars) 38 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 39 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 40 | 41 | ##Latex example 42 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 43 | print(star.out) 44 | ##Update the names hp and wt using one line only 45 | star.out.1 <- star_rhs_names(star.out, 46 | pattern = c("hp", "wt"), 47 | line1 = c("horsepower", "weight") 48 | ) 49 | print(star.out.1) 50 | ##Sometimes variables can have really long names and 51 | ##would be best written on 2 lines 52 | star.out.1 <- star_rhs_names(star.out, 53 | pattern = c("hp", "wt"), 54 | line1 = c("horse-", "weight"), 55 | line2 = c("power", "") 56 | ) 57 | print(star.out.1) 58 | 59 | ##Text example 60 | ##Note that star_rhs_names() will NOT adjust the 61 | ##length of the character elements if the variable names in line1 62 | ##or line 2 are longer than those that they are replacing 63 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 64 | print(star.out) 65 | ##Update the names hp and wt using oneline 66 | star.out.1 <- star_rhs_names(star.out, 67 | pattern = c("hp", "wt"), 68 | line1 = c("horsepower", "weight") 69 | ) 70 | print(star.out.1) 71 | ##Sometimes variables can have really long names and 72 | ##would be best written on 2 lines 73 | star.out.1 <- star_rhs_names(star.out, 74 | pattern = c("hp", "wt"), 75 | line1 = c("horse-", "weight"), 76 | line2 = c("power", "") 77 | ) 78 | print(star.out.1) 79 | 80 | 81 | ## -- Summary stats example -- ## 82 | ##latex example 83 | star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "latex") 84 | print(star.out.summ) 85 | star.out.summ <- star_rhs_names(star.out.summ, 86 | pattern = c("hp", "wt"), 87 | line1 = c("horsepower", "weight") 88 | ) 89 | print(star.out.summ) 90 | ##text example 91 | star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text") 92 | star.out.summ <- star_rhs_names(star.out.summ, 93 | pattern = c("hp", "wt"), 94 | line1 = c("horsepower", "weight") 95 | ) 96 | print(star.out.summ) 97 | } 98 | -------------------------------------------------------------------------------- /tests/testthat/test_lhs_functions.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/tests/testthat/test_lhs_functions.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | 8 | ## context("star_lhs_order function") 9 | 10 | ##Clear the workspace 11 | rm(list = ls()) 12 | 13 | library(stargazer); library(magrittr) 14 | 15 | data(mtcars) 16 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 17 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 18 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 19 | ##latex example 20 | star.out.latex <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 21 | type = "latex") 22 | ##text example 23 | star.out.text <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 24 | type = "text") 25 | 26 | 27 | 28 | test_that("star_lhs_function() works for regression", { 29 | ## -- Latex -- ## 30 | expect_is(star_lhs_names(star.out.latex, 31 | pattern = c("mpg", "hp"), 32 | line1 = c("miles per gallon", "horsepower") 33 | ), 34 | "character") 35 | expect_is(star_lhs_names(star.out.latex, 36 | pattern = c("mpg", "hp"), 37 | line1 = c("miles per", "horse-"), 38 | line2 = c("gallon", "power") 39 | ), 40 | "character") 41 | expect_is(star_lhs_names(star.out.latex, 42 | pattern = c("mpg", "hp"), 43 | line1 = c("miles", "horse-"), 44 | line2 = c("per", "power"), 45 | line3 = c("gallon", "") 46 | ), 47 | "character") 48 | expect_equal(star_lhs_names(star.out.latex, 49 | pattern = c("mpg", "hp"), 50 | line1 = c("miles per gallon", "horsepower") 51 | ) %>% length, 52 | star.out.latex %>% length) 53 | expect_equal(star_lhs_names(star.out.latex, 54 | pattern = c("mpg", "hp"), 55 | line1 = c("miles per", "horse-"), 56 | line2 = c("gallon", "power") 57 | ) %>% length, 58 | length(star.out.latex) + 1) 59 | expect_equal(star_lhs_names(star.out.latex, 60 | pattern = c("mpg", "hp"), 61 | line1 = c("miles", "horse-"), 62 | line2 = c("per", "power"), 63 | line3 = c("gallon", "") 64 | ) %>% length, 65 | length(star.out.latex) + 2) 66 | ## -- Text -- ## 67 | expect_is(star_lhs_names(star.out.text, 68 | pattern = c("mpg", "hp"), 69 | line1 = c("miles per gallon", "horsepower") 70 | ), 71 | "character") 72 | expect_is(star_lhs_names(star.out.text, 73 | pattern = c("mpg", "hp"), 74 | line1 = c("miles per", "horse-"), 75 | line2 = c("gallon", "power") 76 | ), 77 | "character") 78 | expect_is(star_lhs_names(star.out.text, 79 | pattern = c("mpg", "hp"), 80 | line1 = c("miles", "horse-"), 81 | line2 = c("per", "power"), 82 | line3 = c("gallon", "") 83 | ), 84 | "character") 85 | expect_equal(star_lhs_names(star.out.text, 86 | pattern = c("mpg", "hp"), 87 | line1 = c("miles per gallon", "horsepower") 88 | ) %>% length, 89 | star.out.text %>% length) 90 | expect_equal(star_lhs_names(star.out.text, 91 | pattern = c("mpg", "hp"), 92 | line1 = c("miles per", "horse-"), 93 | line2 = c("gallon", "power") 94 | ) %>% length, 95 | length(star.out.text) + 1) 96 | expect_equal(star_lhs_names(star.out.text, 97 | pattern = c("mpg", "hp"), 98 | line1 = c("miles", "horse-"), 99 | line2 = c("per", "power"), 100 | line3 = c("gallon", "") 101 | ) %>% length, 102 | length(star.out.text) + 2) 103 | }) 104 | 105 | -------------------------------------------------------------------------------- /vignettes/starpolishr-intro.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "`starpolishr`: Post-polishing of `stagazer` output " 3 | author: "Chandler Lutz" 4 | date: "`r Sys.Date()`" 5 | output: pdf_document 6 | header-includes: 7 | - \usepackage{graphicx} 8 | - \usepackage{float} 9 | - \usepackage{caption} 10 | - \floatplacement{figure}{H} ##So figures get placed correctly in pdfs 11 | - \floatplacement{table}{H} ##So figures get placed correctly in pdfs 12 | - \usepackage{setspace} 13 | - \onehalfspacing 14 | urlcolor: blue 15 | 16 | # Required: Vignette metadata for inclusion in a package. 17 | vignette: > 18 | %\VignetteIndexEntry{starpolishr} 19 | %\VignetteKeywords{R,stargazer,latex,regression tables} 20 | %\VignetteDepends{starpolishr,stargazer,magrittr} 21 | %\VignetteEncoding{UTF-8} 22 | %\VignetteEngine{knitr::rmarkdown} 23 | --- 24 | 25 | ```{r setup, include=FALSE} 26 | knitr::opts_chunk$set(echo = TRUE) 27 | ``` 28 | 29 | 30 | 35 | 36 | 37 | 38 | First note that the first argument in all `starpolishr` functions is a 39 | `stargazer` table, allowing for easy compatibility with 40 | the 41 | [magrittr](https://cran.r-project.org/web/packages/magrittr/index.html) `%>%`. 42 | Also, all of the functions in the `starpolishr` package begin with 43 | `star_` for easy `tab` completion in emacs or rstudio. 44 | 45 | # Basic Stargazer Tables 46 | 47 | To start, let's run a couple of regressions. These will be miles per 48 | gallon, `mpg`, on `hp` and `cyl` from the `mtcars` dataset. We'll also 49 | produce a 50 | basic 51 | [stargazer](https://cran.r-project.org/web/packages/stargazer/index.html) table. 52 | 53 | ```{r message=FALSE, results = 'asis'} 54 | library(stargazer); library(starpolishr); library(magrittr) 55 | data(mtcars) 56 | mod.mtcars.1 <- lm(mpg ~ hp, mtcars) 57 | mod.mtcars.2 <- lm(mpg ~ hp + cyl, mtcars) 58 | star.out.1 <- stargazer(mod.mtcars.1, mod.mtcars.2, 59 | title = "Motor Car Regressions", 60 | type = "latex", 61 | #For exact latex positioning using the latex 'float' package 62 | table.placement = "H", 63 | keep.stat = "n", 64 | header = FALSE) 65 | ``` 66 | 67 | 68 | Obviously, a potential confound is the vehicle weight, `wt`. For the 69 | purposes of this vignette, let's say we want the regressions that 70 | employ the `wt` variable in a separate table: 71 | 72 | ```{r results = 'asis'} 73 | #Second set of models with weight as a regressor 74 | mod.mtcars.3 <- lm(mpg ~ hp + wt, mtcars) 75 | mod.mtcars.4 <- lm(mpg ~ hp + cyl + wt, mtcars) 76 | star.out.2 <- stargazer(mod.mtcars.3, mod.mtcars.4, 77 | #For exact latex positioning using the latex 'float' package 78 | table.placement = "H", 79 | keep.stat = c("n", "rsq"), 80 | header = FALSE) 81 | ``` 82 | 83 | # Updating Regression Variable Names 84 | 85 | We can improve a number of things about these tables. First, the 86 | left-hand and right-hand side variables are a bit cryptic. Let's clean 87 | this up using `starpolishr` `star_lhs_names()` and `star_rhs_names()` 88 | functions, which use regular expressions to replace variable 89 | names. The advantage of these functions is that they allow the 90 | variable names to span more than one line. As we want to apply these 91 | functions to both tables, let's create a function to minimize 92 | duplicative code. 93 | 94 | ```{r } 95 | clean_var_names <- function(table) { 96 | table %>% 97 | #Update the RHS names 98 | star_rhs_names(pattern = c("hp", "cyl", "wt"), 99 | line1 = c("Hoarsepower", "Number of", "Vehicle"), 100 | line2 = c("", "Engine Cylinders", "Weight")) %>% 101 | star_lhs_names(pattern = c("mpg"), 102 | line1 = "Miles per", 103 | line2 = "Gallon") 104 | } 105 | 106 | ``` 107 | 108 | Now let's apply the function to the two stargazer tables and print 109 | 110 | ```{r results = 'asis'} 111 | star.out.1 <- clean_var_names(star.out.1) 112 | star.out.2 <- clean_var_names(star.out.2) 113 | ``` 114 | 115 | ```{r results = 'asis'} 116 | cat(star.out.1) 117 | ``` 118 | 119 | ```{r results = 'asis'} 120 | cat(star.out.2) 121 | ``` 122 | 123 | \clearpage 124 | 125 | # Create a table with 2 panels and add custom notes 126 | 127 | Now let's combine the tables into a two panel single table using the 128 | `star_panel()` function and add custom table notes using the 129 | `star_notes_tex()` function. Finally, we'll print the output 130 | 131 | 132 | ```{r results='asis'} 133 | star_panel(star.out.1, star.out.2, 134 | panel.names = c("Reg Without Weight", "Reg With Weight"), 135 | panel.label.fontface = "bold" #For bold panel names 136 | ) %>% 137 | #Add table notes 138 | star_notes_tex(note.type = "caption", #Use the latex 'caption' package for notes 139 | note = "Standard errors are in parentheses") %>% 140 | cat 141 | ``` 142 | 143 | -------------------------------------------------------------------------------- /man/star_lhs_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/star_lhs_names.R 3 | \name{star_lhs_names} 4 | \alias{star_lhs_names} 5 | \title{To replace the names of the LHS variables in \code{stargazer} output} 6 | \usage{ 7 | star_lhs_names( 8 | star, 9 | pattern, 10 | line1, 11 | line2 = NULL, 12 | line3 = NULL, 13 | multicol = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{star}{the \code{stargazer} output} 18 | 19 | \item{pattern}{the regular expression pattern} 20 | 21 | \item{line1}{the variable name in the first line} 22 | 23 | \item{line2}{the optional variable name in the second line} 24 | 25 | \item{line3}{the optional variable name in teh third line} 26 | 27 | \item{multicol}{if not \code{NULL}, multicolumn labels will be 28 | used. Options are "l" for left, "c" for center, and "r" for 29 | right. A character vector can be used for different alignments 30 | across different columns. If a single character is used, that 31 | alignment will be used for all elements in pattern. If a latex 32 | multicol is already applied by stargazer, \code{multicol} will 33 | be ignored for that variable. This argument is only valid for 34 | latex output.} 35 | } 36 | \value{ 37 | character vector with stargazer output with the updated LHS 38 | variable names 39 | } 40 | \description{ 41 | To replace the names of the RFHS variables in \code{stargazer}. 42 | The function also allows for optional second and third lines 43 | } 44 | \details{ 45 | Note: the \code{pattern} and \code{line1} arguments must have 46 | the same length. If \code{line2} and \code{line3} are not \code{NULL}, 47 | they must have the same length as \code{pattern} and \code{line1} 48 | } 49 | \examples{ 50 | ## -- Regression Example -- ## 51 | library(stargazer) 52 | data(mtcars) 53 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 54 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 55 | mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 56 | ##latex example 57 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 58 | type = "latex") 59 | print(star.out) 60 | ##update the LHS variable names using one line only 61 | star.out.1 <- star_lhs_names(star.out, 62 | pattern = c("mpg", "hp"), 63 | line1 = c("miles per gallon", "horsepower") 64 | ) 65 | print(star.out.1) 66 | ##Sometimes variables can have really long names and 67 | ##would be best written on 2 lines 68 | star.out.2 <- star_lhs_names(star.out, 69 | pattern = c("mpg", "hp"), 70 | line1 = c("miles per", "horse-"), 71 | line2 = c("gallon", "power") 72 | ) 73 | print(star.out.2) 74 | ##For this example, three lines might be even better 75 | star.out.3 <- star_lhs_names(star.out, 76 | pattern = c("mpg", "hp"), 77 | line1 = c("miles", "horse-"), 78 | line2 = c("per", "power"), 79 | line3 = c("gallon", "") 80 | ) 81 | print(star.out.3) 82 | 83 | ##For Multicolumn centered columns. Using multicolumn 84 | ##for the column labels can be useful for siunitx 85 | ##columns or if the column labels have a different alignment 86 | ##than the rest of the table 87 | star.out.4 <- star_lhs_names(star.out, 88 | pattern = c("mpg", "hp"), 89 | line1 = c("miles", "horse-"), 90 | line2 = c("per", "power"), 91 | line3 = c("gallon", ""), 92 | multicol = c("c") 93 | ) 94 | print(star.out.4) 95 | 96 | 97 | ##Text Examples 98 | ##Note that star_lhs_names() will NOT adjust the 99 | ##length of the character elements if the variable names in line1, 100 | ##line 2, or line3 are longer than those that they are replacing 101 | star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 102 | type = "text") 103 | print(star.out) 104 | ##update the LHS variable names using one line only 105 | star.out.1 <- star_lhs_names(star.out, 106 | pattern = c("mpg", "hp"), 107 | line1 = c("miles per gallon", "horsepower") 108 | ) 109 | print(star.out.1) 110 | ##Sometimes variables can have really long names and 111 | ##would be best written on 2 lines 112 | star.out.2 <- star_lhs_names(star.out, 113 | pattern = c("mpg", "hp"), 114 | line1 = c("miles per", "horse-"), 115 | line2 = c("gallon", "power") 116 | ) 117 | print(star.out.2) 118 | ##For this example, three lines might be even better 119 | star.out.3 <- star_lhs_names(star.out, 120 | pattern = c("mpg", "hp"), 121 | line1 = c("miles", "horse-"), 122 | line2 = c("per", "power"), 123 | line3 = c("gallon", "") 124 | ) 125 | print(star.out.3) 126 | } 127 | -------------------------------------------------------------------------------- /R/star_table_notes.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/star_table_notes.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-21 6 | 7 | ## #' stargazer table notes using latex \code{caption*} package 8 | ## #' 9 | ## #' Only latex is supported 10 | ## #' 11 | ## #' @param star \code{stargazer} output 12 | ## #' @param note the note to be added to the latex table 13 | ## #' @return the updated \code{stargazer} output 14 | ## #' @examples 15 | ## #' library(stargazer) 16 | ## #' data(mtcars) 17 | ## #' ##First set up models without weight 18 | ## #' mod.mtcars.1 <- lm(mpg ~ hp, mtcars) 19 | ## #' mod.mtcars.2 <- lm(mpg ~ hp + cyl, mtcars) 20 | ## #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2) 21 | ## #' star.out <- star_notes_caption(star.out, "custom note") 22 | ## #' print(star.out) 23 | ## star_notes_caption <- function(star, note) { 24 | 25 | ## warning("Warning: star_notes_caption() is deprecated, use star_notes_tex()") 26 | 27 | ## if (!is.latex(star)) stop("Only Latex currently supported") 28 | 29 | ## note <- paste0("\\caption*{\\footnotesize{\\emph{Notes:} ", 30 | ## note, "}}") 31 | 32 | ## ##First remove the standard stargazer note line 33 | ## ##if it exists 34 | ## if (grepl("Note:", star) %>% any) { 35 | ## note.line <- grepl("Note:", star) %>% which 36 | ## star <- star[-note.line] 37 | ## } 38 | 39 | ## end.tabular.line <- grepl("end\\{tabular\\}", star) %>% which 40 | 41 | ## ##add the note 42 | ## star.out <- c(star[1:end.tabular.line], 43 | ## note, 44 | ## star[(end.tabular.line + 1):length(star)] 45 | ## ) 46 | ## return(star.out) 47 | 48 | ## } 49 | 50 | 51 | #' \code{stargazer} tex table notes 52 | #' 53 | #' @param star \code{stargazer} output 54 | #' @param note.type the type of latex note to be used. Options include 55 | #' \code{"caption"} for use with the latex caption package and 56 | #' \code{"threeparttable"} for use with the latex threeparttable 57 | #' package. Only the first element of type will be used 58 | #' @param note the note to be added to the latex table 59 | #' @examples 60 | #' ## -- Regression Example -- ## 61 | #' library(stargazer) 62 | #' data(mtcars) 63 | #' mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 64 | #' mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 65 | #' mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 66 | #' ##latex example 67 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 68 | #' type = "latex") 69 | #' print(star.out) 70 | #' ##use latex caption package 71 | #' star_notes_tex(star.out, note = "my table notes") 72 | #' ##use latex caption package 73 | #' star_notes_tex(star.out, note.type = "caption", note = "my table notes") 74 | #' ##use latex threeparttable package 75 | #' star_notes_tex(star.out, note.type = "threeparttable", note = "my table notes") 76 | #' @export 77 | star_notes_tex <- function(star, note.type = c("caption", "threeparttable"), note) { 78 | 79 | if (!is.latex(star)) { 80 | stop("star_notes_tex() only accepts latex stargazer output") 81 | } 82 | 83 | ##First remove the standard stargazer note line 84 | ##if it exists 85 | if (grepl("Note:", star) %>% any) { 86 | note.line <- grepl("Note:", star) %>% which 87 | star <- star[-note.line] 88 | } 89 | 90 | note.type <- note.type[1] 91 | 92 | if (note.type == "caption") { 93 | 94 | ##The caption 95 | note <- paste0("\\caption*{\\footnotesize{\\textit{Notes:} ", 96 | note, "}}") 97 | 98 | 99 | 100 | end.tabular.line <- grepl("end\\{tabular\\}", star) %>% which 101 | 102 | ##add the note 103 | star.out <- c(star[1:end.tabular.line], 104 | note, 105 | star[(end.tabular.line + 1):length(star)] 106 | ) 107 | } else if (note.type == "threeparttable") { 108 | 109 | ##Note: we have to turn start into a threeparts table. 110 | ##This means putting the three partstable begining and end 111 | ##before the start and end of the table 112 | 113 | ##Inser the \begin{threepartstable} 114 | begin.table <- grep("\\\\begin\\{table\\}", star) 115 | end.table <- grep("\\\\end\\{table\\}", star) 116 | ##for the beginning of the table notes 117 | end.tabular <- grep("\\\\end\\{tabular\\}", star) 118 | threeparttable.string <- c("\\begin{threeparttable}", 119 | paste0("\\begin{tablenotes}\n", 120 | "\\footnotesize\n", 121 | "\\item \\textit{Notes:} ", note, 122 | "\n", 123 | "\\end{tablenotes}"), 124 | "\\end{threeparttable}" 125 | ) 126 | threeparttable.ind <- c(begin.table + 2, 127 | end.tabular, 128 | end.table - 1 129 | ) 130 | 131 | 132 | star.out <- star_insert_row(star, threeparttable.string, insert.after = threeparttable.ind) 133 | 134 | } else { 135 | stop("Error: in star_notes_tex(), note.type must be either 'caption' or 'threeparttable'") 136 | } 137 | 138 | 139 | return(star.out) 140 | 141 | } 142 | 143 | 144 | -------------------------------------------------------------------------------- /R/star_rhs_order.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/star_rhs_order.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | #' Manipulate the order of right-hand side variables using regex 8 | #' 9 | #' Use regular expressions to manipulate the right hand side 10 | #' variables in in a stargazer regrssion or table. 11 | #' This function is compatible works with \code{stargazer} output of type \code{latex} 12 | #' and \code{text}. 13 | #' 14 | #' @param star the stargazer table 15 | #' @param pattern a vector of regular expressions which 16 | #' the order with which the right-hand-side variables will 17 | #' be used 18 | #' @param reg (logical) set to TRUE for a regression; defaults to 19 | #' \code{TRUE} 20 | #' @return character vector with stargazer output 21 | #' with the updated order 22 | #' @examples 23 | #' ## -- Regression example -- ## 24 | #' library(stargazer) 25 | #' data(mtcars) 26 | #' mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 27 | #' mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 28 | #' ##latex example 29 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 30 | #' print(star.out) 31 | #' star.out <- star_rhs_order(star.out, c("cyl", "hp", "wt", "Constant")) 32 | #' print(star.out) 33 | #' ##text example 34 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 35 | #' print(star.out) 36 | #' star.out <- star_rhs_order(star.out, c("cyl", "hp", "wt", "Constant")) 37 | #' print(star.out) 38 | #' 39 | #' ## -- Summary stats example -- ## 40 | #' ##latex example 41 | #' star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "latex") 42 | #' print(star.out.summ) 43 | #' star.out.summ <- star_rhs_order(star.out.summ, c("hp", "mpg", "wt"), reg = FALSE) 44 | #' print(star.out.summ) 45 | #' ##text example 46 | #' star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text") 47 | #' print(star.out.summ) 48 | #' star.out.summ <- star_rhs_order(star.out.summ, c("hp", "mpg", "wt"), reg = FALSE) 49 | #' print(star.out.summ) 50 | #' 51 | #' ## -- Keeping a subset of all variables -- ## 52 | #' ##As star_rhs_order() matches based on regular expressions, 53 | #' ##it can also be used to show only a subset of all variables 54 | #' 55 | #' ##Latex example 56 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 57 | #' print(star.out) 58 | #' ##Only keep hp 59 | #' star.out <- star_rhs_order(star.out, c("hp")) 60 | #' print(star.out) 61 | #' 62 | #' ##text example 63 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 64 | #' print(star.out) 65 | #' ##Only keep hp 66 | #' star.out <- star_rhs_order(star.out, c("hp")) 67 | #' print(star.out) 68 | #' 69 | #' @export 70 | star_rhs_order <- function(star, pattern, reg = TRUE) { 71 | ##Assume that each coefficient comes with a second line for the SE or other output 72 | ##are there empty lines between the varaibles (logical) 73 | ##either latex or text 74 | latex <- is.latex(star) 75 | text <- is.text(star) 76 | 77 | star.lines <- get_star_line_numbers(star, reg = reg) 78 | start.lines <- star.lines$start.lines 79 | main.lines <- star.lines$main.lines 80 | end.lines <- star.lines$end.lines 81 | 82 | ##Check for empty lines in the main output 83 | empty.lines <- (!grepl("[a-zA-Z0-9]", star[main.lines])) %>% any 84 | ## The ^\\s$+ regex starts a string looking for one or more empty lines, then finishes 85 | ## the string 86 | ## empty.lines <- ifelse(grepl("^\\s+$", star) | grepl("& &", star), TRUE, FALSE) %>% 87 | ## any 88 | 89 | ##Check if there's extra regression output 90 | ##e.g. a standard error 91 | if (latex) { 92 | ##This regex will check for an empty space at the start of the string 93 | ##followed by & Then, it will look for numbers 94 | regression.extra <- grepl("(^\\s+&)(.*[0-9])", star[main.lines]) %>% 95 | any 96 | } else if (text) { 97 | ##This regex will check for an empty space at the beginning of the string 98 | ##and the for any other alphanumeric character. Strings with all white 99 | ##space will return false 100 | regression.extra <- grepl("(^\\s+)(.*[0-9a-zA-Z])", star[main.lines]) %>% 101 | any 102 | } 103 | 104 | ##Get the lines with the coefficients that we want keep in order 105 | coef.lines <- vector("numeric", length = length(pattern)) 106 | 107 | for (i in seq_along(pattern)) { 108 | temp <- grep(pattern[i], star) 109 | if (length(temp) == 0) { 110 | stop(paste0(pattern[i], " is not found in star")) 111 | } else if (length(temp) > 1) { 112 | stop(paste0(pattern[i], " matches more than 1 variable")) 113 | } 114 | coef.lines[i] <- temp 115 | } 116 | ##Also add in the standard errors and possible empty lines 117 | if (empty.lines && regression.extra) { 118 | ##both empty lines and regression.extra info 119 | coef.lines <- c(rbind(coef.lines, coef.lines+1, coef.lines+2)) 120 | } else if (!empty.lines && regression.extra) { 121 | ##regression.info but no empty lines 122 | coef.lines <- c(rbind(coef.lines, coef.lines+1)) 123 | } else if (!empty.lines && !regression.extra) { 124 | ##neither regression info or empty lines 125 | coef.lines <- coef.lines 126 | } else { 127 | stop("error in empty.lines or regression flag variables") 128 | } 129 | 130 | star.out <- star[c(start.lines, coef.lines, end.lines)] 131 | 132 | return(star.out) 133 | 134 | } 135 | -------------------------------------------------------------------------------- /R/star_rhs_names.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/star_rhs_names.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | ##Functions to replace the rhs names for variables 8 | 9 | 10 | #' Helper function for star_rhs_names 11 | #' 12 | #' A helper function to replace the names of the stargazer variables 13 | #' and possibly add a second line on the standard error. 14 | #' This function works with regression only 15 | #' 16 | #' @param star the stargazer output 17 | #' @param pattern the regex pattern 18 | #' @param line1 the first line for the variable name 19 | #' @param line2 the second line for the variable name 20 | #' @return the update stargazer output 21 | .star_rhs_replace <- function(star, pattern, line1, line2) { 22 | 23 | ##Get the line where the variable is 24 | line.var <- grep(pattern, star) 25 | 26 | if (length(line.var) > 1) { 27 | stop(paste0("pattern ", pattern, 28 | "matches to more than one line in the stargazer output") 29 | ) 30 | } 31 | 32 | ##Add the second var if requested by the user 33 | if (!is.null(line2)) { 34 | ##if line2 is set, add it in 35 | star[line.var + 1] <- paste0(line2, star[line.var + 1]) 36 | } 37 | ##Replace the main variable 38 | star[line.var] <- sub(pattern, line1, star[line.var]) 39 | return(star) 40 | } 41 | 42 | #' To replace the names of the RHS variables in \code{stargazer} output 43 | #' 44 | #' To replace the names of the RHS variables in \code{stargazer}. 45 | #' The function also allows for an optional second line. 46 | #' 47 | #' Note: the \code{pattern} and \code{line1} arguments must have the 48 | #' same length. If \code{line2} is not \code{NULL}, it must have the 49 | #' same length as \code{pattern} and \code{line1}. Also, \code{line2} 50 | #' will be inserted into \code{star} directly. 51 | #' 52 | #' @param star the \code{stargazer} output 53 | #' @param pattern the regular expression pattern 54 | #' @param line1 the variable name in the first line 55 | #' @param line2 the optional variable name in the second line. The 56 | #' \code{line2} argument takes advantage of the notion that the standard 57 | #' error (for example) of a regression is printed on the line below 58 | #' the coefficient. NOTE: Text in \code{line2} will be inserted directly. 59 | #' @return a character vector with the updated stargazer output 60 | #' @examples 61 | #' ## -- Regression example -- ## 62 | #' library(stargazer) 63 | #' data(mtcars) 64 | #' mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 65 | #' mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 66 | #' 67 | #' ##Latex example 68 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 69 | #' print(star.out) 70 | #' ##Update the names hp and wt using one line only 71 | #' star.out.1 <- star_rhs_names(star.out, 72 | #' pattern = c("hp", "wt"), 73 | #' line1 = c("horsepower", "weight") 74 | #' ) 75 | #' print(star.out.1) 76 | #' ##Sometimes variables can have really long names and 77 | #' ##would be best written on 2 lines 78 | #' star.out.1 <- star_rhs_names(star.out, 79 | #' pattern = c("hp", "wt"), 80 | #' line1 = c("horse-", "weight"), 81 | #' line2 = c("power", "") 82 | #' ) 83 | #' print(star.out.1) 84 | #' 85 | #' ##Text example 86 | #' ##Note that star_rhs_names() will NOT adjust the 87 | #' ##length of the character elements if the variable names in line1 88 | #' ##or line 2 are longer than those that they are replacing 89 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 90 | #' print(star.out) 91 | #' ##Update the names hp and wt using oneline 92 | #' star.out.1 <- star_rhs_names(star.out, 93 | #' pattern = c("hp", "wt"), 94 | #' line1 = c("horsepower", "weight") 95 | #' ) 96 | #' print(star.out.1) 97 | #' ##Sometimes variables can have really long names and 98 | #' ##would be best written on 2 lines 99 | #' star.out.1 <- star_rhs_names(star.out, 100 | #' pattern = c("hp", "wt"), 101 | #' line1 = c("horse-", "weight"), 102 | #' line2 = c("power", "") 103 | #' ) 104 | #' print(star.out.1) 105 | #' 106 | #' 107 | #' ## -- Summary stats example -- ## 108 | #' ##latex example 109 | #' star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "latex") 110 | #' print(star.out.summ) 111 | #' star.out.summ <- star_rhs_names(star.out.summ, 112 | #' pattern = c("hp", "wt"), 113 | #' line1 = c("horsepower", "weight") 114 | #' ) 115 | #' print(star.out.summ) 116 | #' ##text example 117 | #' star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text") 118 | #' star.out.summ <- star_rhs_names(star.out.summ, 119 | #' pattern = c("hp", "wt"), 120 | #' line1 = c("horsepower", "weight") 121 | #' ) 122 | #' print(star.out.summ) 123 | #' @export 124 | star_rhs_names <- function(star, pattern, line1, line2 = NULL) { 125 | 126 | ##pattern and line1 must have the same length 127 | ##if line1 to is set it must have the same length as line1 128 | if (length(pattern) != length(line1)) 129 | stop("pattern and line1 do not have the same length") 130 | if (!is.null(line2) && (length(line2) != length(line1))) 131 | stop("line1 and line2 do not have the same length") 132 | 133 | ##loop through and update star 134 | for (i in seq_along(pattern)) { 135 | star <- .star_rhs_replace(star, pattern[i], line1[i], line2[i]) 136 | } 137 | return(star) 138 | 139 | } 140 | -------------------------------------------------------------------------------- /tests/testthat/test_rhs_functions.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/tests/testthat/test_rhs_functions.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | 8 | context("star_rhs_order function") 9 | 10 | ##Clear the workspace 11 | rm(list = ls()) 12 | 13 | library(stargazer); library(magrittr) 14 | 15 | library(stargazer) 16 | data(mtcars) 17 | mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 18 | mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 19 | star.out.latex <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex") 20 | star.out.text <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text") 21 | 22 | star.out.summ.latex <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "latex") 23 | star.out.summ.text <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text") 24 | 25 | 26 | ## -- star_rhs_order () -- ## 27 | 28 | 29 | 30 | 31 | test_that("star_rhs_order() works for regression", { 32 | expect_is(star_rhs_order(star.out.latex, c("cyl", "hp", "wt", "Constant")), "character") 33 | expect_equal(star_rhs_order(star.out.latex, c("cyl", "hp", "wt", "Constant")) %>% length, 34 | star.out.latex %>% length) 35 | expect_is(star_rhs_order(star.out.text, c("cyl", "hp", "wt", "Constant")), "character") 36 | expect_equal(star_rhs_order(star.out.text, c("cyl", "hp", "wt", "Constant")) %>% length, 37 | star.out.text %>% length) 38 | }) 39 | 40 | test_that("star_rhs_order() works for summary stats", { 41 | expect_is(star_rhs_order(star.out.summ.latex, c("hp", "mpg", "wt"), reg = FALSE), 42 | "character") 43 | expect_equal(star_rhs_order(star.out.summ.latex, c("hp", "mpg", "wt"), reg = FALSE) 44 | %>% length, 45 | star.out.summ.latex %>% length) 46 | expect_is(star_rhs_order(star.out.summ.text, c("hp", "mpg", "wt"), reg = FALSE), 47 | "character") 48 | expect_equal(star_rhs_order(star.out.summ.text, c("hp", "mpg", "wt"), reg = FALSE) %>% 49 | length, 50 | star.out.summ.text %>% length) 51 | }) 52 | 53 | ## -- star_rhs_names() -- ## 54 | 55 | context("star_rhs_names") 56 | 57 | test_that("star_rhs_names() works for regressions", { 58 | expect_is(star_rhs_names(star.out.latex, 59 | pattern = c("hp", "wt"), 60 | line1 = c("horsepower", "weight") 61 | ), 62 | "character") 63 | expect_is(star_rhs_names(star.out.latex, 64 | pattern = c("hp", "wt"), 65 | line1 = c("horse-", "weight"), 66 | line2 = c("power", "") 67 | ), 68 | "character") 69 | expect_is(star_rhs_names(star.out.text, 70 | pattern = c("hp", "wt"), 71 | line1 = c("horsepower", "weight") 72 | ), 73 | "character") 74 | expect_is(star_rhs_names(star.out.text, 75 | pattern = c("hp", "wt"), 76 | line1 = c("horse-", "weight"), 77 | line2 = c("power", "") 78 | ), 79 | "character") 80 | expect_equal(star_rhs_names(star.out.latex, 81 | pattern = c("hp", "wt"), 82 | line1 = c("horsepower", "weight") 83 | ) %>% length, 84 | star.out.latex %>% length 85 | ) 86 | expect_equal(star_rhs_names(star.out.latex, 87 | pattern = c("hp", "wt"), 88 | line1 = c("horse-", "weight"), 89 | line2 = c("power", "") 90 | ) %>% length, 91 | star.out.latex %>% length 92 | ) 93 | expect_equal(star_rhs_names(star.out.text, 94 | pattern = c("hp", "wt"), 95 | line1 = c("horsepower", "weight") 96 | ) %>% length, 97 | star.out.text %>% length 98 | ) 99 | expect_equal(star_rhs_names(star.out.text, 100 | pattern = c("hp", "wt"), 101 | line1 = c("horse-", "weight"), 102 | line2 = c("power", "") 103 | ) %>% length, 104 | star.out.text %>% length 105 | ) 106 | }) 107 | 108 | 109 | 110 | test_that("star_rhs_names() works for summary stats", { 111 | expect_is(star_rhs_names(star.out.summ.latex, 112 | pattern = c("hp", "wt"), 113 | line1 = c("horsepower", "weight") 114 | ), 115 | "character") 116 | expect_is(star_rhs_names(star.out.summ.latex, 117 | pattern = c("hp", "wt"), 118 | line1 = c("horse-", "weight"), 119 | line2 = c("power", "") 120 | ), 121 | "character") 122 | expect_equal(star_rhs_names(star.out.summ.latex, 123 | pattern = c("hp", "wt"), 124 | line1 = c("horsepower", "weight") 125 | ) %>% length, 126 | star.out.summ.latex %>% length 127 | ) 128 | expect_equal(star_rhs_names(star.out.summ.latex, 129 | pattern = c("hp", "wt"), 130 | line1 = c("horse-", "weight"), 131 | line2 = c("power", "") 132 | ) %>% length, 133 | star.out.summ.latex %>% length 134 | ) 135 | ## -- text -- ## 136 | expect_is(star_rhs_names(star.out.summ.text, 137 | pattern = c("hp", "wt"), 138 | line1 = c("horsepower", "weight") 139 | ), 140 | "character") 141 | expect_is(star_rhs_names(star.out.summ.text, 142 | pattern = c("hp", "wt"), 143 | line1 = c("horse-", "weight"), 144 | line2 = c("power", "") 145 | ), 146 | "character") 147 | expect_equal(star_rhs_names(star.out.summ.text, 148 | pattern = c("hp", "wt"), 149 | line1 = c("horsepower", "weight") 150 | ) %>% length, 151 | star.out.summ.text %>% length 152 | ) 153 | expect_equal(star_rhs_names(star.out.summ.text, 154 | pattern = c("hp", "wt"), 155 | line1 = c("horse-", "weight"), 156 | line2 = c("power", "") 157 | ) %>% length, 158 | star.out.summ.text %>% length 159 | ) 160 | }) 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /R/star_panel.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/star_panel.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | 8 | #' To get a panel of stargazer output 9 | #' 10 | #' Currently only the same lhs variables and 11 | #' the same summary statistics across all panels are allowed 12 | #' 13 | #' @param ... \code{stargazer} output 14 | #' @param starlist a list of \code{stargazer} output 15 | #' @param panel.names the names for the panels in the same order as 16 | #' the stargazer output 17 | #' @param reg (logical) set to TRUE if the panels are regression 18 | #' output; defualt is TRUE 19 | #' @param same.lhs.vars (logical) set to \code{TRUE} if all panels 20 | #' have the same left-hand-side variables. Currently, only 21 | #' \code{TRUE} is supported 22 | #' @param same.summary.stats (logical) set to \code{TRUE} if all 23 | #' panels have the same summary stats. Default is \code{TRUE}. If 24 | #' set to \code{FALSE}, different panels can have different 25 | #' summary stats. \code{same.summary.stats = FALSE} is only 26 | #' supported for regression and so \code{reg} must be set to 27 | #' \code{TRUE} 28 | #' @param panel.label.fontface The fontface for the panel 29 | #' labels. Options are "plain", "italic", "bold", 30 | #' "bold.italic". Default is "plain". 31 | #' @return the updated \code{stargazer} output 32 | #' @examples 33 | #' ## -- Regressoin example -- ## 34 | #' library(stargazer) 35 | #' data(mtcars) 36 | #' ##First set up models without weight 37 | #' mod.mtcars.1 <- lm(mpg ~ hp, mtcars) 38 | #' mod.mtcars.2 <- lm(mpg ~ hp + cyl, mtcars) 39 | #' star.out.1 <- stargazer(mod.mtcars.1, mod.mtcars.2, keep.stat = "n") 40 | #' ##Second set of models with weight as a regressor 41 | #' mod.mtcars.3 <- lm(mpg ~ hp + wt, mtcars) 42 | #' mod.mtcars.4 <- lm(mpg ~ hp + cyl + wt, mtcars) 43 | #' star.out.2 <- stargazer(mod.mtcars.3, mod.mtcars.4, keep.stat = c("n", "rsq")) 44 | #' 45 | #' ##stargazer panel -- same summary statistics across panels. 46 | #' star.panel.out <- star_panel(star.out.1, star.out.2, 47 | #' panel.names = c("Without Weight", "With Weight") 48 | #' ) 49 | #' print(star.panel.out) 50 | #' ##write to a tex file and compile to check output 51 | #' \dontrun{ 52 | #' tex_write(star.panel.out, file = "my_tex_file.tex", headers = TRUE) 53 | #' } 54 | #' 55 | #' ##stargazer panel -- different summary statistics across panels. 56 | #' star.panel.out2 <- star_panel(star.out.1, star.out.2, 57 | #' same.summary.stats = FALSE, 58 | #' panel.names = c("Without Weight", "With Weight") 59 | #' ) 60 | #' print(star.panel.out2) 61 | #' ##write to a tex file and compile to check output 62 | #' \dontrun{ 63 | #' tex_write(star.panel.out2, file = "my_tex_file2.tex", headers = TRUE) 64 | #' } 65 | #' @export 66 | star_panel <- function(..., starlist = NULL, panel.names, 67 | reg = TRUE, same.lhs.vars = TRUE, 68 | same.summary.stats = TRUE, 69 | panel.label.fontface = "plain") { 70 | 71 | ##Get all of the star output 72 | if (is.null(starlist)) starlist <- c(list(...)) 73 | latex <- is.latex(starlist[[1]]) 74 | text <- is.text(starlist[[1]]) 75 | 76 | if (!latex) stop("Currently only latex supported") 77 | 78 | if (length(panel.names) != length(starlist)) { 79 | stop("the length of panel.names needs to be the same as the length of starlist") 80 | } 81 | 82 | ##Get the panel names using letters 83 | for (i in seq_along(panel.names)) { 84 | ##Update the panel name 85 | panel.names[i] <- paste0("Panel ", LETTERS[i], ": ", panel.names[i]) 86 | } 87 | 88 | ##Now get the star lines 89 | star.lines <- lapply(starlist, get_star_lines, reg = reg) 90 | 91 | ##The starting and ending lines 92 | start.lines <- star.lines[[1]]$start.lines 93 | 94 | 95 | ##Initialize the summary statistics for each panel to NULL 96 | panel.summary.stats <- vector(mode = "list", 97 | length = length(starlist)) 98 | 99 | ##Only regressions are allowed to have different 100 | ##summary stats across panels 101 | if (same.summary.stats == FALSE && reg == TRUE) { 102 | ##Get the end lines without any summary stats. The 103 | ##end lines will be from the second hline to the end, 104 | ##after the summary stats 105 | end.lines <- star.lines[[1]]$end.lines 106 | end.lines.second.hline <- grep("hline", end.lines)[2] 107 | end.lines <- end.lines[end.lines.second.hline:length(end.lines)] 108 | 109 | ##Loop through the starlist and add the summary stats 110 | ##Use a cline before each set of summary stats 111 | for (i in seq_along(panel.summary.stats)) { 112 | ##the end lines 113 | out <- star.lines[[i]]$end.lines 114 | ##The summary stats will be between the first and second hlines 115 | out.hlines <- grep("hline", out) 116 | 117 | ##The summary statistics 118 | out <- out[(out.hlines[1] + 1):(out.hlines[2] - 1)] 119 | 120 | ##Add in a cline before the summary stats 121 | out <- c(paste0("\\cline{2-", star_ncol(out), "} \\\\[-2.0ex]"), 122 | out) 123 | 124 | ##For the bottom panel, add a little space after 125 | ##the summary stats 126 | if (i == length(panel.summary.stats)) 127 | out <- c(out, "\\\\[-2.0ex]") 128 | 129 | ##Assign the output 130 | panel.summary.stats[[i]] <- out 131 | 132 | ##remove out 133 | rm(out, out.hlines) 134 | } 135 | 136 | } else { 137 | ##If the summary statistics are the same across all panels, 138 | ##use the summary statistics from the first panel 139 | end.lines <- star.lines[[1]]$end.lines 140 | 141 | } 142 | 143 | 144 | ##Get just the main lines 145 | star.lines <- lapply(star.lines, function(x) x$main.lines) 146 | 147 | ##Add the panel names at the top of the main lines 148 | for (i in seq_along(starlist)) { 149 | 150 | ##The temporary panel name 151 | ##note: add @{} to left alignment as stargazer adds 152 | ##extra line space. see http://tex.stackexchange.com/a/276468/16412 153 | temp.panel.name <- paste0("\\\\[-2.0ex] \\multicolumn{", 154 | star_ncol(star.lines[[i]]), 155 | ##make sure to add an empty column after the 156 | ##panel name 157 | "}{@{} l}{") 158 | ##Add in the panel name with the fontface requested by the user 159 | if (panel.label.fontface == "plain") { 160 | temp.panel.name <- paste0(temp.panel.name, panel.names[i], "}") 161 | } else if (panel.label.fontface == "bold") { 162 | temp.panel.name <- paste0(temp.panel.name, "\\textbf{", panel.names[i], "}}") 163 | } else if (panel.label.fontface == "italic") { 164 | temp.panel.name <- paste0(temp.panel.name, "\\textit{", panel.names[i], "}}") 165 | } else if (panel.label.fontface == "bold.italic") { 166 | temp.panel.name <- paste0(temp.panel.name, 167 | "\\textit{\\textbf{", panel.names[i], "}}}") 168 | } else { 169 | stop('temp.panel.name must be "plain", "bold", "italic", or "bold.italic') 170 | } 171 | 172 | ##Finish the line and add a little space 173 | temp.panel.name <- paste0(temp.panel.name, "\n \\\\", 174 | "\n \\\\[-1.5ex]") 175 | 176 | ##If not the last panel, add the hline and a little 177 | ##space after. Otherwise, add nothing 178 | if (i != length(panel.names)) { 179 | ## 180 | hline.end <- "\\\\[-1.83ex] \n \\hline \\\\[-1.83ex]" 181 | } else { 182 | hline.end <- NULL 183 | } 184 | 185 | ##If there are going to be different summary statistics 186 | ##for each panel, add them in 187 | 188 | star.lines[[i]] <- c(temp.panel.name, 189 | star.lines[[i]], 190 | panel.summary.stats[[i]], ##Add in the panel summ stats 191 | hline.end 192 | ) 193 | 194 | } 195 | 196 | out <- c(start.lines, unlist(star.lines), end.lines) 197 | 198 | return(out) 199 | 200 | } 201 | -------------------------------------------------------------------------------- /R/star_lhs_names.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/star_lhs_names.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-19 6 | 7 | 8 | #' Mode function 9 | #' 10 | #' https://www.tutorialspoint.com/r/r_mean_median_mode.htm 11 | #' @param v the vector 12 | #' @return the mode 13 | getmode <- function(v) { 14 | uniqv <- unique(v) 15 | uniqv[which.max(tabulate(match(v, uniqv)))] 16 | } 17 | 18 | 19 | #' To replace the names of the LHS variables in \code{stargazer} output 20 | #' 21 | #' To replace the names of the RFHS variables in \code{stargazer}. 22 | #' The function also allows for optional second and third lines 23 | #' 24 | #' Note: the \code{pattern} and \code{line1} arguments must have 25 | #' the same length. If \code{line2} and \code{line3} are not \code{NULL}, 26 | #' they must have the same length as \code{pattern} and \code{line1} 27 | #' 28 | #' @param star the \code{stargazer} output 29 | #' @param pattern the regular expression pattern 30 | #' @param line1 the variable name in the first line 31 | #' @param line2 the optional variable name in the second line 32 | #' @param line3 the optional variable name in teh third line 33 | #' @param multicol if not \code{NULL}, multicolumn labels will be 34 | #' used. Options are "l" for left, "c" for center, and "r" for 35 | #' right. A character vector can be used for different alignments 36 | #' across different columns. If a single character is used, that 37 | #' alignment will be used for all elements in pattern. If a latex 38 | #' multicol is already applied by stargazer, \code{multicol} will 39 | #' be ignored for that variable. This argument is only valid for 40 | #' latex output. 41 | #' @return character vector with stargazer output with the updated LHS 42 | #' variable names 43 | #' @examples 44 | #' ## -- Regression Example -- ## 45 | #' library(stargazer) 46 | #' data(mtcars) 47 | #' mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 48 | #' mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 49 | #' mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 50 | #' ##latex example 51 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 52 | #' type = "latex") 53 | #' print(star.out) 54 | #' ##update the LHS variable names using one line only 55 | #' star.out.1 <- star_lhs_names(star.out, 56 | #' pattern = c("mpg", "hp"), 57 | #' line1 = c("miles per gallon", "horsepower") 58 | #' ) 59 | #' print(star.out.1) 60 | #' ##Sometimes variables can have really long names and 61 | #' ##would be best written on 2 lines 62 | #' star.out.2 <- star_lhs_names(star.out, 63 | #' pattern = c("mpg", "hp"), 64 | #' line1 = c("miles per", "horse-"), 65 | #' line2 = c("gallon", "power") 66 | #' ) 67 | #' print(star.out.2) 68 | #' ##For this example, three lines might be even better 69 | #' star.out.3 <- star_lhs_names(star.out, 70 | #' pattern = c("mpg", "hp"), 71 | #' line1 = c("miles", "horse-"), 72 | #' line2 = c("per", "power"), 73 | #' line3 = c("gallon", "") 74 | #' ) 75 | #' print(star.out.3) 76 | #' 77 | #' ##For Multicolumn centered columns. Using multicolumn 78 | #' ##for the column labels can be useful for siunitx 79 | #' ##columns or if the column labels have a different alignment 80 | #' ##than the rest of the table 81 | #' star.out.4 <- star_lhs_names(star.out, 82 | #' pattern = c("mpg", "hp"), 83 | #' line1 = c("miles", "horse-"), 84 | #' line2 = c("per", "power"), 85 | #' line3 = c("gallon", ""), 86 | #' multicol = c("c") 87 | #' ) 88 | #' print(star.out.4) 89 | #' 90 | #' 91 | #' ##Text Examples 92 | #' ##Note that star_lhs_names() will NOT adjust the 93 | #' ##length of the character elements if the variable names in line1, 94 | #' ##line 2, or line3 are longer than those that they are replacing 95 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 96 | #' type = "text") 97 | #' print(star.out) 98 | #' ##update the LHS variable names using one line only 99 | #' star.out.1 <- star_lhs_names(star.out, 100 | #' pattern = c("mpg", "hp"), 101 | #' line1 = c("miles per gallon", "horsepower") 102 | #' ) 103 | #' print(star.out.1) 104 | #' ##Sometimes variables can have really long names and 105 | #' ##would be best written on 2 lines 106 | #' star.out.2 <- star_lhs_names(star.out, 107 | #' pattern = c("mpg", "hp"), 108 | #' line1 = c("miles per", "horse-"), 109 | #' line2 = c("gallon", "power") 110 | #' ) 111 | #' print(star.out.2) 112 | #' ##For this example, three lines might be even better 113 | #' star.out.3 <- star_lhs_names(star.out, 114 | #' pattern = c("mpg", "hp"), 115 | #' line1 = c("miles", "horse-"), 116 | #' line2 = c("per", "power"), 117 | #' line3 = c("gallon", "") 118 | #' ) 119 | #' print(star.out.3) 120 | #' @export 121 | star_lhs_names <- function(star, pattern, line1, line2 = NULL, line3 = NULL, 122 | multicol = NULL) { 123 | 124 | ##if not null, pattern, line1, line2, and line3 must have the same length 125 | ##pattern must be the same length as line1 126 | if (length(pattern) != length(line1)) { 127 | stop("pattern must be the sampe length as line 1") 128 | } 129 | 130 | ##Test to make sure each of the the elements in pattern is 131 | ##in star 132 | for (i in seq_along(pattern)) { 133 | if (!grepl(pattern[i], star) %>% any) { 134 | stop(paste0(pattern[i], " not found in star")) 135 | } 136 | } 137 | 138 | ##Get either latex or text output 139 | latex <- grepl("tabular", star) %>% any 140 | text <- grepl("==", star) %>% any 141 | 142 | ##If multicol is not null and has length 1, set it equal 143 | ##to the length of all lhs variables 144 | if (!is.null(multicol)) { 145 | if (length(multicol) == 1) { 146 | multicol <- rep(multicol, length(pattern)) 147 | } 148 | } 149 | 150 | ##get the line position of the dependent variables -- use the 151 | ##mode to get the most likely candidate 152 | dep.pos <- sapply(pattern, function(p) grep(p, star)[1]) %>% 153 | getmode 154 | 155 | ##Define line1, line2 and line3 as necessary 156 | line1.out <- star[dep.pos] 157 | 158 | if (!is.null(line2)) { 159 | line2.out <- line1.out 160 | } else { 161 | line2.out <- NULL 162 | } 163 | 164 | ##remove any extra whitespace/newlines from line2.out. 165 | ##note that we need to escape back-slashes in both R and regex 166 | ##So, to match \\ we need 2*4 + 2 = 10 backslashes. So, 8 escape 167 | ##backslashes and 2 regreular back slashes 168 | if (!is.null(line2)) { 169 | line2.out <- sub("\\\\\\\\\\[.*\\]", "", line2.out) 170 | } 171 | ##if line3 is to be use, set line3.out equal to line2.out 172 | if (!is.null(line3) ) { 173 | line3.out <- line2.out 174 | } else { 175 | line3.out <- NULL 176 | } 177 | 178 | ##Do the substitution 179 | for (i in seq_along(pattern)) { 180 | ##check if multicolumn is already present. If not, add it as requested 181 | ##by the user 182 | if (latex && !is.null(multicol) && !grepl(paste0("multicolumn\\{.\\}\\{.\\}\\{",pattern[i]), line1.out)) { 183 | line1[i] <- paste0("\\\\multicolumn{1}{", multicol[i], "}{", line1[i], "}") 184 | ##For line2 and line3 185 | if (!is.null(line2)) { 186 | line2[i] <- paste0("\\\\multicolumn{1}{", multicol[i], "}{", line2[i], "}") 187 | } 188 | if (!is.null(line3)) { 189 | line3[i] <- paste0("\\\\multicolumn{1}{", multicol[i], "}{", line3[i], "}") 190 | } 191 | } ##End of multicol if 192 | 193 | line1.out <- sub(pattern[i], line1[i], line1.out) 194 | ##If latex, multicol, and no multicol already present, 195 | ##add the multicol 196 | 197 | if (!is.null(line2)) { 198 | line2.out <- sub(pattern[i], line2[i], line2.out) 199 | } 200 | if (!is.null(line3)) 201 | line3.out <- sub(pattern[i], line3[i], line3.out) 202 | } 203 | 204 | dependent.var <- c(line1.out, line2.out, line3.out) 205 | 206 | star <- c(star[1:(dep.pos - 1)], dependent.var, star[(dep.pos + 1):length(star)]) 207 | 208 | return(star) 209 | 210 | } 211 | -------------------------------------------------------------------------------- /R/star_cleaning_functions.R: -------------------------------------------------------------------------------- 1 | ## c:/Dropbox/Rpackages/starpolish/R/star_cleaning_functions.R 2 | 3 | ## Chandler Lutz 4 | ## Questions/comments: cl.eco@cbs.dk 5 | ## $Revisions: 1.0.0 $Date: 2016-12-21 6 | 7 | ##Extra functions for cleaning stargazer ouput 8 | 9 | #' Insert column numbrers in a table 10 | #' 11 | #' Currently this only works for latex. This function is helpful when 12 | #' creating a stargazer table from matrix output as stargazer does not 13 | #' automatically use column numbers for matrices 14 | #' 15 | #' @param star the \code{stargazer} output 16 | #' @param insert.after insert the column numbers after 17 | #' \code{inster.after} 18 | #' @param skip.col.1 skip the first column? If \code{TRUE} no column 19 | #' number will be placed after the first column 20 | #' @param add.space add space before the previous column? Defaults to \code{FALSE} 21 | #' @param multicol if not \code{NULL}, multicolumn labels will be 22 | #' used. Options are "l" for left, "c" for center, and "r" for 23 | #' right. A character vector can be used for different alignments 24 | #' across different columns. If a single character is used, that 25 | #' alignment will be used for all elements in pattern. If a latex 26 | #' multicol is already applied by stargazer, \code{multicol} will 27 | #' be ignored for that variable. 28 | #' @return an updated version of the stargazer table with column numbers 29 | #' @examples 30 | #' library(stargazer) 31 | #' data(mtcars) 32 | #' star.out <- stargazer(as.matrix(head(mtcars))) 33 | #' print(star.out) 34 | #' ##Insert column number using default values 35 | #' star_add_column_numbers(star.out, insert.after = 10) 36 | #' ##Do not skip the first column when creating column numbers 37 | #' star_add_column_numbers(star.out, insert.after = 10, skip.col.1 = FALSE) 38 | #' ##Add a space before the previous column 39 | #' star_add_column_numbers(star.out, insert.after = 10, add.space = TRUE) 40 | #' ##Use multicolumns in latex. Could also pass a vector of columns 41 | #' star_add_column_numbers(star.out, insert.after = 10, multicol = "c") 42 | #' ##Use multicolumn and don't skip the first column 43 | #' star_add_column_numbers(star.out, insert.after = 10, skip.col.1 = FALSE, multicol = "c") 44 | #' @export 45 | star_add_column_numbers <- function(star, insert.after, skip.col.1 = TRUE, add.space = FALSE, 46 | multicol = NULL) { 47 | 48 | if (!is.latex(star)) 49 | stop("star_add_column_numbers() currently only supported with latex") 50 | 51 | ##The number of columns 52 | num.cols <- star_ncol(star) 53 | 54 | ##If multicol is not null and has length 1, set it equal 55 | ##to the length of num.cols 56 | if (!is.null(multicol)) { 57 | if (length(multicol) == 1 && skip.col.1) { 58 | multicol <- rep(multicol, num.cols - 1) 59 | } else if (length(multicol) == 1) { 60 | multicol <- rep(multicol, num.cols) 61 | } 62 | } 63 | 64 | 65 | ##Get the first and last strings for use with multicolumn 66 | if (!is.null(multicol)) { 67 | ##Use multicolumns 68 | first.string <- paste0("\\multicolumn{1}{", multicol, "}{(") 69 | last.string <- paste0(")}") 70 | } else { 71 | ##No multicolumns 72 | first.string <- "(" 73 | last.string <- ")" 74 | } 75 | 76 | if (skip.col.1) { 77 | ##if skip.col.1 is TRUE, skip the first column 78 | 79 | ##get the column numbers 80 | col.nums <- paste0(first.string, 1:(num.cols - 1), last.string, collapse = " & ") 81 | ##add the first & for the first row and the end of the latex column 82 | col.nums <- paste0(" & ", col.nums, " \\\\ ") 83 | } else { 84 | ##use a column number for the first column 85 | 86 | ##get the column numbers 87 | col.nums <- paste0(first.string, 1:num.cols, last.string, collapse = " & ") 88 | ##add the first & for the first row and the end of the latex column 89 | col.nums <- paste0(col.nums, " \\\\ ") 90 | } 91 | 92 | if (add.space) { 93 | ##Add the space as in the normal stargazer output 94 | col.nums <- paste0("\\\\[-1.8ex] ", col.nums) 95 | } 96 | 97 | return(star_insert_row(star, col.nums, insert.after = insert.after)) 98 | 99 | } 100 | 101 | 102 | #' A function to replace asterisks with latex compatible values 103 | #' 104 | #' @param star the \code{stargazer} output 105 | #' @return updated stargazer output with latex compaitble 106 | #' asterisks 107 | #' @export 108 | star_asterisks <- function(star) { 109 | 110 | star <- gsub("(\\\\textasteriskcentered\\s?){3}", "$^{***}$", star) 111 | star <- gsub("(\\\\textasteriskcentered\\s?){2}", "$^{**}$", star) 112 | star <- gsub("(\\\\textasteriskcentered\\s?){1}", "$^*$", star) 113 | 114 | return(star) 115 | } 116 | 117 | 118 | #' Insert a row in a stargazer table 119 | #' 120 | #' @param star the \code{stargazer} output 121 | #' @param string the string of text to insert into the table 122 | #' @param insert.after insert \code{string} after 123 | #' \code{inster.after}. \code{insert.after} can be a vector, so 124 | #' you can insert multiple strings at one time. If 125 | #' \code{insert.after} has length 1, then all elements of 126 | #' \code{string} are insterted after \code{insert.after}. 127 | #' @return the updated \code{stargazer} output 128 | #' @examples 129 | #' ## -- Regression Example -- ## 130 | #' library(stargazer) 131 | #' data(mtcars) 132 | #' mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars) 133 | #' mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars) 134 | #' mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars) 135 | #' ##latex example 136 | #' star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3, 137 | #' type = "latex") 138 | #' print(star.out) 139 | #' star_insert_row(star.out, "Controls? & No & No & No \\\\", insert.after = 27) 140 | #' star_insert_row(star.out, 141 | #' c("Controls? & No & No & No \\\\", 142 | #' "Linear Model? & Yes & Yes & Yes \\\\"), 143 | #' insert.after = c(27, 32)) 144 | #' @export 145 | star_insert_row <- function(star, string, insert.after) { 146 | 147 | if (length(string) < length(insert.after)) { 148 | stop("Error: in star_insert_row() insert.after has more elements than string") 149 | } 150 | 151 | if (length(insert.after) == 1) { 152 | ##If the length of insert.after is 1, use the append function 153 | return(append(star, string, after = insert.after)) 154 | } 155 | ##c(star[1:insert.after], string, star[(insert.after + 1):length(star)])) 156 | 157 | if (length(string) > length(insert.after)) { 158 | ##string has more elements than Insert after 159 | ##fill insert.after with its last element so its length 160 | ##matches string 161 | length.diff <- length(string) - length(insert.after) 162 | insert.after <- c(insert.after, 163 | rep(insert.after[length(insert.after)], length.diff)) 164 | } 165 | 166 | ##From http://stackoverflow.com/a/1495204/1317443 167 | id <- c(seq_along(star), insert.after + 0.5) 168 | star <- c(star, string) 169 | star <- star[order(id)] 170 | return(star) 171 | } 172 | 173 | 174 | #' get the number of stargazer columns 175 | #' 176 | #' @param star the \code{stargazer} output 177 | #' @return the number of columns in the \code{stargazer} output 178 | #' @examples 179 | #' library(stargazer) 180 | #' data(mtcars) 181 | #' star.out <- stargazer(mtcars) 182 | #' star_ncol(star.out) 183 | #' @export 184 | star_ncol <- function(star) { 185 | 186 | max.num.ampersands <- max(stringr::str_count(star, "&")) 187 | max.escaped.ampersands <- max(stringr::str_count(star, "\\\\&")) 188 | 189 | num.cols <- max.num.ampersands - max.escaped.ampersands + 1 190 | 191 | return(num.cols) 192 | } 193 | 194 | #' A function to for scientific unit columns from the latex 195 | #' \code{siunitx} package 196 | #' 197 | #' The latex \code{siunitx} package has to be loaded in the preamble. 198 | #' See this answer on tex stackexchange: 199 | #' \url{http://tex.stackexchange.com/a/2747} This function is useful 200 | #' with \code{paste0} when updating column to work \code{siunitx} and 201 | #' aligning decimals. 202 | #' 203 | #' @param table.format the latex \code{siunitx}. For example \code{3.2} 204 | #' is three integers and two decimals. \code{table.format} can be a 205 | #' string or numeric. 206 | #' @param rep.times the number of times to repeat the given column 207 | #' @export 208 | star_si_col <- function(table.format, rep.times = 1) { 209 | out <- paste0("S[table-format=", table.format, "]") 210 | if (rep.times > 1) { 211 | out <- paste(rep(out, rep.times), collapse = "") 212 | } 213 | return(out) 214 | } 215 | 216 | #' Turn a latex table into a \code{sidewaystable} latex table 217 | #' 218 | #' @param star the \code{stargazer} output 219 | #' @return the latex table turned sideways 220 | #' @examples 221 | #' library(stargazer) 222 | #' data(mtcars) 223 | #' star.out <- stargazer(mtcars) 224 | #' star_sidewaystable(star.out) 225 | #' @export 226 | star_sidewaystable <- function(star) { 227 | star <- sub("begin\\{table\\}", "begin{sidewaystable}", star) 228 | star <- sub("end\\{table\\}", "end{sidewaystable}", star) 229 | return(star) 230 | } 231 | 232 | -------------------------------------------------------------------------------- /README.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |

starpolishr

30 |

This package allows for easy post polishing of latex stargazer output. See this vignette for a quick introduction. For examples, see the Tables section of my papers here and here. All of these tables were generated exclusively within R using stargazer and starpolishr.

31 |

Installation

32 |

You can install starpolishr from github with:

33 |
# install.packages("devtools")
34 | devtools::install_github("ChandlerLutz/starpolishr")
35 |

Quick Tips

36 | 40 |

Key Functions

41 |

Here is a list of key starpolishr functions in order of how often I use them. See their help files for more details.

42 | 51 |

Authors

52 |

Chandler Lutz

53 |

License

54 |

GPL (>= 2)

55 | 56 | 57 | 58 | --------------------------------------------------------------------------------