Beautiful table outputs in R, part 2 #rstats #sjPlot

First of all, I’d like to thank my readers for the lots of feedback on my last post on beautiful outputs in R. I tried to consider all suggestions, updated the existing table-output-functions and added some new ones, which will be described in this post. The updated package is already available on CRAN.

This posting is divided in two major parts:

  1. the new functions are described, and
  2. the new features of all table-output-functions are introduced (including knitr-integration and office-import)

New functions

First I want to give an overview of the new functions. As you may have noticed, all table-output-functions have new parameters, which enable you to modify the appearance and retrieve objects for knitr-integration and so on. This is described below.

Viewing imported SPSS data sets

As I have mentioned some times before, one purpose of this package is to make it easier for (former) SPSS users to switch to and use R. Beside the data import functions (see all functions beginning with sji) I now added two functions, where one is specifically useful for SPSS data sets, while the other one is generally useful for data frames.

With the function sji.viewSPSS you can easily create a kind of “code plan” for your data sets. Note that this function only works for SPSS data sets that have been imported using the sji.SPSS function (because else variable and value label attributes are missing)! The function call is quite simple. Load the library with require(sjPlot) and run the following example:


This will give you an overview of: Variable number, variable name, variable label, variable values and value labels:
You can suppress the output of values and value labels if you just want to quickly inspect the variable names. The table can also be sorted either by variable number or by variable name.

Description and content of data frames

If you want to inspect the data frame’s variables, you can use the sjt.df function. By default, this function calls the describe-function from the psych-package and prints the output as HTML-table:


If you set the parameter describe=FALSE, you can view the data frame’s content instead. See this example, where alternate row colors are activated and the table is ordered by column “e42dep”:

sjt.df(efc[1:20,1:5], alternateRowColors=TRUE,
       orderColumn="e42dep", describe=FALSE)

Be careful when applying this function to large data frames, because it becomes very slow then…

Principal Component Analysis and Correlations

Two more new functions are sjt.pca for printing results of principal component analyses and sjt.corr for printing correlations. Printing PCA results will give you an overview of all extracted factors, where the highest factor loading is printed in black, while the other factor loadings are a bit faded (thus, it’s easier to see which item belongs to which factor). Furthermore, you can print the MSA for each item, the Cronbach’s Alpha value for each “scale” and other statistics:

# retrieve variable and value labels
varlabs <- sji.getVariableLabels(efc)
# recveive first item of COPE-index scale
start <- which(colnames(efc)=="c82cop1")
# recveive last item of COPE-index scale
end <- which(colnames(efc)=="c90cop9")
# create data frame with COPE-index scale
df <-[,c(start:end)])
colnames(df) <- varlabs[c(start:end)]
sjt.pca(df, showMSA=TRUE, showVariance=TRUE)


The next example is a correlation table. Note: This table may look more beautiful if opened in a web browser (because of more space). And second note: See the usage of the CSS-parameter! (more on this later)

sjt.corr(df, pvaluesAsNumbers=TRUE, 
CSS=list(css.thead="border-top:double black; font-weight:normal; font-size:0.9em;",
         css.firsttablecol="font-weight:normal; font-size:0.9em;"))


Stacked frequencies and Likert scales

The last new table-output-function is sjt.stackfrq, which prints stacked frequencies of (Likert) scales.

# recveive first item of COPE-index scale
start <- which(colnames(efc)=="c82cop1")
# recveive first item of COPE-index scale
end <- which(colnames(efc)=="c90cop9")
# retrieve variable and value labels
varlabs <- sji.getVariableLabels(efc)
vallabs <- sji.getValueLabels(efc)

Similar to the sjp.stackfrq function (see this posting), you can order the items according to their lowest / highest first value etc.

Tweaking the table-output-functions and integrating output into knitr

In this section, important new parameters of the table-output-functions are described.

Each sjt function as well as sji.viewSPSS now have following parameters:

  • CSS
  • useViewer
  • no.output

And all of them (invisibly) return at least following values:

  • the web page style sheet (,
  • the web page content (page.content),
  • the complete html-output (output.complete) and
  • the html-table with inline-css for use with knitr (knitr)

Parameters explained

The table-output is in HTML format, using cascading style sheets to modify the appearance of tables. You can inspect the and page.content parameters to see which CSS classes are used in the HTML-table, for instance:

> value <- sjt.df(efc)
> value$
[1] "<style>\ntable { border-collapse:collapse; border:none; }\ncaption { font-weight: bold; text-align:left; }\n.thead { border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; }\n.tdata { padding:0.2cm; text-align:left; vertical-align:top; }\n.arc { background-color:#eaeaea; }\n.lasttablerow { border-top:1px solid; border-bottom: double; }\n.firsttablerow { border-bottom:1px solid; }\n.leftalign { text-align:left; }\n.centertalign { text-align:center; }\n.firsttablecol {  }\n.comment { font-style:italic; border-top:double black; text-align:right; }\n</style>"

To use the CSS parameter, you must define a list with values, where the value-name equals the css-class-name with css. prefix. If you want to change the appearance of the first table column (with variable names), use:

sjt.df(efc, CSS=list(css.firsttablecol="color:blue;font-style:italic;"))

Note that each style-definition in the parameter-list has to end with ;, because sometimes style-attributes are concatenated and thus need this separator char. Refer to the function-help to see more examples…

useViewer and no.output
With useViewer set to FALSE, you can simply force opening the html-table-output in a web browser, even if a viewer is available. With no.output set to TRUE, you can suppress the table output completely. This is useful if you want to integrate the tables in your knitr-documents…

Knitr integration

As said above, each sjt-function returns an object where you can access the created html-output. The $knitr object contains the pure html-table (without HTML-pageheader or body-tags) with inline CSS (thus, no class-attributes are used). This allows the simple integration into knitr-documents. Use following code snippet in your knitr-documents and knit it to HTML:

`r sjt.df(efc, no.output=TRUE)$knitr`

Office import improvements

When setting the file parameter, the table-ouput is saved to a file. This can be opened via MS Word, LibreOffice Writer etc. The import has been improved, so the imported table should render properly now.

Last Words…
Well, enough said. ;-) All feature available in the latest sjPlot-package.

About these ads

26 Gedanken zu “Beautiful table outputs in R, part 2 #rstats #sjPlot

  1. Awesome work! Thank you very much..

  2. […] article was first published on Strenge Jacke! » R, and kindly contributed to […]

  3. […] Outra dica para você, que gosta deste negócio de integrar R em relatórios. […]

  4. […] Beautiful table outputs in R, part 2 #rstats #sjPlot […]

  5. Awesome work! Can we also get LATEX output for using it in .Rnw files ?

    • Thank you for your feedback! I originally focused on HTML because 1) I wanted to use the viewer pane as alternative view to the console output and 2) HTML-tables can be easily imported into Office apps, while LaTex / PDF cannot be easily imported. Supporting LaTex would mean quite some work and I would also have to become more familiar with LaTex syntax to properly implement LaTex support. I’m afraid and sorry to say that I’m not able to do this in the near future.

      • Thank you for your answer. Could you please help on how to use a different alternate row color than light gray ?

      • Yes, you can use the CSS parameter: CSS=list(css.arc="background-color:#993333;") (don’t forget the ; at the end). arc stands for alternate row color. Or you can turn it completely off with the related parameter (alternateRowColor, i’m not sure about the spelling, because i’m out of Office to check).

  6. Once again, brilliant stuff.. Thank you so much for your work and release of great tools!

  7. […] First of all, I'd like to thank my readers for the lots of feedback on my last post on beautiful outputs in R. I tried to consider all suggestions, updated the existing table-output-functions and a…  […]

  8. […] Note: There’s a second part of this series here. […]

  9. This is great. I’ve always wondered how to get things to show up in the viewer. This makes copy paste into excel really easy as well. Are you on github?

  10. Hat dies auf New Hampshire R Users Group (NH UseRs) rebloggt und kommentierte:
    Nice post on making nice tables in R, which is generally more of a challenge than making pretty figures.

  11. Matheus Abreu

    Hi guy, it is a really good job. Congrats! I have one doubt about how to use sjt.df() integrated to knitr chunk multiline, inside a for statement. Can you help me?

  12. Decimal point alignment in tables would be a nice feature.

  13. […] source, binaries follow). While the last two updates included new functions for table outputs (see here and here for details on these functions), the current update only provides small helper functions […]

  14. […] again. Especially the table output functions (see my previous blog posts on table output functions here and here) improved a lot. Tables now have more and better possibilities for style customization and […]

  15. […] again. Especially the table output functions (see my previous blog posts on table output functions here and here) improved a lot. Tables now have more and better possibilities for style customization and […]

  16. Your tips for knitting tables were awesome. Do you have any advice for knitting the graphs?

  17. Extremely good work. Keep it up.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ photo

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s