How to add bibliography/references to TOC in bookdown - r

I have been trying to render my thesis with the bibliography added to the TOC.
After reading bookdown documentation carefully I have come to the conclusion that I must do the following:
bookdown::render_book(input = "index.Rmd",output_format = "bookdown::pdf_book", toc_bib = TRUE)
However, "unused argument" error is thrown in the console when I try to use it.
Now, the documentation says that the third argument of render_book is the dots (i.e. ...) argument and that it gets passed to output_format, so it should work.
I'm confused here, why doesn't it work?

The following works for me:
bookdown::render_book("index.Rmd", "bookdown::pdf_book", output_options=list(toc_bib = TRUE))

You can also add to the YAML in your thesis.Rmd file
header_includes:
- \usepackage[nottoc,numbib]{tocbibind}

Related

How to pass args to makeindex in tinytex?

In this previous question: how to use zhmakeindex instead of makeindex in bookdown, I see there is a way to pass arguments to makeindex in tinytex, for example setting
options(tinytex.makeindex.args = c('-z', 'pinyin')).
I'm currently trying to use the nomencl package, which requires passing arguments to makeindex, as described here: https://abhigupta.io/2021/11/08/nomenclature-in-latex.html, but can't get them to work in tinytex.
MWE (this is test.tex:
\documentclass{article}
\usepackage{nomencl}
\makenomenclature
\begin{document}
Here is an example:
\nomenclature{\(c\)}{Speed of light in a vacuum}
\nomenclature{\(h\)}{Planck constant}
\printnomenclature
\end{document}
Then, in my R terminal I would set
options(tinytex.makeindex.args = c('test.nlo', '-s', 'nomencl.ist', '-o', 'test.nls'))
When I run tinytex::pdflatex('test.tex'), I get a PDF that says "Here is an example:", but no nomenclature.
I'm not sure if I'm passing the arguments correctly or if there's something else I'm missing, but any help is appreciated.

#Error in table(StudentSurvey$Gender, StudentSurvey$Smoke): object 'StudentSurvey' not found

So, I'm attempting to knit a R document to a HTML file however, when it knits it produces this error "#Error in table(StudentSurvey$Gender, StudentSurvey$Smoke): object 'StudentSurvey' not found"
For reference this is my R code.
read.csv("StudentSurvey.csv")
barplot(table(StudentSurvey$Gender, StudentSurvey$Smoke), legend=TRUE)
boxplot(StudentSurvey$Pulse ~ StudentSurvey$Smoke , xlab = "Smoke" , ylab = "Pulse")
I'm unsure whats wrong, I've tried to troubleshoot by changing my Rworking directory and ensure that "StudentSurvey.csv" is in there, but alas this issue appears to be happening. If it isn't immediately apparent I'm extremely new to R as I'm using it for the first time in UNI so apologies if this is a simple fix.
you just need to assign the output of read.csv() to an object.
StudentSurvey <- read.csv("StudentSurvey.csv")

R Markdown error in code when knit to HTML

I am trying to run code chunks in my markdown document. I have an R script that runs all the code that I need without any issues. Then, when I copy and paste the code into the markdown document, the code will run within the chunk, but will fail when trying to knit into an output document (html/pdf).
I had to create a safe.ifelse function to prevent r from converting my dates to a numeric format as discussed here.
The error appears to be with the code:
safe.ifelse = function(cond, yes, no){structure(ifelse(cond, yes, no), class = class(yes))
}
The error message I get is:
Line 121 Error in structure(ifelse(cond,yes,no), class = class(yes)) : could not find function "days" Calls: ... transform.data.frame ->eval->eval-> safe.ifelse-> structure Execution halted
The line of code following my safe.ifelse function is
seminoma1 = transform(seminoma1, recur.date = safe.ifelse(salvage.tx=="Yes",
date.diagnosis + days(pmax(time.rad, time.chemo, na.rm=TRUE)), NA))
Any help would be appreciated. Thanks.
I'm still too new to comment, but the only time I get an error like that is when I forget to define a function/variable or forget to source a package.
Since days() isn't part of R's base package, I think you need to add:
```{r echo = FALSE}
library("lubridate")
```

Knitting returns parse error

In attempting to knit a PDF. I'm calling a script that should return two ggplots by calling the chunk:
```{r, echo=FALSE}
read_chunk('Script.R')
```r
But receive the error
processing file: Preview-24a46368403c.Rmd
Quitting from lines 9-12 (Preview-24a46368403c.Rmd) Error in
parse(text = x, srcfile = src) : attempt to use zero-length
variable name Calls: <Anonymous> ... <Anonymous> -> parse_all ->
parse_all.character -> parse Execution halted
The script on its own runs and returns the two plots, but won't return them when knitted.
Similarly attempted to use source()
But got a similar error
Quitting from lines 7-10 (Preview-24a459ca4c1.Rmd) Error in
file(filename, "r", encoding = encoding) : cannot open the
connection Calls: <Anonymous> ... withCallingHandlers -> withVisible
-> eval -> eval -> source -> file Execution halted
While this does not appear to be a solution for you, this exact same error message appears if the chunk is not ended properly.
I experienced this error and traced it to ending chunk with `` instead of ```. Correcting the syntax of the chunk solved the problem I experienced with the same error message as you.
Are you sure that knitr is running from the directory you think it is? It appears that it is failing to find the file.
use an absolute path, if that fixes it, you've found your problem
once you've done that, you can use opts_knit$set(root.dir = "...") -- don't use setwd(.) if you want it (the cwd) to be maintained.
Knitr's default is the directory of the .Rmd file itself.
It may have to do with the "r" at the end of the triple backquotes demarcating your code chunk. There should be nothing after the triple backquotes, but I think the problem is specifically that the letter is "r".
The issue stems from the fact that R markdown processes backquoted statements starting with r as inline code, meaning it actually runs whatever is between the backquotes.
I had similar issues writing a problem set in an Rmd with this statement, which had backquoted text intended to be monospace but not run as inline code:
Use sapply or map to calculate the probability of a failure rate over r <- seq(.05, .5, .025).
When I knit the document, I got opaque error messages saying I had an improper assignment using <-. It was because instead of just displaying the backquoted statement in monospace, r <- seq(.05, .5, .025) was actually processed as R inline code of <- seq(.05, .5, .025)...thus the improper assignment error. I fixed my error by changing the variable name from r to rate.
The actual text of the error message in your question might refer to whatever follows your code chunk, as the knitting process is probably trying to run that as code. In this case, just removing that stray r at the end of the code chunk should fix the error.
You should use the following similar syntax, I had the same exact issue but got it fixed:
```{r views}
bank.df <- read.csv("C:/Users/User/Desktop/Banks.csv", header = TRUE) #load data
dim(bank.df) # to find dimension of data frame
head(bank.df) # show first six rows
```
the ``` has to be in the end of the line.
In my case was that I finished the code with four comas, not three . Check this and If you finished with four comas too, try to delete one of them.

Correctly set R default graphic device to quartz?

I tried to add the following line in my .Rprofile file:
options(device = quartz)
It produced an error:
Error in options(device = quartz) : object 'quartz' not found
Then I tried:
options(device = "quartz")
And it works.
However, both work in the regular R session. Can anyone tell me what is the reason for the difference in behavior?
The erro message says it all. There is no data-object named 'quartz' and the options function is not expecting (nor can it find) a function name as an argument value for the 'device'-node.
You are seeing the effect of the environment where .Rprofile is being evaluated because some of the usual packages (such as stats or graphics) are not yet loaded. Read more about this at ?Startup. You could avoid this by starting .Rprofile with require(grDevices)

Resources