R Markdown automatically makes title - r

I'm about to write my master's thesis in R Markdown and when I knit to pdf there is the standard title which one gets in LaTeX with '\maketitle'.
The autor, the title and the name. Quite ugly.
I don't know how to delete that ...
Thank you!

Overview
You can modify your YAML header in many ways. Please read the PDF Documents section within RStudio's RMarkdown page.
Reproducible Example
When you first create an .rmd file, this is the default YAML header:
---
title: "Default_YAML"
author: "Daffy Duck"
date: "2/17/2018"
output: pdf_document
---
# Header
Some text.
```{r}
df <- read.csv( file = "somefilepath.csv" )
```
You can customize what does and does not appear within your YAML header. Below is an example of me deleting the entire thing:
# Header
Some text.
```{r}
df <- read.csv( file = "somefilepath.csv" )
```

Related

Run R markdown (.Rmd) from inside other R script to produce HTML

As the example, if you create a new R markdown file and save it as 'test'. Can one then run or deploy this test.Rmd file from within a normal R script. The purpose being to generate the output in HTML, without having to open the .Rmd file.
I'm hoping to create one master file to do this for many markdown files in one go; which would save considerable time as you then don't have to open many markdown files and wait for each one to complete.
You are looking for rmarkdown::render().
Contents of "test.Rmd"
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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:
```{r cars}
summary(cars)
```
Contents of script.R
# provided test.Rmd is in the working directory
rmarkdown::render("test.Rmd")
A Way to Render Multiple Rmd
cwd_rmd_files <- list.files(pattern = ".Rmd$")
lapply(cwd_rmd_files, rmarkdown::render)
Thanks the-mad-statter, your answer was very helpful. The issue I faced, required me to prepare markdown dynamically. By adapting your code, that's easily possible:
Contents of "test_dyn.rmd"
---
title: "Untitled"
output: html_document
---
The chunk below adds formatted text, based on your inputs.
```{r text, echo=FALSE, results="asis"}
cat(text)
```
The chunk below uses your input in as code.
```{r results}
y
```
Contents of "script_dyn.r"
in_text <- c("**Test 1**", "*Test 2*")
in_y <- 1:2
lapply(1:2, function(x) {
text <- in_text[[x]]
y <- in_y[[x]]
rmarkdown::render(input = "test_dyn.rmd", output_file = paste0("test", x))
})
Like this you can create files with different text and different variables values in your code.

Split rmarkdown presentation code over multiple text files [duplicate]

I have two files in the same folder: chapter1.Rmd and chapter2.Rmd, with the following content:
chapter1.Rmd
---
title: "Chapter 1"
output: pdf_document
---
## This is chapter 1. {#Chapter1}
Next up: [chapter 2](#Chapter2)
chapter2.Rmd
---
title: "Chapter 2"
output: pdf_document
---
## This is chapter 2. {#Chapter2}
Previously: [chapter 1](#Chapter1)
How can I knit these so that they combine into a single pdf output?
Of course, render(input = "chapter1.Rmd", output_format = "pdf_document") works perfectly but render(input = "chapter1.Rmd", input = "chapter2.Rmd", output_format = "pdf_document") does not.
Why do I want to do this? To break up a giant document into logical files.
I've used #hadley 's bookdown package to build latex from .Rmd but this seems like overkill for this particular task. Is there a simple solution using knitr/pandoc/linux command line I'm missing? Thanks.
August, 2018 update: This answer was written before the advent of bookdown, which is a more powerful approach to writing Rmarkdown based books. Check out the minimal bookdown example in #Mikey-Harper's answer!
When I want to break a large report into separate Rmd, I usually create a parent Rmd and include the chapters as children. This approach is easy for new users to understand, and if you include a table of contents (toc), it is easy to navigate between chapters.
report.Rmd
---
title: My Report
output:
pdf_document:
toc: yes
---
```{r child = 'chapter1.Rmd'}
```
```{r child = 'chapter2.Rmd'}
```
chapter1.Rmd
# Chapter 1
This is chapter 1.
```{r}
1
```
chapter2.Rmd
# Chapter 2
This is chapter 2.
```{r}
2
```
Build
rmarkdown::render('report.Rmd')
Which produces:
And if you want a quick way to create the chunks for your child documents:
rmd <- list.files(pattern = '*.Rmd', recursive = T)
chunks <- paste0("```{r child = '", rmd, "'}\n```\n")
cat(chunks, sep = '\n')
# ```{r child = 'chapter1.Rmd'}
# ```
#
# ```{r child = 'chapter2.Rmd'}
# ```
I'd recommend that people use the bookdown package for creating reports from multiple R Markdown files. It adds a lot of useful features like cross-referencing which are very useful for longer documents.
Adapting the example from #Eric, here is a minimal example of the bookdown setup. The main detail is that the main file has to be called index.Rmd , and must include the additional YAML line site: bookdown::bookdown_site:
index.Rmd
---
title: "A Minimal bookdown document"
site: bookdown::bookdown_site
output:
bookdown::pdf_document2:
toc: yes
---
01-intro.Rmd:
# Chapter 1
This is chapter 1.
```{r}
1
```
02-intro.Rmd:
# Chapter 2
This is chapter 2.
```{r}
2
```
If we Knit the index.Rmd bookdown will merge all the files in the same directory in alphabetical order (this behaviour can be changed using an extra _bookdown.yml file).
Once you get comfortable with this basic setup, it is easy to customise the bookdown document and output formats using additional configuration files i.e. _bookdown.yml and _output.yml
Further Reading
R Markdown: The definitive Guide: Chapter 11 provides a great overview of bookdown
Authoring books with bookdown provides a comprehensive guide on bookdown, and recommended for more advanced details.
This worked for me:
Rmd_bind <-
function(dir = ".",
book_header = readLines(textConnection("---\ntitle: 'Title'\n---")))
{
old <- setwd(dir)
if(length(grep("book.Rmd", list.files())) > 0){
warning("book.Rmd already exists")
}
write(book_header, file = "book.Rmd", )
cfiles <- list.files(pattern = "*.Rmd", )
ttext <- NULL
for(i in 1:length(cfiles)){
text <- readLines(cfiles[i])
hspan <- grep("---", text)
text <- text[-c(hspan[1]:hspan[2])]
write(text, sep = "\n", file = "book.Rmd", append = T)
}
render("book.Rmd", output_format = "pdf_document")
setwd(old)
}
Imagine there's a better solution and would be nice to have something like this in rmarkdown or knitr packages.

Set a header as the value of a variable in R markdown

I have an R markdown presentation that I would like to create different versions of. One of the things I was hoping to accomplish by doing this is changing the headers of a slide in the presentation based on some value that I've defined.
For example:
mytitle <- 'R Markdown Presentation'
I would like the value that is stored in mytitle to be the value that is used for the header. So the header would say "R Markdown Presentation". I have attempted the following solutions, but none have worked:
## title
## `title`
## eval(title)
```{r}
pres_title <- 'R Markdown Presentation'
pres_author <- 'Me'
pres_date <- Sys.Date()
```
---
title: `r pres_title`
author: `r pres_author`
date: `r pres_date`
output: html_document
---
Four years later and I was also looking for a clean way to do it, in a Notebook with R Studio. The original question was exactly what took me here. The answers helped, but I want to stress out that we can also do it almost as requested by Harrison Jones, in his example.
Define vars first:
myTitle1 <- 'R Markdown Presentation'
mySecondTitle2 <- 'Cool things regarding R notebooks'
And then apply them in markdown headers, along the text, as you wish, with inline R code:
# `r myTitle1`
## `r mySecondTitle2`
This is a R notebook example, with two headers and a paragraph.
You can also generate the entire header line, including markdown, by inline R code, like in the following example:
`r paste("#", myTitle1, sep=" ")`
`r paste("##", mySecondTitle2, sep=" ")`
This is a R notebook example, with two headers, a paragraph
and a beautiful table printed using knitr:
`r knitr::kable(cars)`
R Notebooks are a easy and powerfull.
When using R Studio, I prefer something like:
---
title: !r "An Example R Markdown Beamer Presentation"
author: !r "Me"
date: !r Sys.Date()
output: beamer_presentation
---
Inserting code before yaml header can interfere with output format. In this example I used beamer_presentation output so you can test it yourself.

Proper R Markdown Code Organization

I have been reading about R Markdown (here, here, and here) and using it to create solid reports. I would like to try to use what little code I am running to do some ad hoc analyses and turn them into more scalable data reports.
My question is rather broad: Is there a proper way to organize your code around an R Markdown project? Say, have one script that generates all of the data structures?
For example: Let's say that I have the cars data set and I have brought in commercial data on the manufacturer. What if I wanted to attach the manufacturer to the current cars data set, and then produce a separate summary table for each company using a manipulated data set cars.by.name as well as plot a certain sample using cars.import?
EDIT: Right now I have two files open. One is an R Script file that has all of the data manipulation: subsetting and re-categorizing values. And the other is the R Markdown file where I am building out text to accompany the various tables and plots of interest. When I call an object from the R Script file--like:
```{r}
table(cars.by.name$make)
```
I get an error saying Error in summary(cars.by.name$make) : object 'cars.by.name' not found
EDIT 2: I found this older thread to be helpful. Link
---
title: "Untitled"
author: "Jeb"
date: "August 4, 2015"
output: html_document
---
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:
```{r}
table(cars.by.name$make)
```
```{r}
summary(cars)
summary(cars.by.name)
```
```{r}
table(cars.by.name)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
plot(cars.import)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
There is a solution for this sort of problem, explained here.
Basically, if you have an .R file containing your code, there is no need to repeat the code in the .Rmd file, but you can include the code from .R file. For this to work, the chunks of code should be named in the .R file, and then can be included by name in the .Rmd file.
test.R:
## ---- chunk-1 ----
table(cars.by.name$make)
test.Rmd
Just once on top of the .Rmd file:
```{r echo=FALSE, cache= F}
knitr::read_chunk('test.R')
```
For every chunk you're including (replace chunk-1 with the label of that specific chunk in your .R file):
```{r chunk-1}
```
Note that it should be left empty (as is) and in run-time your code from .R will be brought over here and run.
Often times, I have many reports that need to run the same code with slightly different parameters. Calling all my "stats" functions separately, generating the results and then just referencing is what I typically do. The way to do this is as follows:
---
title: "Untitled"
author: "Author"
date: "August 4, 2015"
output: html_document
---
```{r, echo=FALSE, message=FALSE}
directoryPath <- "rawPath" ##Something like /Users/userid/RDataFile
fullPath <- file.path(directoryPath,"myROutputFile.RData")
load(fullPath)
```
Some Text, headers whatever
```{r}
summary(myStructure$value1) #Where myStructure was saved to the .RData file
```
You can save an RData file by using the save.image() command.
Hope that helps!

How to combine two RMarkdown (.Rmd) files into a single output?

I have two files in the same folder: chapter1.Rmd and chapter2.Rmd, with the following content:
chapter1.Rmd
---
title: "Chapter 1"
output: pdf_document
---
## This is chapter 1. {#Chapter1}
Next up: [chapter 2](#Chapter2)
chapter2.Rmd
---
title: "Chapter 2"
output: pdf_document
---
## This is chapter 2. {#Chapter2}
Previously: [chapter 1](#Chapter1)
How can I knit these so that they combine into a single pdf output?
Of course, render(input = "chapter1.Rmd", output_format = "pdf_document") works perfectly but render(input = "chapter1.Rmd", input = "chapter2.Rmd", output_format = "pdf_document") does not.
Why do I want to do this? To break up a giant document into logical files.
I've used #hadley 's bookdown package to build latex from .Rmd but this seems like overkill for this particular task. Is there a simple solution using knitr/pandoc/linux command line I'm missing? Thanks.
August, 2018 update: This answer was written before the advent of bookdown, which is a more powerful approach to writing Rmarkdown based books. Check out the minimal bookdown example in #Mikey-Harper's answer!
When I want to break a large report into separate Rmd, I usually create a parent Rmd and include the chapters as children. This approach is easy for new users to understand, and if you include a table of contents (toc), it is easy to navigate between chapters.
report.Rmd
---
title: My Report
output:
pdf_document:
toc: yes
---
```{r child = 'chapter1.Rmd'}
```
```{r child = 'chapter2.Rmd'}
```
chapter1.Rmd
# Chapter 1
This is chapter 1.
```{r}
1
```
chapter2.Rmd
# Chapter 2
This is chapter 2.
```{r}
2
```
Build
rmarkdown::render('report.Rmd')
Which produces:
And if you want a quick way to create the chunks for your child documents:
rmd <- list.files(pattern = '*.Rmd', recursive = T)
chunks <- paste0("```{r child = '", rmd, "'}\n```\n")
cat(chunks, sep = '\n')
# ```{r child = 'chapter1.Rmd'}
# ```
#
# ```{r child = 'chapter2.Rmd'}
# ```
I'd recommend that people use the bookdown package for creating reports from multiple R Markdown files. It adds a lot of useful features like cross-referencing which are very useful for longer documents.
Adapting the example from #Eric, here is a minimal example of the bookdown setup. The main detail is that the main file has to be called index.Rmd , and must include the additional YAML line site: bookdown::bookdown_site:
index.Rmd
---
title: "A Minimal bookdown document"
site: bookdown::bookdown_site
output:
bookdown::pdf_document2:
toc: yes
---
01-intro.Rmd:
# Chapter 1
This is chapter 1.
```{r}
1
```
02-intro.Rmd:
# Chapter 2
This is chapter 2.
```{r}
2
```
If we Knit the index.Rmd bookdown will merge all the files in the same directory in alphabetical order (this behaviour can be changed using an extra _bookdown.yml file).
Once you get comfortable with this basic setup, it is easy to customise the bookdown document and output formats using additional configuration files i.e. _bookdown.yml and _output.yml
Further Reading
R Markdown: The definitive Guide: Chapter 11 provides a great overview of bookdown
Authoring books with bookdown provides a comprehensive guide on bookdown, and recommended for more advanced details.
This worked for me:
Rmd_bind <-
function(dir = ".",
book_header = readLines(textConnection("---\ntitle: 'Title'\n---")))
{
old <- setwd(dir)
if(length(grep("book.Rmd", list.files())) > 0){
warning("book.Rmd already exists")
}
write(book_header, file = "book.Rmd", )
cfiles <- list.files(pattern = "*.Rmd", )
ttext <- NULL
for(i in 1:length(cfiles)){
text <- readLines(cfiles[i])
hspan <- grep("---", text)
text <- text[-c(hspan[1]:hspan[2])]
write(text, sep = "\n", file = "book.Rmd", append = T)
}
render("book.Rmd", output_format = "pdf_document")
setwd(old)
}
Imagine there's a better solution and would be nice to have something like this in rmarkdown or knitr packages.

Resources