to_latex: Print LaTeX Tables that Mimic the Look of SPSS Output

View source: R/to_latex.R

to_latexR Documentation

Print LaTeX Tables that Mimic the Look of SPSS Output

Description

Use information from an R object to print a LaTeX table that mimics the look of SPSS output. Typically, one would first call to_SPSS with an object returned by a function in r2spss, and then call to_latex with the resulting object of class "SPSS_table" to print the LaTeX table. Note that the print methods in r2spss perform these two steps at once, but calling to_SPSS and to_latex separately can be useful for customization of the LaTeX table.

Usage

to_latex(object, ...)

## S3 method for class 'SPSS_table'
to_latex(object, version = r2spss_options$get("version"), ...)

## S3 method for class 'data.frame'
to_latex(
  object,
  main = NULL,
  sub = NULL,
  header = TRUE,
  label = NULL,
  row_names = TRUE,
  info = NULL,
  alignment = NULL,
  border = NULL,
  footnotes = NULL,
  major = NULL,
  minor = NULL,
  version = r2spss_options$get("version"),
  ...
)

Arguments

object

an object of class "SPSS_table" as returned by to_SPSS methods, or a data.frame.

...

for the "data.frame" method, additional arguments to be passed to format_SPSS. For the "SPSS_table" method, additional arguments are currently ignored.

version

a character string specifying whether the table should mimic the look of recent SPSS versions ("modern") or older versions (<24; "legacy"). For the "SPSS_table" method, note that also the content of some tables generated by functions in r2spss is different for current and older SPSS versions. These objects contain a component "version" which will passed to the "data.frame" method to ensure that the content and look of the table match. Other tables have the same content irrespective of the SPSS version, and this argument controls the look of those tables. The default is to inherit from the global option within the current R session (see r2spss_options).

main

a single character string defining the main title of the SPSS table, or NULL to suppress the main title.

sub

a single character string defining the sub-title of the SPSS table, or NULL to suppress the sub-title.

header

a logical indicating whether to include a header in the SPSS table based on the column names of object (defaults to TRUE). Alternatively, it is possible to supply a character vector giving the header of each column, or a list defining a complex header layout with merged header cells. In the latter case, the list can have up to three components, with each component defining one level of the header. The last list component should be a character vector giving the bottom-level header of each column. The other list components should be data frames with the following columns:

first

an integer vecot giving the first column of each (merged) header cell.

last

an integer vector giving the last column of each (merged) header cell.

text

a character vector containing the text of each (merged) header cell.

Line breaks (character \n) can be included to wrap the text of a header cell over several rows.

label

a character string giving a label to be added as the first column of the table, or NULL to suppress such a column. In many SPSS tables, this contains the name of a variable used in the analysis.

row_names

a logical indicating whether to add the row names of object as a column in the SPSS table (defaults to TRUE). Alternatively, it is possible to supply a character vector giving the row labels to be added as a column. Line breaks (character \n) can be included to wrap the text of a row label over several rows.

info

an integer giving the number of columns in the SPSS table that contain auxiliary information on the results. This has an effect of the default formatting, alignment, and borders. The default is 0 if row_names is FALSE and 1 otherwise. Note that a column defined by label and a column defined by row_names are always added to info if the former are supplied.

alignment

a list with components header and table, with each component being a character vector that contains the LaTeX alignment specifiers of the header and table body, respectively, of each column. Permissible alignment specifiers are "l" for left-aligned, "c" for centered, and "r" for right aligned. The default is left-aligned for the header and table body of the columns containing auxiliary information, and centered and right-aligned, respectively, for the header and table body of the columns containing the actual results. It should not be necessary to set the column alignment manually.

border

a logical vector indicating which (outer and inner) vertical borders should be drawn. The default is that tables that mimic recent versions of SPSS (version = "modern") draw only borders in between columns that contain the actual results, whereas tables that mimic older versions of SPSS (version = "legacy") draw all borders except in between columns containing auxiliary information. It should not be necessary to set the vertical borders manually.

footnotes

a character vector giving footnotes to be added below the SPSS table, or NULL to suppress footnotes. Alternatively, it is possible to supply a data frame with the following columns:

marker

character vector giving footnote markers to be included in a cell of the SPSS table. For footnotes without a marker, an empty character string can be used.

row

an integer vector specifying the row of the SPSS table in which to include each footnote marker, or NA for footnotes without a marker. In addition, the character strings "main" and "sub" can be used to include footnote markers in the main title and sub-title, respectively.

column

an integer vector specifying the column of the SPSS table in which to include each footnote marker, or NA for footnotes without a marker or footnote markers in the main title or sub-title.

text

a character vector containing the text of each footnote.

major, minor

an integer vector specifying the rows of the SPSS table after which to draw major or minor grid lines that stretch across all columns of the table, or NULL to suppress the respective grid lines. Alternatively, each of these arguments can be a data frame with the following columns defining partial grid lines:

row

an integer vector specifying the rows of the SPSS table after which to draw grid lines.

first

an integer vector specifying the first column of each partial line.

last

an integer vector specifying the last column of each partial line.

The only difference between the two type of grid lines is that minor grid lines can also be suppressed globally within the current R session by setting r2spss_options$set(minor = FALSE), see r2spss_options. Also note that these arguments only control the grid lines in between rows of the table body. Horizontal table borders are always drawn.

Details

The "SPSS_table" method takes component table of the object and supplies it to the data.frame method, with additional components in the object being passed as additional arguments.

The "data.frame" method allows to extend the functionality of r2spss with additional LaTeX tables that mimic the look of SPSS output.

Value

Nothing is returned, the function is called for its side effects.

Note

LaTeX tables that mimic recent versions of SPSS (version = "modern") may require several LaTeX compilations to be displayed correctly.

Author(s)

Andreas Alfons

Examples

## Kruskal-Wallis test example

# load data
data("Eredivisie")

# compute a Kruskual-Wallis test to investigate whether
# market values differ by playing position
kw <- kruskal_test(Eredivisie, "MarketValue",
                   group = "Position")

# convert to an object of class "SPSS_table" that
# contains the table with the test results
kw_spss <- to_SPSS(kw, statistics = "test")
kw_spss

# blank out the number of degrees of freedom to ask
# an assignment question about it
kw_spss$table[2, 1] <- "???"

# print the LaTeX table to be included in the assignment
to_latex(kw_spss)


## t test example

# load data
data("Exams")

# test whether the average grade on the resit
# differs from 5.5 (minimum passing grade)
t <- t_test(Exams, "Resit", mu = 5.5)

# convert to an object of class "SPSS_table" that
# contains the table with the test results
t_spss <- to_SPSS(t, statistics = "test")

# this is an example of a complex header layout
t_spss$header

# add additional line breaks in bottom-level header
t_spss$header[[3]] <- gsub("-", "-\n", t_spss$header[[3]],
                           fixed = TRUE)

# print the LaTeX table
to_latex(t_spss)


r2spss documentation built on May 25, 2022, 5:05 p.m.