knitr::opts_chunk$set(echo = TRUE) # create CLO file required by the Latex template CTUtemplate::use_ubreportclo( # Personal info sign = params$author, email = params$email, job = params$job, # Project info projnum = params$projnum, projname = params$projname, # Report info version = params$version, reporttype = params$reportname )
cat(file = 'preamble.tex', "\\section*{Administrative Information} \n \n", "\\begin{center}\n\\begin{tabular}{l|c}\n", "Project number & ", params$projnum, " \\\\ \n", "SAP version & x.x, dated dd.mm.yyyy \\\\ \n", "Protocol version & x.x, dated dd.mm.yyyy \\\\ \n", "Other document & \\\\ \n", "Date of export & ", params$export_date, " \\\\ \n", "\\end{tabular} \n \\end{center}" )
```{cat, engine.opts=list(file = 'preamble2.tex')} \section*{Revision History}
\begin{center} \begin{tabular}{p{3cm}|p{6cm}|p{6cm}} Release date & Summary of changes & Reason for change(s) \ \hline dd.mm.yyyy & Initial version & Not applicable \ \end{tabular} \end{center}
\chapter{Latex syntax can be used} A chapter is created like this... ```r \chapter{Latex syntax can be used}
It is in fact the easiest (only?) way to get chapters...
Otherwise, document sections, subsections etc can be created using Rmd syntax
# section ## subsection ### subsubsection
Use '{-}' to signify that a section should not be numbered.
For chapters, place an asterisk after 'chapter' to use an unnumbered chapter.
\chapter*{Latex syntax can be used}
\chapter{Tables}
Tables can be created via multiple approaches. Short tables that can be edited by hand can be written in Markdown
| Tables | Are | Cool | | ------------- |:-------------:| -----:| | col 3 is | right-aligned | $1600 | | col 2 is | centered | $12 | | zebra stripes | are neat | $1 |
| Tables | Are | Cool | | ------------- |:-------------:| -----:| | col 3 is | right-aligned | $1600 | | col 2 is | centered | $12 | | zebra stripes | are neat | $1 |
btabler is another option. Here, you should use 'results='asis'' in the chunk option
# remotes::install_github("CTU-Bern/btabler") library(btabler) df <- data.frame(name = c("", "Row 1", "Row2"), out_t = c("Total", "t1", "t1"), out_1 = c("Group 1", "g11", "g12"), out_2 = c("Group 2", "g21", "g22")) btable(df, nhead = 1, nfoot = 0, caption = "Table1")
other popular options are xtable
and knitr::kable
.
Flextable should also be possible to use (there seems to be some issue with the latex template - undefined control sequence...). Captions are/can be defined in the chunk header (see the flextable site for more info).
library(flextable) flextable(head(mtcars))
It is possible to do lots of formatting with Flextable...
library(magrittr) set_flextable_defaults(font.size = 8) dat <- head(ggplot2::diamonds, n = 10) qflextable(dat) %>% color(~ price < 330, color = "orange", ~ price + x + y + z ) %>% bold(j = c("price", "x"), bold = TRUE) %>% bg(bg = "#e05297", part = "header")
When compiled, Rmd files assume that any paths mentioned are relative to the location of the Rmd. In contrast, when running individual chunks, it paths relative to the current working directory. What that means is that if your working directory is .
and your Rmd is stored in ./report
with data in ./data
and you use data/file.csv
in your Rmd, it'll work fine when you run in interactive mode (one chunk at a time), but it will not compile when knit - then it'll be looking for .report/data/file.csv
, which doesnt exist.
The solution is the here
package. here
contains the function here
which is a drop in replacement for file.path
with the difference that it always looks for a .Rproj
file and uses that location as the reference point. here::here('data', 'file.csv')
will always point to the same location. It is recommended to put the .Rproj
file on level above the script folders (i.e. in the 06_Analysis_XX_YY folder). This avoids going up and down the folder structure.
Markdown is great for literate programming. It can, however result in very lengthy documents, which doesn't make things easier to work with. One solution to this is to use child documents, which are secondary Rmd files, which should be included in the main document. Child Rmds do not need the YAML header.
Were the following to be saved into a file (e.g. foo.Rmd), it could be included into this document via the child
argument to an R chunk (e.g. uncomment the following code)
start foo.Rmd
:
whatever prose you want
# do stuff
end foo.Rmd
Another solution would be using normal R scripts, which can be sourced:
source(here::here("rscript", "file.R"))
As mentioned in the previous section, the here
package can be used to make paths that are robust to changes in the working directory.
Markdown itself doesnt seem to be able to be able to rotate pages, but Latex can. Surround the part that should be rotated with \begin{landscape}
and \end{landscape}
.
\begin{landscape} stuff that should be in landscape (probably tables...) \end{landscape}
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
You can also embed plots, for example:
plot(pressure)
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Stata code can also be used in Rmd files.
If stata doesn't exist in the PATH variable, set the path via knitr::opts_chunk$set
.
knitr::opts_chunk$set(engine.path = list(stata = "C:/Program Files/Stata17/StataBE-64.exe"))
The Statamarkdown
package adds extra capabilities (like collecting code as each chunk is ran in a fresh Stata session).
# remotes::install_github("Hemken/Statamarkdown") library(Statamarkdown)
Then, rather than using {r}
, use {stata}
as the header of the chunk.
sysuse auto summ *
Use the collectcode=TRUE
chunk option to use code from previous chunks (e.g. loading data).
```{stata, collectcode = TRUE} sysuse auto summ *
```{stata} regress mpg weight
\chapter{Reproducibility}
# install.packages('svn', repos = 'https://ctu-bern.r-universe.dev') library(svn)
CTU Bern uses the SVN version control system. This report is based on revision r sub(paste("Folder", getwd(), "is at revision"), "", svn_details())
.
R packages used in the analysis:
options(width = 90) sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.