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.
Related
I am trying to include a table in a Word document but with no results. What I get in the resulting document is just a text list of the elements of the table including column names.
My code is organized in two scripts:
Script calling Markdown file KG-report.Rmd
rmarkdown::render("KG-report.Rmd", params = list(
pest.name = pest.name),
output_file = paste0("Report-", pest.name, ".docx")
Markdown script KG-report.Rmd
---
title: "Some title"
author: "me"
date: "`r Sys.Date()`"
output:
officedown::rdocx_document:
reference_docx: Simple_master_template_opinions.docx
tables:
style: Table
params:
pest.name: "Default pest"
---
```{r process-map, echo=FALSE, warning=FALSE, message=FALSE, fig.keep='all'}
# create and check directories
source("R_scripts\\PEST.PROFILE.check.pest.directories.r", local = knitr::knit_global())
# download distribution tables or load reviewed distribution table
source("R_scripts\\PEST.PROFILE.web.EPPO.distribution.table.r", local = knitr::knit_global())
knitr::kable(pest.kg.table)
etc...
After many tests and after following suggestions on word formatting (e.g. here) which did not solve the issue, it looks like the problem is related to looping on
rmarkdown::render()
If I do not use it the table is printed....
Is there any known issue about this or am I just using it in the wrong way?
Thank you
UPDATE - SOLUTION FOUND
In the end I was able to find a solution. It looked like the kable function was printing a html table in the word file which resulted in text with no table formatting. Adding the argument format = "pipe" solved the issue.
The advice in bookdown for formatting long figure captions and other text is to create a reference,
(ref:foo) Title with _formatting_.
With a blank line above and below, and then referring to this in the figure caption.
I'm not getting this to work for theorem environments like example, however, when knitting to pdf.
Reproducible example:
---
title: "Table test"
output:
bookdown::pdf_document2:
keep_tex: true
---
# Test
(ref:foo) This is a test.
```{exercise revision1, echo = T, name="(ref:foo)"}
Some exercise description.
```
When knitted as a pdf:
rmarkdown::render("test.rmd")
Results in,
Exercise 1.1 ((ref:foo)). Some exercise description.
This is a bug in bookdown that I just fixed on Github. For now, you may try the development version of bookdown:
remotes::install_github(c('yihui/knitr', 'rstudio/bookdown'))
the codes in rmarkdown is:
rnorm(1)
Assume the result is 0.23. I want to save this 0.23 to my word document.
After cache=TRUE was set, the result every time I knit to word is the same but not equal to the current output 0.23.
How do I fix the current output and knit it to word?
Please do not use set.seed(). Because the rnorm is just a simple example for a procedure which has different outputs every time it runs and set.seed may not work.
Please do not cite variables in rmarkdown to fix an output.Citing does't work when the output in rmarkdown can't be cited such as a summary in a model. And all you have is a summary output and you can't cite it. In lm(linearmodel) you can cite every element in it while in many other models there is no such attributes thus you can't cite.
So fixing the current output is very important. Is it possible in rmarkdown?
I may be oversimplifying this, but is something like the following .Rmd not what you're after?
---
title: "Saving RNorm"
author: "John Doe"
date: "3 January 2019"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Setting the value
```{r}
random <- rnorm(1)
plot(random)
```
## Using the value
As you can see in the chart above, the random number I produced is `r random`.
You can assign your random value to a variable, use it in whatever analysis or plot you need to, and then cite it in the text body by wrapping it in back quotes (prefixed with r).
The output this produces is below.
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" )
```
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!