umlauts in comments in preambel with knitr cause problems with texi2dvi - r

I found some errors with umlauts in comments with knitr in latex. First my mini-example:
\documentclass[12pt, % ß
a4paper % page format
]{scrartcl}
\begin{document}
<<code>>=
rnorm(30)
#
\end{document}
And this errors a shown:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,: Running 'texi2dvi' on 'minimalexample.tex' failed. LaTeX errors: ! LaTeX Error: Environment knitrout undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... ! Undefined control sequence. l.6 \definecolor {shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{... The control sequence at the end of the top line of your error message was never \def'ed. If you have ! Undefined control sequence. l.6 ...shadecolor}{rgb}{0.969, 0.969, 0.969}\color {fgcolor}\begin{kframe} The control sequence at the end of the top line of your error message was never \def'ed. If you have ! LaTeX Error: Environment kframe undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... ! LaTeX Error: Environment alltt undefined. See the La Calls: knit2pdf -> <Anonymous> -> texi2dvi In addition: There were 13 warnings (use warnings() to see them)
Execution halted
Process exited with error(s)
I use Texstudio (V 2.9.4) as my editor and my knitr version is 1.12.3.
Is there a chance to use umlauts in comments and avoid these errors?

Ok, finally I got the solution.
It didn't affected only the comments. Every umlaut caused that Error. The file was encoded in iso-8859-1 and R was running with utf-8. I found it out in R with:
Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C"

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.

Knitr cannot find img files in /static/ folder

I have a hugo-academic website (methods101.com) that has been working well for last year.
I just went to edit some pages and I've started getting a new error.
The code that seems to be creating the problems is the knitr::include_graphics() function.
This is an example of the text that produces an error:
{r, echo=FALSE, out.width=600,
fig.cap="Newspaper article in Word document, next to same article on internet.",
fig.align='center'}
knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png")
This is the error message:
Rendering content/docs/SOC224_qual_analysis_eg.Rmd
Quitting from lines 80-81 (SOC224_qual_analysis_eg.Rmd)
Error in knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png") :
Cannot find the file(s): "/img/soc224_qual_analysis_eg_figure_1.png"
Calls: local ... withCallingHandlers -> withVisible -> eval -> eval -> <Anonymous>
Execution halted
<simpleError in render_page(f): Failed to render 'content/docs/SOC224_qual_analysis_eg.Rmd'>
I get the same problem on different computers, and after fresh re-installing/downloading the website contents.
The image file is definitely inside the folder:
/static/img/
You may see the help page ?knitr::include_graphics. In your case, you need
knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png", error = FALSE)
We managed to solve this problem by rolling back to the earlier version of Knitr.
This was the code we used:
remove.packages("knitr")
packageurl <- "https://cran.r-project.org/src/contrib/Archive/knitr/knitr_1.27.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
We had no problems on 5th Feb, but noticed the new version of knitr came out on 6th Feb, and so thought this could be causing the problems.
We don't get the problems with the old version of knitr.
Not sure what the underlying cause is.

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.

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.

Why does Sweave throw an error on LaTeX code that has been commented out?

Sweaving the following:
\documentclass{article}
\begin{document}
<<>>=
x <- 5
y <- 10
#
The value of $z$ is \\
%\Sexpr{z}
\end{document}
generates the following error:
Error in eval(expr, envir, enclos) : object 'z' not found
Calls: <Anonymous> -> Sweave -> <Anonymous> -> eval -> eval
Execution halted
If \Sexpr{z} is commented out, why is it causing an error?
This answer is a placeholder to record what I learned on my own. I will gladly accept a better one...
Sweave doesn't parse the LaTeX, so it will evaluate any \Sexpr, even if it is commented out. So if you assigned a value to z in the code chunk, %\Sexpr{z} would print the value of z. Amusingly, \Sexpr{#z} will suppress the printing of the value of z.
I also tracked down this (rather old) discussion of this issue in a mailing list.

Resources