Rmd: \input LaTeX equivalent - r

LaTeX function
\documentclass{article}
\begin{document}
\input{folder/file.tex}
\end{document}
\input{folder/file.tex} command in LaTeX fetches the text in the file from the folder. When the main file is run, all the text from file.tex is compiled and included.
Rmd - desired output
I am using R Markdown for my project. I desire to develop and document different topics in different files.
I want to have the following directory structure:
main.Rmd
main_directory
code
file1.Rmd
file2.Rmd
figures
data
read_me
Explored pathways
I have explored the following routes to achieve this.
As suggested in Link 1
sys.source(file = file.path(getwd(), "main_directory",
"code",
"file1.r"))
Here, the code text is not included in the output HTML knit from the Rmd.
As suggested in Link 2
```{r, child=file.path(getwd(), "main_directory", "code", "file1.Rmd"}
```
It led to the following error:
Error: attempt to use zero-length variable name
Here, the code text is not included in the output HTML knit from the Rmd.
As suggested in Link 3
```{r}
includeMarkdown(file.path(getwd(), "main_directory", "code", "file1.Rmd"))
```
It also led to the following error:
Error: attempt to use zero-length variable name
Question: How to include the Rmd code into main file similar to LaTeX input

Related

Render latex text from within r function in rmarkdown?

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)
```

With knitr and .Rnw for LaTeX, how do you print the full bibliography in PDF output?

I can't seem to hack my way through all the possibilities and have a full bibliography inserted in the PDF output of RStudio, knitr, an .Rnw script, and the "Compile PDF" button. The desired text in the PDF would be the details of the cited works.
Here is a Lilliputian bibtex file, called jabrefbibtest.bib, saved in the working directory.
#Book{GreentargetEngagement2012,
Title = {"2012 - In - House Counsel New Media Engagement Survey"},
Author = {"Inside Counsel "},
Publisher = {"Greentarget"},
Year = {"2012"},
Pages = {"20"},
Plots = {"9"},
Tables = {"0"},
Url = {"http://www.greentarget.com/wp-content/uploads/2012/01/2012GTZGICSurveyReportFinal-WebsiteVersion.pdf"}
}
#Book{CitiprivateBank,
Title = {"Intellectual Leadership with Law Watch"},
Author = {""},
Publisher = {""},
Year = {"2008"},
Pages = {"2"},
Plots = {"1"},
Tables = {"4"},
Url = {"http://www.citigroup.com/privatebank/lawassociates/pdfs/lawwatch/slipsheet.pdf"}
}
The .Rnw script, stripped down, is
\documentclass[11pt]{article}
\usepackage[backend=bibtex]{biblatex}
% \addbibresource{} # not sure if this is needed
\begin{document}
<<bibbackground, echo=FALSE, include=FALSE>>=
setwd("~/R/knitr docs/")
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
#
\bibliographystyle{plain}
\bibliography{jabrefbibtest}
Here is one citation \cite{GreentargetEngagement2012} and here is a second \cite{CitiprivateBank}.
Now do full References show below?
\printbibliography
\end{document}
The log:
! Package biblatex Error: '\bibliographystyle' invalid.
See the biblatex package documentation for explanation.
Type H <return> for immediate help.
...
l.59 \bibliographystyle{plain}
Use the package option 'style' instead.
I'm ignoring this command.
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.60 \bibliography
{jabrefbibtest}
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
LaTeX Warning: Citation 'GreentargetEngagement2012' on page 1 undefined on inpu
t line 62.
[more omitted]
Along with The Latex Companion, Dynamic Documents with R and knitr, by Yihui Xie, two LaTeX primers and the 262-page manual for biblatex I have struggled through the complicated advice of these sites. Fruitlessly.
https://tex.stackexchange.com/questions/71565/knitr-and-biblatex
https://tex.stackexchange.com/questions/63852/question-mark-instead-of-citation-number
http://texblog.org/2013/08/20/rknitr-automatic-bibliography-generation-with-biblatex-in-rstudio/
http://www.inside-r.org/packages/cran/knitcitations/docs/bibliography
EDIT after COMMENTS
All the PDF file has is this:
References
Here is one citation [?] and here is a second [?].
Now do full References show below?
As the error messages told you:
Don't use \bibliographystyle{plain} (this does not work for biblatex); use the style option in \usepackage[]{biblatex} instead;
\bibliography{jabrefbibtest} must be put in the preamble instead of the body.
After you correct these issues, it should work:
\documentclass[11pt]{article}
\usepackage[backend=bibtex]{biblatex}
\bibliography{jabrefbibtest}
% or use \addbibresource{jabrefbibtest.bib}
\begin{document}
Here is one citation \cite{ABFWomenFirstChairs2015} and
here is a second \cite{ACCGCSkills2013}.
Now do full References show below?
\printbibliography
\end{document}
BTW, RStudio probably does not support the default backend biber of biblatex, so the backend=bibtex option was used.
I use exactly this setup below to get (note I'm not a fan of changing wd in knitr/rmarkdown and removed this; also your keys in the Rnw didn't match the key in the mwe):
\documentclass[11pt]{article}
\usepackage[american]{babel}
\usepackage[style=apa,backend=biber,bibencoding=latin1]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{jabrefbibtest.bib}
\begin{document}
<<bibbackground, echo=FALSE, include=FALSE>>=
#setwd("~/R/knitr docs/")
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
#
%\bibliographystyle{plain}
Here is one citation \cite{GreentargetEngagement2012} and here is a second \cite{CitiprivateBank}.
Now do full References show below?
\printbibliography
\end{document}
Also after the Rnw knits I have to run the tex file though a LaTeX compiler to render the references the first time.
I always just place \bibliography{jabrefbibtest} at the end of the script where I want the references to occur.
To include all references from your .bib file in your bibliography, i.e. even ones that you didn't end up citing, include the line
\nocite{*} right before the line \printbibliography

Cannot include figures via R/Sweave/LaTeX

I am trying to create a pdf vs R/Sweave/LaTeX that will include a plot. Here is the relevant snippet from my .Rnw file:
\begin{figure}
\begin{center}
<<fig=TRUE,echo=FALSE>>=
makeMyPlot()
#
\end{center}
\label{fig:one}
\end{figure}
When I run Sweave it generates a .pdf file named "analysis report-005.pdf" and a .tex file including the following line:
\includegraphics{analysis report-005.pdf}
So far, so good. But when I run
tools::texi2pdf('analysis report.tex')
I get this error
Error: running 'texi2dvi' on 'analysis report.tex' failed
LaTeX errors:
:218: LaTeX Error: File `analysis report-005' not found.
It looks like texi2pdf is failing to find the file because the extension, ".pdf", is not stated in the .tex file output. When I add ".pdf" directly to the relevant line of the .tex file, the error goes away. However, I shouldn't need to do this manually. I'm not sure if this is a problem with Sweave or texi2pdf; I suspect the latter. How can I fix it?

Using knitr to create HTML slide with separate output of just the R code

I am using knitr to create a set of lecture slides for a class using R. I would like to create a separate "companion file" that contains just has the R code (corresponding to the slides), so that students can execute the R code by cutting and pasting from the companion file.
For example, in the .Rmd file:
``` {r ....}
plot(x,y)
```
Then there would be a text file with:
plot(x,y)
But, have such a file be automatically produced from the .Rmd file?
Even better if the .Rmd file has such tags:
``` {r basic.plot ....}
plot(x,y)
```
Then, text file has:
# basic.plot
plot(x,y)
Can this be accomplished using knitr?
Yes, this is possible. What you're trying to do is called tangling, and it comes from the world of literate programming.
The knit function supports a tangle option that should be set to TRUE if you want to extract source code.

Pandoc insert appendix after bibliography

I'm using the knitr package and pandoc in R to convert a .Rmd file to a PDF. Pandoc is linked to a .bib file and automatically inserts the bibliography at the end of the PDF
The entries in my .bib file look like these, taken from http://johnmacfarlane.net/pandoc/demo/biblio.bib:
#Book{item1,
author="John Doe",
title="First Book",
year="2005",
address="Cambridge",
publisher="Cambridge University Press"
}
#Article{item2,
author="John Doe",
title="Article",
year="2006",
journal="Journal of Generic Studies",
volume="6",
pages="33-34"
}
To build my bibliography, I'm using the following function, taken from: http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html
knitsPDF <- function(name) {
library(knitr)
knit(paste0(name, ".Rmd"), encoding = "utf-8")
system(paste0("pandoc -o ", name, ".pdf ", name, ".md --bibliography /Users/.../Desktop/test.bib --csl /Users/.../Desktop/taylor-and-francis-harvard-x.csl"))
}
The contents of my .Rmd file is:
This is some text [#item1]
This is more text [#item2]
# References
And outputted PDF looks like this:
If I try to insert an appendix, the references still print at the end of the document, like this:
How do insert an appendix after the references?
With newer pandoc versions, you can specify the bibliography's position with <div id="refs"></div> source
This is some text [#item1]
This is more text [#item2]
# References
<div id="refs"></div>
# appendix
Eventually reference handling will change to make it possible to put the references wherever you like (https://github.com/jgm/pandoc/issues/771), but right now there's no easy way to do it.
As suggested here, you could put your appendix in a separate file, use pandoc to convert it to a LaTeX fragment, then include that fragment using the --include-after-body flag. It would then come after the bibliography.
When working in an Rmarkdown document, enter the following text where the citations are to be located. It can be placed in any part of the document allowing other materials, like an appendix, to follow as necessary. The method relies on pandoc's fenced divs which will work in Rmarkdown.
::: {#refs}
:::
The aforementioned code should not be in an R code chunk, rather it should be placed on blank lines by themselves. Once processed by pandoc via knitter, this code will produce the same result as <div id="refs"></div> mentioned in the answer by #soca. The two lines of code do consistently allow for exact placement of the references in any section of the document.
In the example below, references are placed first under a heading of the same name while all of the code chunks in the document are placed afterwards in a code appendix. Here is the pandoc fenced div placed in Rmarkdown that can be used to generate the image that follows.
# References
::: {#refs}
:::
# Appendix A: R Code
```{r ref.label=knitr::all_labels(), echo=TRUE, eval=FALSE}
```
Provided there is a .bib file identified in the yaml frontmatter, the preceding Rmarkdown produces output similar to the following:
Helpful links:
Pandoc User’s Guide - Placement of the Bibliography
Pandoc User’s Guide - Divs and Spans
How can the position of the bibliograpy section be set Latex format
9.6 Custom blocks (*) | R Markdown Cookbook

Resources