I'm trying to get knitr to create a bibtex file that includes all the packages that I am using as shown (and answered) in this question. That part is working fine but a single R package, foreach, results in non-valid bibtex code.
Here is a minimal .Rnw example that produces the error
\documentclass[11pt]{article}
\usepackage{natbib}
\begin{document}
This is a test
<<>>=
library(foreach)
# Write the bibtex file:
write_bib("foreach", file="test.bib")
#
\nocite{*}
\bibliographystyle{apalike}
\bibliography{test}
\end{document}
kniting this file produces this test.bib file
#Manual{R-foreach,
title = {foreach: Provides Foreach Looping Construct for R},
author = {{Revolution Analytics} and Steve Weston}},
year = {2015},
note = {R package version 1.4.3},
url = {https://CRAN.R-project.org/package=foreach},
}
Note that the author line has two curly braces towards the end of the author line so using this bibtex file without removing the curly brace results in a bunch of problems. I've only encountered this problem with the foreach package.
Can anyone tell me what I can do to prevent the error (currently I have a one-line sed code to remove the extra brace)?
The citation("foreach") output looks fine and dandy, so I'm guessing the error may be deep down in utils:::toBibtex.bibentry but I haven't been able to figure it out.
Related
I am typing up some results comparing various methods for importing unicode files in R. I read in a CSV file using dplyr::read_csv() and utils::read.csv(), then compare them using dplyr::setdiff(). In fact, the two data files are different because the column names are read differently by the default options of the two methods. This causes an error to be written to the console. I know how to fix the error itself; that is not the issue. The three commands in the markdown file are
```{r read.csv message=FALSE, warning=FALSE, error=FALSE}
dplyr_read_csv <- read_csv("World Class.csv")
base_read_csv_T <- read.csv("World Class.csv")
setdiff(dplyr_read_csv, base_read_csv_T)
```
..which, when run as an R script result in:
> setdiff(dplyr_read_csv, base_read_csv_T)
Error in `setdiff()`:
! `x` and `y` are not compatible.
✖ Cols in `y` but not `x`: `height..in..`, `weight..lb..`, `height..cm..`,
`weight..kg..`, `中文..Simplified.`, `中文..Traditional.`, `English..GB.`,
`English..US.`, `Русский.язык`, `Tiê.ng.Viê.t`.
✖ Cols in `x` but not `y`: `height (in.)`, `weight (lb.)`, `height (cm.)`, `weight
(kg.)`, `中文 (Simplified)`, `中文 (Traditional)`, `English (GB)`, `English
(US)`, `Русский язык`, `Tiếng Việt`.
I would like for this error to show in the generated output.
When I put the code in the R markdown file, the file will not knit because of this thrown error. I have done the obvious things like putting error = FALSE in the R chunk, but none of my efforts have worked.
You can find the data file (named World Class.csv) and the R markdown file (read_world_classes.Rmd) in my github repository. What should I do to include this chunk and its error in the markdown file?
(There is a regular R script read_world_classes.R that I initially made to compare a bunch of other file types aside from CSV. That's what all the other files in the repository are).
As #bs93 said above, the correct output is obtained by adding error=TRUE to the r chunk:
```{r error=true}
<code goes here>
```
I have tried to create an exercise that includes formatted r-code.
Question
= = = = = = = =
You want to load the tidyverse package. Please complete the following r code.
```r
_______(tidyverse)
```
Now the respective environments shaded, etc. are missing from rmarkdown.
I have tried using the header argument in the exams2nops function. But this will only include strings in the document body. How can I edit the preamble of the the output pdf?
Thanks :)
I found a solution. I have created a inject.tex file that I pass to the header argument after scanning it. The file contains the following
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
I am using the LaTeX package problems to create solution sets from a database of problems. The database is structured like a bibliography database, in a .bib file. The whole system works beautifully for regular LaTeX, but now some of my solutions have R code (in knitr chunks) in them.
The default sequence of knitting/TeXing/BibTeXing in RStudio is not working-- the code ends up in the document verbatim, along with mangled versions of the chunk delimiters. So, I need to find the right workflow of steps to ensure that the code makes it through.
This package seems to be very set on having two files, one for the database and one for the .tex/.rnw, so I can't make a working example, but something like this:
\documentclass{article}
\usepackage[solution]{problems}
\Database{
#QUESTION{1.1,
problem = {1.1},
solution = {This solution only uses TeX, like $\bar{x}$. It works fine.}}
#QUESTION{1.2,
problem = {1.2},
solution = {This solution includes code
<<>>=
head(iris)
#
It does not work.
}}}
\begin{document}
Problems for this week were 1.1 and 1.2
\problems{1.1}
\problem{1.2}
\end{document}
You will have to knit the .bib file first and then run LaTeX and BibTeX.
While you usually have a .Rnw file that is knitted to .tex and then let LaTeX tools process the .tex and the .bib file you will have to start with a (let's call it) .Rbib file that is knitted to .bib and then processed by LaTeX.
For simplicity, I give the file I called .Rbib above the name bibliography.Rnw but you can choose any extension you like. I chose .Rnw because the syntax used inside is the same as in .Rnw files.
As dummy entries for the bib-file I use data from verbosus.com and added some knitr code.
The first chunk sets global chunk options to prevent knitr from adding the code of the chunks or any markup to the output file. The next chunk shows how for example the title field could be filled with generated content and the \Sexpr{} part is an example how this could be used to add some dynamic text.
<<setup>>=
library(knitr)
opts_knit$set(
echo = FALSE,
results = "asis"
)
#
article{article,
author = {Peter Adams},
title = {
<<echo=FALSE, results = "asis">>=
cat("The title of the work")
#
},
journal = {"The title of the journal"},
year = 1993,
number = 2,
pages = {201-213},
month = 7,
note = {An optional note},
volume = 4
}
#book{book,
author = {Peter Babington},
title = {\Sexpr{"The title of the work"},
publisher = {The name of the publisher},
year = 1993,
volume = 4,
series = 10,
address = {The address},
edition = 3,
month = 7,
note = {An optional note},
isbn = {3257227892}
}
It is important to have the chunk option results = "asis" and to use cat() instead of print(). Otherwise, there would be unwanted characters in the output.
If this is saved as bibliography.Rnw the following is enough to get a .bib file with the chunks evaluated:
knit(input = "bibliography.Rnw", output = "bibliography.bib")
After that only standard LaTeX compilation remains.
I'm new to using Markdown, and have looked for a similar problem to this on SO without success. I'm using Rmarkdown (with Rstudio and knitr) to write a vignette that describes reading in a datafile which is imported as part of the package. I can correctly access the datafile using
> system.file("extdata", "Marlin-tag38606.txt", package = "xtractomatic")
I want to show the first few lines of this file in the vignette, so my code reads
```{r, results=as.is}
datafile <- system.file("extdata", "Marlin-tag38606.txt", package = "xtractomatic")
system(paste("head -n5 ",datafile))
```
The problem is that the results of this call are output to the Rmarkdown console and NOT to the vignette html file.
The output in the Rmarkdown window of RStudio is (but formatted nicer):
|................... | 29%
label: unnamed-chunk-8
date lon lat lowLon higLon lowLat higLat
4/23/2003 203.899 19.664 203.899 203.899 19.664 19.664
4/24/2003 204.151 19.821 203.912597 204.389403 18.78051934 20.86148066
4/30/2003 203.919 20.351 203.6793669 204.1586331 18.79728188 21.90471812
5/1/2003 204.229 20.305 203.9943343 204.4636657 18.90440013 21.70559987
|.................... | 31%
Which is what I wanted outputted to the vignette text, but it is not there. Within the resulting vignette all I have is the two lines of R code, but not the output from the system call.
Any advice would be appreciated. Thanks.
Cara Wilson
Use intern = TRUE for system(), then cat() the output:
cat(system(paste("head -n5", datafile), intern = TRUE), sep = '\n')
Using a bash chunk in the Rmarkdown document does the job for me.
For example with the package testdat which has .csv file in its extdata directory:
```{bash}
head -n5 ~/R/x86_64-pc-linux-gnu-library/3.3/testdat/extdata/2012.csv
```
will give in your html file:
## 14,,2012,Censo,1775351,,
## 14,,2012,Votantes,1135568,64.0,
## 14,,2012,Nulos,9168,0.8,
## 14,,2012,Válidos,1126400,99.2,
## 14,,2012,Blancos,14640,1.3,
I am not sure though this option existed in 2014 when you've asked the question.
I'm very new to sweave and I feel this will be an easy question, but I'm struggling to solve it myself (googling omit ">" Sweave doesn't really work because I can't search for ">"!).
I'm including a short script into my document:
<<echo=true, results=hide, eval=false>>=
# This is a simple script.
print('Hello World!\n')
#
I basically want the script to appear verbatim in the document, using whatever Sweave's "code" environment is. I don't want to evaluate it - it's just there to demonstrate how one might write a script utilising the functions in the package this document is for.
However, this produces the following in the output document:
> # This is a simple script
> print('Hello World!\n')
Is there some way to omit the > ? Is this the right way to put a script into a Sweave document, or is there some other environment that is meant to be used? (I can use a \begin{verbatim}, but it seemed to me that I should use Sweave commands if possible for code chunks).
I just want the output to be
# This is a simple script.
print('Hello World!\n')
You can try changing the prompt and continue options,
which define the > and + characters.
options(continue=" ", prompt=" ")
I like Vincent's first idea using knitr.
install.packages("knitr")
library(knitr)
Here's a sample Rnw.
\documentclass[a4paper]{article}
%\usepackage{Sweave}
\title{Test case}
\author{romunov}
\begin{document}
\maketitle
<<eval = FALSE, echo = TRUE>>=
# This is a simple script.
print('Hello World!\n')
#
\end{document}
And then
knit("coffee.Rnw")
Convert your .tex file into something pretty, and you get