CommunityData:Knitr: Difference between revisions

From CommunityData
No edit summary
Line 1: Line 1:
Knitr is a package that allows you to import dataframes from R and dynamically generate LaTeX tables in your paper. Using knitr is good practice for times when you're working on a paper and make a slight change to your analysis, and need to update every instance where you've reported results.
Knitr is a package that allows you to import dataframes from R and dynamically generate LaTeX tables in your paper. Using knitr is good practice for times when you're working on a paper and make a slight change to your analysis, and need to update every instance where you've reported results.


== Preparation ==
== Preparation ==
Line 18: Line 17:
Finally, append each table dataframe to a list (i.e. <code>results$summary.stats <- summary.stats</code>), and save this list as an .Rdata file.
Finally, append each table dataframe to a list (i.e. <code>results$summary.stats <- summary.stats</code>), and save this list as an .Rdata file.


== Preamble ==
== Latex Preamble ==
 
Save the .Rdata file in the same folder as your tex document. Now, include a preamble like this in your tex file (by all your <code>\usepackage</code> commands):


<<init, echo=FALSE, message=FALSE>>=
knit_hooks$set(document = function(x) {
  sub('\\usepackage[]{color}',
'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE)
})
@




== Documentation ==
== Documentation ==

Revision as of 01:11, 26 August 2016

Knitr is a package that allows you to import dataframes from R and dynamically generate LaTeX tables in your paper. Using knitr is good practice for times when you're working on a paper and make a slight change to your analysis, and need to update every instance where you've reported results.

Preparation

First, you'd want to create an R script that's organized around every table you want to create in your document. The script should create separate dataframes for every one of them. If your table is going to have rownames, include a vector of rownames in your data frame.

Your data frame could look something like this:

summary.stats <- data.frame(cbind(
                             c("Var 1", "Var 2", "Var 3"),
                             c(min(d$var.1), min(d$var.2), min(d$var.3)),
                             c(median(d$var.1), median(d$var.2), median(d$var.3)),
                             c(max(d$var.1), max(d$var.2), max(d$quality.score))
                                                 ))


Finally, append each table dataframe to a list (i.e. results$summary.stats <- summary.stats), and save this list as an .Rdata file.

Latex Preamble

Save the .Rdata file in the same folder as your tex document. Now, include a preamble like this in your tex file (by all your \usepackage commands):

<<init, echo=FALSE, message=FALSE>>=
knit_hooks$set(document = function(x) {
 sub('\\usepackage[]{color}',
'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE)
})
@


Documentation