I got a problem using R-markdown to write my master's work. I am using the papaja package. Everything's fine except that when I knit my document to produce a pdf the first name of the author in my bibliography are BEFORE the last name. Here is a little reproducible example of my problem. Here is the r-makrdown content:
---
title: "The title"
shorttitle: "Title"
author:
- name: First Author
affiliation: 1
corresponding: yes # Define only one corresponding author
address: Postal address
email: my#email.com
- name: Ernst-August Doelle
affiliation: "1,2"
affiliation:
- id: 1
institution: Wilhelm-Wundt-University
- id: 2
institution: Konstanz Business School
abstract: |
Enter abstract here (note the indentation, if you start a new paragraph).
note: |
Complete departmental affiliations for each author (note the indentation, if you start a new paragraph).
Enter author note here.
keywords: "keywords"
wordcount: X
class: man
lang: american
figsintext: yes
lineno: yes
bibliography:
- r-references.bib
output: papaja::apa6_pdf
---
```{r message = FALSE, warning = FALSE}
library("papaja")
apa_prepare_doc() # Prepare document for rendering
```
[#R-papaja]
# References
```{r create_r-references}
r_refs(file = "test-references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
\setlength{\parskip}{8pt}
And here are my references which are in the "test-references.bib" file:
%% This BibTeX bibliography file was created using BibDesk.
%% http://bibdesk.sourceforge.net/
%% Created for Lucien at 2015-12-09 13:45:03 +0100
%% Saved with string encoding Unicode (UTF-8)
#Manual{R-base,
title = {R: A Language and Environment for Statistical Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2015},
url = {https://www.R-project.org/},
}
#Manual{R-papaja,
author = {Frederik Aust and Marius Barth},
title = {papaja: Create APA manuscripts with RMarkdown},
year = {2015},
note = {R package version 0.1.0.9054},
url = {https://github.com/crsh/papaja},
}
The output in the pdf for the reference is:
Frederik Aust and Marius Barth.papaja: Create APA manuscripts with RMarkdown, 2015.20URL https://github.com/crsh/papaja. R package version 0.1.0.9054.
And I expect:
Aust Frederik and Barth Marius .papaja: Create APA manuscripts with RMarkdown, 2015.20URL https://github.com/crsh/papaja. R package version 0.1.0.9054.
Your paper was probably already due... Anyway. You should have the authors in the format Lastname, Firstname [and Lastname, Firstname]*.
In your case:
#Manual{R-papaja,
author = {Aust, Frederik and Barth, Marius},
title = {papaja: Create APA manuscripts with RMarkdown},
year = {2015},
note = {R package version 0.1.0.9054},
url = {https://github.com/crsh/papaja},
}
Related
My testing files with related directory paths (in windows operating system) are listed as followed
E:\mywork\test.rmd
E:\myref\apa.csl
E:\myref\ref.bib
E:\mydata\xx.csv
In test.rmd, it shows
---
title: "Untitled"
author: "xxx"
date: "xxx"
output: pdf_document
bibliography: "E:/myref/ref.bib" #ok!
# bibliography: "../myref/ref.bib" #ok!
# bibliography: r"(E:\myref\ref.bib)" #no!
# bibliography: "E:\\myref\\ref.bib" #no!
csl: "E:/myref/apa.csl" #https://www.zotero.org/styles
---
# R Markdown
#efron2004least
```{r}
# d0 = write.csv(iris[1:10,],file=r"(E:\mydata\xx.csv)")
d1 = read.csv(r"(E:\mydata\xx.csv)"); head(d1)
d2 = read.csv("E:\\mydata\\xx.csv"); head(d2)
```
# References
and works well, where ref.bib reads
#Article{efron2004least,
author = {Efron, Bradley and Hastie, Trevor and Johnstone, Iain and Tibshirani, Robert},
title = {Least angle regression},
journal = {The Annals of Statistics},
year = {2004},
volume = {32},
number = {2},
pages = {407--499},
}
and apa.csl is downloaded from https://www.zotero.org/styles. In particular, bibliography: "../myref/ref.bib" is also working!
After running ?Quotes in the Console I note that
## A Windows path written as a raw string constant:
r"(c:\Program files\R)"
Thus, I try bibliography: r"(E:\myref\ref.bib)" and bibliography: "E:\\myref\\ref.bib", but these two choices failed. I don't know how to fix them. Or the latter two choices are not permitted in the yaml? (PS: The latter two work for the xx.csv data file case!)
I am writing a document in RStudio using RMarkdown, knitr and R scripts. The output should be in MSWord. My document contains a lot of tables, so I need to have neatly formatted talble titles and cross reffernces to them. The only way to have cross references I found was bookdown package, so I use it to knit Word document from RMarkdown.
My problem is I cannot change prefix word and separator in the table caption. For example, I need the prefix word to be "MyTable" and separator " -- ". So the table caption in MSWord should look something like:
MyTable 1.1 -- Table caption
| Col1 | Col2 | Col3 |
----------------------
| Text | Text | Text |
But what I finally managed to get is only:
MyTable 1.1: Table caption
| Col1 | Col2 | Col3 |
----------------------
| Text | Text | Text |
The main problem is to replace ":" witn "--".
My code in RMarkdown is as follows:
---
title: "Test"
output:
bookdown::word_document2:
reference_docx: template.docx
html_document: default
---
.```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ")
library(bookdown)
library(flextable)
set_flextable_defaults(font.size = 11, font.family = "Times New Roman", decimal.mark = ",", big.mark = " ", theme_fun = "theme_box")
.```
# Chapter Header
Some paragraphs of text. Then goes comes the place for my table.
.```{r}
ft <- flextable(head(mtcars))
ft <- set_caption(ft, "My table caption", autonum = run_autonum(seq_id = "tab", post_label = " -- ", bkm = "tab1"))
ft
.```
Very important information is in MyTable \#ref(tab:tab1).
Besides I have _bookdown.yml file with the following language settings:
language:
label:
fig: 'MyPicture '
tab: 'MyTable '
The latter actually is the way I found to change prefix word "Table" to "MyTable".
I also have the MSWord file with styles, made according to Pandoc user manuals. In the output I get my caption formatted according to the style it should be (but the word "Table" and number is not formatted as it would be if the table caption were made in MSWord).
I have also tried to knit the output file without using bookdown. In this case the line: knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ") seems to work,
I get the right prefix word and the right separator, but simple table number (the result is "MyTable 1" instead of "MyTable 1.1") and sure no cross references.
My package versions are:
bookdown - 0.27
flextable - 0.7.3
knitr - 1.3.8
officer - 0.4.3
Unfortunately, I did not have any success with lua filters. I ended using officedown package. For it the code of YAML will be:
---
title: "Test"
output:
officedown::rdocx_document:
reference_docx: template.docx
tables:
style: Table
caption:
pre: "Table "
sep: " -- "
# To prevent word "Table" and crossref numbers from being bold
fp_text: !expr officer::fp_text_lite(bold = FALSE)
---
My little example (test.rmd) is as follows:
---
output:
beamer_presentation:
latex_engine: xelatex
citation_package: natbib
bibliography: ref.bib
biblio-style: "unsrtnat"
colorlinks: yes
---
## Papers
- Cp #mallows1973some
- AIC #akaike1974new
- BIC #schwarz1978estimating
- GCV #craven1978smoothing
- bridge #frank1993statistical
- LASSO #tibshirani1996regression
- SCAD #fan2001variable
- MCP #zhang2010nearly
## References {.allowframebreaks}
where ref.bib is
#Article{mallows1973some,
author = {Mallows, Colin L.},
journal = {Technometrics},
title = {{Some comments on $C_p$}},
year = {1973},
number = {1},
pages = {87--94},
volume = {42},
}
#Article{akaike1974new,
author = {Akaike, Hirotugu},
journal = {IEEE Transactions on Automatic Control},
title = {A new look at the statistical model identification},
year = {1974},
number = {6},
pages = {716--723},
volume = {19},
}
#Article{schwarz1978estimating,
author = {Schwarz, Gideon},
journal = {The Annals of Statistics},
title = {Estimating the dimension of a model},
year = {1978},
number = {2},
pages = {461--464},
volume = {6},
publisher = {Institute of Mathematical Statistics},
}
#Article{craven1978smoothing,
author = {Craven, Peter and Wahba, Grace},
journal = {Numerische Mathematik},
title = {Smoothing noisy data with spline functions},
year = {1978},
number = {4},
pages = {377--403},
volume = {31},
}
#Article{frank1993statistical,
author = {Frank, Ildiko E. and Friedman, Jerome H.},
journal = {Technometrics},
title = {A statistical view of some chemometrics regression tools},
year = {1993},
number = {2},
pages = {109--135},
volume = {35},
}
#Article{tibshirani1996regression,
author = {Tibshirani, Robert},
title = {Regression shrinkage and selection via the lasso},
journal = {Journal of the Royal Statistical Society. Series B (Methodological)},
year = {1996},
volume = {58},
number = {1},
pages = {267--288},
publisher = {JSTOR},
}
#Article{fan2001variable,
author = {Fan, Jianqing and Li, Runze},
journal = {Journal of the American Statistical Association},
title = {Variable selection via nonconcave penalized likelihood and its oracle properties},
year = {2001},
number = {456},
pages = {1348--1360},
volume = {96},
}
#Article{zhang2010nearly,
author = {Zhang, Cun-Hui},
journal = {The Annals of Statistics},
title = {Nearly unbiased variable selection under minimax concave penalty},
year = {2010},
number = {2},
pages = {894--942},
volume = {38},
}
My problem is that test.rmd can run successfully while there is a blank page in the second page.
One may suggest that I can use the csl: xxx.csl instead of citation_package: natbib in Rmarkdown for beamer. Surely, csl: xxx.csl is fine and can run without accidents, but I still prefer to using citation_package: natbib.
Curiously, if I change ## References {.allowframebreaks} in test.rmd with
`r if (knitr::is_html_output()) '
## References {.allowframebreaks}
'`
then the blank page disappears. However, a new problem has emerged; the frame title and the bookmark of References both disappear. How can I fix the problems?
Quick hack:
---
output:
beamer_presentation:
latex_engine: xelatex
keep_tex: true
citation_package: natbib
bibliography: ref.bib
biblio-style: "unsrtnat"
colorlinks: yes
header-includes:
- \pretocmd{\bibliographytrue}{\frametitle{References}}{}{}
---
## Papers
- Cp #mallows1973some
- AIC #akaike1974new
- BIC #schwarz1978estimating
- GCV #craven1978smoothing
- bridge #frank1993statistical
- LASSO #tibshirani1996regression
- SCAD #fan2001variable
- MCP #zhang2010nearly
Using # References instead of ## References solves this problem.
---
output:
beamer_presentation:
latex_engine: xelatex
citation_package: natbib
bibliography: ref.bib
biblio-style: "unsrtnat"
colorlinks: yes
---
## Papers
- Cp #mallows1973some
- AIC #akaike1974new
- BIC #schwarz1978estimating
- GCV #craven1978smoothing
- bridge #frank1993statistical
- LASSO #tibshirani1996regression
- SCAD #fan2001variable
- MCP #zhang2010nearly
# References
Consider the following R Markdown code:
---
title: 'John Doe'
output: pdf_document
bibliography: [bib1.bib, bib2.bib]
nocite: '#*'
csl: vancouver.csl
---
# Peer-reviewed publications
# Other publications
bib1.bib code:
#article{article1,
Author = {Doe, John},
Title = {{Article 1 name}},
Journal = {{Journal 1 name}},
Year = {{2021}},
Volume = {{1}},
Pages = {{1-2}},
}
#article{article2,
Author = {Doe, John},
Title = {{Article 2 name}},
Journal = {{Journal 2 name}},
Year = {{2020}},
Volume = {{1}},
Pages = {{1-2}},
}
bib2.bib code:
#article{article3,
Author = {Doe, John},
Title = {{Article 3 name}},
Journal = {{Journal 3 name}},
Year = {{2021}},
Volume = {{1}},
Pages = {{1-2}},
}
The knit output:
However, I'm rather looking after the following output:
Please note that I don't want in-text citations but rather the whole bibliographies listed, which is why I approached the problem by adding the "nocite: '#*'" to the code, but I couldn't get the result I'm after.
By the way, I used the csl style vancouver.csl (please see: https://www.zotero.org/styles?q=vancouver) here.
Any help is greatly appreciated.
You could use biblatex to split the bibliographies:
---
title: 'John Doe'
output:
pdf_document:
keep_tex: true
csl: vancouver.csl
header-includes:
- \include{preamble.tex}
---
with preamble.tex:
\usepackage[style=vancouver]{biblatex}
\addbibresource{bib1.bib}
\addbibresource{bib2.bib}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\perdatasource{bib1.bib}
\step[fieldset=keywords, fieldvalue={,peer}, append]
}
}
}
\AtEndDocument{\nocite{*}
\printbibliography[keyword={peer},title={Peer-reviewed publications}]
\DeclareFieldFormat{labelnumber}{%
\ifinteger{#1}
{\number\numexpr#1-2\relax}
{#1}}
\printbibliography[notkeyword={peer},title={Other publications}]}
And then compile the resulting filename.tex file with
pdflatex filename
biber filename
pdflatex filename
I'm trying to loop through a list of employee IDs to create a report for each id.
I know I have to declare the parameter in the YAML but I'm getting a scanner error.
---
title: "Employee Record"
params:
MASTER_ID: !r uniqueID
output: pdf_document
---
**Employee ID:** `r params$MASTER_ID`
The field of employee ids in the dataset is called MASTER_ID and uniqueID is just a list of each unique employee id (length = 880)
The error I'm getting is:
Error in yaml::yaml.load(string, ...) :
Scanner error: while scanning a simple key at line 3, column 1 could not find expected ':' at line 4, column 1
I don't have any extra white spaces or anything so i'm not sure what I'm missing
To follow up on what others have already said, it is best to call your Rmd from a separate R file using the rmarkdown::render function. This also allows you to easily control the file naming and output location.
employees <- 1:10
for (i in employees) {
rmarkdown::render("test_pdf.Rmd",
params = list(MASTER_ID = i),
output_file = paste0('employee-', i, ".pdf"),
output_dir = '/reports')
}
With test_pdf.Rmd containing:
title: "Employee Record"
params:
MASTER_ID:
value: 1
output: pdf_document
---
**Employee ID:** `r params$MASTER_ID`