Parse error in .Rmd - r

I'm trying to knit an .Rmd and I keep getting
Error in parse(text = x, srcfile = src) : <text>:1:1: unexpected '$'
The line that is generating this error message is:
$\frac{51}{60}$
Any idea why I'm getting this message? I've tried all variations of $ and / in the equation, and even copied and pasted a different fraction example I found online and still get that message

You're probably putting that line inside a chunk:
```{r}
# don't put me here!
```
Put it in the main body of the document instead.

Related

Rmakdown serious problem with strange warning and error message in out of bound I never encounterd

Oh my god, the problem nearly drive me crazy.
I even used simplest method to create a markdownfile with default format through clicking Knit button, and before it , I chose output format in knit to PDF. whatever I tried knit to HTML or Knit to HTML, it was always giving warning message and didn't output anything. The warning message is below:
processing file: _main.Rmd
output file: _main.knit.md
Output created: _main.html
Output created: D:/R-project/me/0000-MainCottonAkesu/NewProcedure2021/Rmarkdown/_main.html
Warning message:
In value[[3L]](cond) :
Could not force captions - error occurred: 'Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'xmlParent': subscript out of bounds
I tried creating new projects but didn't work. I nearly can't resolve it. Please help me.
Sincerely thanks to you.

How to add bibliography/references to TOC in bookdown

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}

tikzDevice not working for basic example

Consider the following example:
library(tikzDevice)
tikz("test.tex")
plot(1:10)
dev.off()
An error will be produced once the plot is created, with a lot of code but near the end the error appears:
("C:\ProgramError in getMetricsFromLatex(TeXMetrics, verbose = verbose) :
TeX was unable to calculate metrics for the following string
or character:
m
Common reasons for failure include:
* The string contains a character which is special to LaTeX unless
escaped properly, such as % or $.
* The string makes use of LaTeX commands provided by a package and
the tikzDevice was not told to load the package.
The contents of the LaTeX log of the aborted run have been printed above,
it may contain additional details as to why the metric calculation failed.
In addition: Warning messages:
1: In readLines(texLog) :
incomplete final line found on 'C:\Users\cgmil\AppData\Local\Temp\Rtmp6FouwP/tikzStringWidthCalc.log'
2: In readLines(texLog) :
incomplete final line found on 'C:\Users\cgmil\AppData\Local\Temp\Rtmp6FouwP/tikzStringWidthCalc.log'
What's going on?
A bit late...
You should have a .tex file with the tikz code of your plot in C:\Users\cgmil\AppData\Local\Temp\Rtmp6FouwP/. Just replace the last two lines
\makeatletter
\##end
by
\end{document}
\end{tikzpicture}
Then compile the document. New packages will be installed and it should works.

How to get the error message in try() with rmarkdown?

When I use rmarkdown::render to compile the following code into html,
```{r, error=T}
setClass('A', slots=c())
try(setClass('A', slots=c()))
```
I found that there is no error message shown in the html file for the second command, instead it is printed in the command line. Does anybody know to make the error message printed in the html file as well? Thanks.
here are 2 methods to print the error message
```{r, error=T}
setClass('A', slots=c())
#method 1
temp <- try(setClass('A', slots=c()))
message(temp)
#method 2
try(setClass('A', slots=c()))
message(geterrmessage())
```
found the 2 methods from Details section in ?try
try evaluates an expression and traps any errors that occur during the
evaluation. If an error occurs then the error message is printed to
the stderr connection unless options("show.error.messages") is false
or the call includes silent = TRUE. The error message is also stored
in a buffer where it can be retrieved by geterrmessage. (This should
not be needed as the value returned in case of an error contains the
error message.)

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.

Resources