How to render LaTeX correctly within function help pane in Rstudio? - r

Mathematical equations written in LaTeX are often presented within the function documentation accessed in R via help() or ? and presented in the help pane in Rstudio. They do not render correctly within the help pane of Rstudio on my machine. I am however able to use LaTeX successfully in all other application and with knitR outputs.
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.3
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.3.3
Output from Terminal showing my LaTeX version
latex -v
pdfTeX 3.14159265-2.6-1.40.17 (TeX Live 2016)
kpathsea version 6.2.2
Copyright 2016 Han The Thanh (pdfTeX) et al.
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.21; using libpng 1.6.21
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with xpdf version 3.04
E.g. when I input ?glm() the following function description loads in Rstudio:

Help in R renders essentially in HTML or LaTeX. The .Rd files that the documentation is written in are rendered as PDF manual, the help page showing upon ?topic or help("topic") call is the HTML one and this does not any math rendering, it is basically only enclosed in <i> </i> tags.
It is really a shame that the HTML form of the documentation that is used the most completely resigns over the math typesetting. One would think that R as "statistical environment" should definitely handle that. Long story short - this behaviour is no error.
However, actually, if you are a package developer and you care about your users' sanity, there is a way how to achieve that math correctly renders in RStudio Help pane, see https://github.com/wviechtb/mathjaxr.
EDIT: I have created a package that hacks Rd to HTML documentation "conversion" which enables proper math rendering for EVERY help page, see https://github.com/netique/documath. It is a sort of monkey patch, but works well. I am in contact with R developers, so maybe we'll see the feature in a proper release. However, that's for a long run.
EDIT2: R 4.2.0+ actually implements the feature from the aforementioned package, so if you want to see the math rendered properly in HTML help, update your R.

Related

Why capabilities() are not the same between R from the console and RStudio?

I am trying to understand why capabilities() are not the same between R running in the console and RStudio despite that both use the same R binaries.
In the console:
In RStudio:
I only have 1 R installation:
> Sys.getenv("R_HOME")
[1] "/usr/local/lib/R"
Because dynamic libraries may need to be changed by RStudio for its display purposes? On Linux it is well known that via the LD_PRELOAD trick you can get yourself different libraries (for, say, debug versions). Maybe Cairo gets in the way? (And there are add-on packages for Cairo-based graphics too, and now the ragg package and ....) It could also be on purpose.
In my case (on Linux using the CRAN-mirrored Ubuntu binary by Michael based on my Debian package of R), all capabilities() appear to be the same and TRUE (with the exception of aqua, but RStudio Server for example has X11 as FALSE where as RStudio (desktop) still has it. All seems to make sense to me :)

R: Knitting error rticles \vadjust in vertical mode

I tried to use the package rticles within a Rmd file, in order to generate journal publications. When trying to knit the template as it is, I get following error
his is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
I was unable to find any missing LaTeX packages from the error log UC4_SupplySecurity.log.
! You can't use `\vadjust' in vertical mode.
l.300 \leavevmode\vadjust
pre{\hypertarget{ref-Neurobot2013}{}}%
Error: LaTeX failed to compile UC4_SupplySecurity.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See UC4_SupplySecurity.log for more info.
Execution halted
Within the log file, the same error is reported:
Package pdftex.def Info: UC4_SupplySecurity_files/figure-latex/Figure-1-1.pdf
used on input line 291.
(pdftex.def) Requested size: 241.86113pt x 241.86148pt.
! You can't use `\vadjust' in vertical mode.
l.300 \leavevmode\vadjust
I use following configurations
Latex
pdfTeX 3.141592653-2.6-1.40.22 (TeX Live 2021)
kpathsea version 6.3.3
Copyright 2021 Han The Thanh (pdfTeX) et al.
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.37; using libpng 1.6.37
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 4.03
R 4.0.5
What am I doing wrong?
Unfortunately I cannot provide a reproductible example, as it corresponds to the raw Frontiers Journal Article. The same issue persists with the Elsevier Journal Article template.
The template compiles if I remove all references and citations [#XY], but a publication without references is a no-go.
I believe this issue with rticles has been reported in the Github repo already
https://github.com/rstudio/rticles/issues/383 and it has been fixed in rticles 0.20 which is not yet on CRAN.
So for now, one should do
remotes::install_github("rstudio/rticles")
Using the dev version, I can render without error the default template for Frontier article.
You cannot knit with code in an \texttt{enumerate} or \texttt{itemize} environment. Maybe this is your problem.

How can I get past this error building Bookdown into ePub format?

I'm getting an error building R for Data Science in ePub format
> render_book("index.rmd", epub_book())
output file: strings.knit.md
Error: Functions that produce HTML output found in document targeting epub3 output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: true
Note however that the HTML output will not be visible in non-HTML formats.
Execution halted
Error in Rscript_render(f, render_args, render_meta, add1, add2) :
Failed to compile strings.Rmd
sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Is there something I can do to allow compilation to ePub that will keep all content intact?
For my bookdown::gitbook project I deleted my _output.yml and _bookdon.yml files from the book directory and specified the sharing settings, documentation class etc in the Index.Rmd file.This solved it for my HTML output but I'm not sure whether the same applies for ePub formats.
I am having similar problems, and (for me) narrowed it down to using kable_styling (epub format with kable_styling fails)
No solution yet though... :-<
P.

Understanding BLAS - RMarkdown and regular R session using different BLAS?

My problem is running an Rmd in RStudio runs all chunks perfectly, but knitting (to html document) encounters an obscure error.
To debug, I ran sessionInfo() in both a regular R session and inside a clean Rmd with the same packages loaded.
The only difference is the R session says
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
whereas the Rmd output of sessionInfo() is:
BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
Could this be an issue or is it normal for the R session inside RStudio to use a different BLAS to RMarkdown?
In learning about BLAS, I read that it's the result of calling extSoftVersion()["BLAS"], which is documented here and says:
The name of the binary/executable file for BLAS can be used as an indication of which implementation is in use. Typically, the R version of BLAS will appear as libR.so (libR.dylib), R or libRblas.so (libRblas.dylib), depending on how R was built. Note that libRblas.so (libRblas.dylib) may also be shown for an external BLAS implementation that had been copied, hard-linked or renamed by the system administrator. For an external BLAS, a shared object file will be given and its path/name may indicate the vendor/version. The detection does not work on Windows.
FWIW I tried to simply move the working BLAS into the location of the not-working BLAS (removing the not-working file), but I get
dyld: Library not loaded: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.dylib
Referenced from: /Library/Frameworks/R.framework/Resources/bin/exec/R
Reason: image not found

Random Number Generator for R 3.6.1

I have the latest version of R (3.6.1), but when I use functions that are using the random number generator, they default to an older (i.e. pre-3.6.0) RNG which uses Rounding instead of Rejection for sampling. I am not sure why this is happening, and would appreciate your help resolving it.
set.seed(1)
sample(20)
RNGkind()
R.version
Below are the results of my run:
set.seed(1)
sample(20)
# 6 8 11 16 4 14 15 9 19 1 3 2 20 10 5 7 12 17 18 13
RNGkind()
# "Mersenne-Twister" "Inversion" "Rounding"
R.version
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status Patched
major 3
minor 6.1
year 2019
month 09
day 06
svn rev 77160
language R
version.string R version 3.6.1 Patched (2019-09-06 r77160)
nickname Action of the Toes
Based on the NEWS and the linked discussion, I am expecting the output of RNGkind() to look as follows instead:
# "Mersenne-Twister" "Inversion" "Rejection"
Am I misunderstanding the NEWS?
As suggested by #JanvanderLaan in the comments, a possible problem might stem from an .RData file being loaded upon start up. For example if one had a previous version of R installed an every used it, the initial working directory from getwd() upon starting up a session will contain an .RData file and a .Rhistory file, if one ever saved the session. Usually this is the documents folder on windows if one uses Rstudio, which most individuals goes out of their way to clear of old or unusual files.
Following the suggestion in the comment, going to the directory output by getwd() in a fresh R session, I found an .RData file, closed the existing R sessions without saving the current session, and reopened a new R session. And it seems to have correctly fixed the problem as can be seen below. Thus it seems the method for generating random numbers is indeed saved between sessions within the .Rdata file.
RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rejection"
Edit (illustration)
We can actually quite easily illustrate this in a fresh R session, regardless of which random number generator is set. Assuming one has ever opened and saved an R session prior to R-3.6.1, the following code illlustrates the problem
#Assuming that the R session has just opened
>RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rounding"
>RNGversion("3.6.1")
>RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rejection"
>load(".RData", verbose = TRUE)
Loading objects:
.Random.seed
>RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rounding"
As can be seen, it stores the .Random.seed, however what is not shown is that the type of random number generator is also imported, upon loading the previous environment. Executing
file.remove(".RData")
q("no")
should thus fix the issue for future sessions, assuming working directory has not been changed in the current session.
I had hoped that setting a bounty would extract a definite answer to what caused OP's problem. While that didn't happen, some comments and answers suggested a few reasons. I provide an additional answer here to put them all in one place and provide a little better illustration for how to tell when one thing is happening versus the other.
Suggested causes:
Seed being set by a .RData file in the initial working directory
RNG type being set by .Rprofile
Bug in a recent patch
Seed set by a .RData file
As discussed in Oliver's answer, this could be caused by a .RData file in your initial working directory. I won't go into much more detail (you can consult the linked answer for that), but I did want to show what you would see on startup if that were the case. This is what the start up message in R looks like on my machine:
R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
If you are reading in a .RData file on startup that could cause that, you'd see a notification about that right after the last paragraph of the startup message:
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Workspace loaded from ~/.RData]
RNG type set by .Rprofile
.Rprofile is a script that runs on startup that you can use to set some settings at the outset of your session. (You can read a little more about it here or here, or in the R documentation). Though I doubt this is the case for you, it is at least possible the problem was caused by a .Rprofile file being run with a line something like the following
RNGkind(sample.kind = "Rounding")
If you had such a setting in a .Rprofile file that was causing your problem, you'd see a warning at the end of your startup message:
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used
Bug
If you see neither of those messages at startup, my best guess is that this is caused by some kind of bug introduced in a recent patch to R 3.6.1. I kind of hesitate to say that, but I can't see another option (I had kind of hoped that offering a bounty would draw an answer that provided such another option). If so, I'd report it as a bug; find out more here.

Resources