I have a parent which includes a child file, and I'd like to compile the parent file, when I press Cmd+shift+k (I'm using rstudio). I know in latex you can reference a master file by writing a line of code at the top of your child file. I was wondering if you could do something similar with rmarkdown?
In R Markdown it works like this:
---
title: "parent"
output: pdf_document
---
This is the title page
```{r child='child1.Rmd'}
```
```{r child='child2.Rmd'}
```
The child .Rmd files are in the same folder in this example.
You do not need to specify a YAML header in the child .Rmd's
The knitr /demo/child/ page has an overview on how to do this.
If that's not informative enough, you can check out the knitr section of a thesis template repo's README.
Finally, you can see the parent-child model in action over in https://github.com/stevenpollack/masters_thesis/blob/master/masters_thesis.Rnw. A stripped down version of that link would be:
\documentclass[masters]{ucbthesis}
\begin{document}
<<abstract, child='abstract.Rnw'>>=
#
<<introduction, child='introduction.Rnw'>>=
#
<<adaboostToBoost, child='boosting_methodology.Rnw'>>=
#
<<boostrImplementation, child='boostr_implementation.Rnw'>>=
#
<<discussion, child='summary_and_future_work.Rnw'>>=
#
\end{document}
Related
I am trying to render a .qmd file to HTML specifying an output file. I am compiling it from a Quarto Project from RStudio on MacOS 12.6. I also have a bootstrap theme in the YAML header.
---
title: "quarto_output_experiment"
format:
html:
theme: journal
---
If I render it with Cmd + Shift + K from RStudio, the HTML document appears in the same directory as the .qmd and looks fine. However if I render it from the Quarto CLI specifying the output directory (a folder called output), the HTML can't render plots and lacks any sort of formatting and style. The command is
quarto render quarto_output_experiment.qmd --output output/report.html
Here are the results.
This is when rendered from RStudio
This is when render via Quarto CLI
What am I missing? How can I render the HTML in a different directory and have plots and proper style?
For reference, this is the document itself
---
title: "quarto_output_experiment"
format:
html:
theme: journal
---
## Quarto
Quarto enables you to weave together content and executable code into a finished document.
To learn more about Quarto see <https://quarto.org>.
```{r}
1 + 1
```
# Lorem ipsum
```{r}
library(ggplot2)
ggplot(data.frame(x = rnorm(100)), aes(x)) + geom_density()
```
UPDATE
Specifying --output-dir intead of --output produces a document with plots and style, but the question still remains if there is a way to change the name of the output HTML file? Probably not mission critical, but I'm still curious.
That is a really weird bug and it seems to be related to the path (see here. Usually, you can use quarto::render_quarto() like this,
quarto::quarto_render(input = "quarto_output_experiment.qmd", output_file = "report.html")
This works, but when you add the folder name
quarto::quarto_render(input = "quarto_output_experiment.qmd", output_file = "results/report.html")
it does not work anymore. The problem is that the folder (quarto_output_experiment_files) where the libs and images are stored, is not automatically also placed where the output-file is located.
I would file an issue here.
A possible solution for the time being would be the self-contained param that produces self-contained html files, e.g.
---
title: "quarto_output_experiment"
format:
html:
theme: journal
self-contained: true
---
then you could use file.copy to move that file in the correct folder.
Edit: The option self-contained is depreciated, use embed-resources instead! Link
I would like to produce a custom title page when I knit my R Markdown document to pdf.
Here are the contents of my R Markdown document:
---
output:
pdf_document:
template: template.tex
---
# abstract
this is just some text
And here are the contents of template.tex:
\begin{document}
\maketitle
\end{document}
When I knit to pdf none of the R Markdown text appears. Only the template does.
Could anyone explain how I could type in R Markdown after using a latex template?
Your R Markdown document seems correct. The issue lies in your template.tex document.
This page shows a simple template.tex example:
\documentclass{article}
\begin{document}
$body$
\end{document}
The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.
However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.
I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.
\documentclass[]{report} % use report class because it contains a title page
\usepackage{hyperref} % load the hyperref package to avoid an undefined
% control sequence error
\title{$title$} % set title to what you passed from the yaml in the R
% Markdown document
\author{} % set author to empty to avoid warning message about
% no author set; use \author{$author$} if you want to
% set author from the yaml like `author: "My Name"`
\date{} % set date to empty to avoid a date on the title page;
% use \date{$date$} to set from yaml `date: 2021-01-08`
\begin{document}
\maketitle
$body$
\end{document}
But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.
I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).
In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:
---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text
I would like to produce a custom title page when I knit my R Markdown document to pdf.
Here are the contents of my R Markdown document:
---
output:
pdf_document:
template: template.tex
---
# abstract
this is just some text
And here are the contents of template.tex:
\begin{document}
\maketitle
\end{document}
When I knit to pdf none of the R Markdown text appears. Only the template does.
Could anyone explain how I could type in R Markdown after using a latex template?
Your R Markdown document seems correct. The issue lies in your template.tex document.
This page shows a simple template.tex example:
\documentclass{article}
\begin{document}
$body$
\end{document}
The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.
However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.
I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.
\documentclass[]{report} % use report class because it contains a title page
\usepackage{hyperref} % load the hyperref package to avoid an undefined
% control sequence error
\title{$title$} % set title to what you passed from the yaml in the R
% Markdown document
\author{} % set author to empty to avoid warning message about
% no author set; use \author{$author$} if you want to
% set author from the yaml like `author: "My Name"`
\date{} % set date to empty to avoid a date on the title page;
% use \date{$date$} to set from yaml `date: 2021-01-08`
\begin{document}
\maketitle
$body$
\end{document}
But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.
I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).
In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:
---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text
I am working within RStudio 1.1.383 using rmarkdown. I am trying to render to Latex a string that is created within a knitr chunk so that it appears as latex code within the document.
So far I have experimented with the 'results=' options in the chunk header and find that results='asis' enables it to be rendered once the document is knitted, but I have not been able to find a way to enable the result to be rendered using the preview feature that allows you to run a single chunk and see the results within the .Rmd editor.
Any help on this matter would be appreciated.
A minimal example is included below that should be copied into a .Rmd file before rendering.
Thanks in advance,
Michael
---
title: "Minimal Example"
output: html_document
---
```{r}
str <- "$$\\alpha \\cdot \\beta = \\delta$$"
# this doesn't show in the preview 'run chunk feature'
cat(str, "\n")
# neither does this
writeLines(str)
```
I am practicing data analysis using R programming. I want my files to be on github. I am unable to figure out why github is not showing the output of .rmd files.
Here is the link to the file in my github repository Data Analysis Practice
I want the output including plots to be shown on github.
Instead of:
output: html_document
make it:
output: rmarkdown::github_document
(assuming you have the latest rmarkdown installed which you should if you're using RStudio — which I suspect you are — and keep it updated or update packages regularly).
It will create Exploring_one_variable.md and render the images as files.
When you browse to that markdown file, the images will render.
An alternative is to use:
output:
html_document:
keep_md: true
and it will render to both Exploring_one_variable.md and Exploring_one_variable.html so you'll have the best of both worlds without the local github-esque preview that the former provides.
You can get fancier and put something like the following as the first code section in the Rmd:
```{r, echo = FALSE}
knitr::opts_chunk$set(
fig.path = "README_figs/README-"
)
```
which will put the figures in a directory of your choosing.
You can see this in action here as the README.md was generated with knitr from the README.Rmd in the same directory.
There is now output: github_document