Set 'texi2dvi' for 'R CMD Rd2pdf' - r

My texi2dvi is apparently in a place where R CMD Rd2pdf doesn't expect it. Mine is at /usr/local/bin/texi2dvi, and it's being looked for at /usr/local/opt/texinfo/bin/texi2dvi:
[KenMacBook:~/git] % \R CMD Rd2pdf missing
Hmm ... looks like a package
Converting Rd files to LaTeX
Creating pdf output from LaTeX ...
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: /usr/local/opt/texinfo/bin/texi2dvi: No such file or directory
Output:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: /usr/local/opt/texinfo/bin/texi2dvi: No such file or directory
Output:
Error in running tools::texi2pdf()
I can work around this by running R_TEXI2DVICMD=/usr/local/bin/texi2dvi R CMD Rd2pdf, and then the docs are built correctly.
I'd like to put that setting in my .Rprofile so that things like RStudio (which won't read my .zshrc) and other random R sessions will see the setting. But neither of the following seems to have any effect in my .Rprofile:
Sys.setenv(R_TEXI2DVICMD='/usr/local/bin/texi2dvi')
options(texi2dvi='/usr/local/bin/texi2dvi')
I'm guessing .Rprofile doesn't get read by R CMD commands, is that correct? Is there an appropriate place to put my settings?
UPDATE:
Since Dirk doubts my doubting of .RProfile for affecting R CMD Rd2pdf :-), here's my evidence:
[KenMacBook:~/git] % tail -n2 ~/.Rprofile
Sys.setenv(TEXI2DVI='/no/where')
cat("End of RProfile\n")
[KenMacBook:~/git] % Rscript -e '2+2'
End of RProfile
[1] 4
[KenMacBook:~/git] % R CMD Rd2pdf missing
Hmm ... looks like a package
Converting Rd files to LaTeX
Creating pdf output from LaTeX ...
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: /usr/local/opt/texinfo/bin/texi2dvi: No such file or directory
Output:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: /usr/local/opt/texinfo/bin/texi2dvi: No such file or directory
Output:
Error in running tools::texi2pdf()
Notice that the file's settings are respected in a normal R session, but setting TEXI2DVI has no effect here.

That seems wrong as /usr/local/bin/texi2dvi should be in the $PATH.
I have
edd#max:~$ grep texi2dvi /etc/R/Renviron
## used for options("texi2dvi")
R_TEXI2DVICMD=${R_TEXI2DVICMD-${TEXI2DVI-'/usr/bin/texi2dvi'}}
edd#max:~$
Note that if you want to set the TEXI2DVI environment variable, you probably have to do start before you start R -- think ~/.bash_profile
Here is an example explicitly setting TEXI2DVI:
edd#max:/tmp$ TEXI2DVI=/no/where R CMD Rd2pdf Rcpp-package.Rd
Converting Rd files to LaTeX ...
Rcpp-package.Rd
Creating pdf output from LaTeX ...
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: 1: /no/where: not found
Output:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: 1: /no/where: not found
Output:
Error in running tools::texi2pdf()
edd#max:/tmp$
As you can see, it is respected.
Edit: Also let's not forget Renviron and Renviron.site so you have plenty of choices so set his.
Edit 2: As you seem to doubt ~/.Rprofile:
edd#max:~$ tail -1 .Rprofile
cat("End of .Rprofile\n")
edd#max:~$ Rscript -e '2+2'
End of .Rprofile
[1] 4
edd#max:~$

Had the same problem and figured out how to fix it - I think it has something to do with a previous installation of macports interfering with the path when R has been installed using brew (assuming you're on OSX).
Run the following in terminal:
defaults write com.apple.finder AppleShowAllFiles TRUE
Then go Apple > Force Quit > Finder > Relaunch. You'll now be able to see hidden files.
In your user directory there may be a file named .profile, in this file I commented out the line (i.e. put a # in front of it, as shown).
#export PATH=/opt/local/bin:/opt/local/sbin:$PATH
Then navigate to your R.home() (get this by running R.home() in R)
R.home()
[1] "/usr/local/Cellar/r/3.2.4_1/R.framework/Resources"
And modify the following line in Renviron
R_TEXI2DVICMD=${R_TEXI2DVICMD-${TEXI2DVI-'/usr/local/bin/texi2dvi'}}
This fixed it for me.
To put finder back to usual run
defaults write com.apple.finder AppleShowAllFiles FALSE
in the terminal and relaunch it.
Hope that helps.

The back-and-forth with Dirk produced some solutions, but they're pretty buried, so I summarize them here.
Diagnosis: etc/Renviron contains stale info.
My /usr/local/Cellar/r/3.2.2_1/R.framework/Versions/3.2/Resources/etc/Renviron file (installed using Homebrew) contains this line:
R_TEXI2DVICMD=${R_TEXI2DVICMD-${TEXI2DVI-'/usr/local/opt/texinfo/bin/texi2dvi'}}
That's a remnant of someone (possibly me, possibly Homebrew's R package creator) who installed MacTeX in the default location, and then that path got frozen in time in the Renviron file. My texi2dvi is now at /usr/local/bin/texi2dvi, so this value needs to be overridden somehow.
1) $HOME/.Rprofile and $HOME/.Renviron won't help.
They don't take effect soon enough for R to notice them. R sets options("texi2dvi") based on the environment it sees at startup,
% tail -n2 ~/.Rprofile
Sys.setenv(TEXI2DVI='/no/where')
options(texi2dvi='/no/where/else')
% cat ~/.Renviron
TEXI2DVI=/no/where/at/all
% R CMD Rd2pdf myPackageDirectory # Still no joy
Hmm ... looks like a package
Converting Rd files to LaTeX
Creating pdf output from LaTeX ...
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: /usr/local/opt/texinfo/bin/texi2dvi: No such file or directory
...
See "Initialization at Start of an R Session" for more info about startup files, though as shown above, the information in that document about overriding R_HOME/etc/Renviron is either incorrect or incomplete for this situation. Perhaps it should be amended in the section about R_CHECK_ENVIRON and R_BUILD_ENVIRON to also include something about R CMD RD2*, but I'm not sure whether that's what's going on, I only know this isn't a solution.
2) $HOME/.zshrc (and friends) won't help.
On OS X, your shell startup file is not consulted when you launch GUI apps. You could use defaults write or launchctl setenv to change the TEXI2DVI variable so that it's set when R launches, but you'd also have to stick it in your shell startup file for processes not started by launchd, which is icky. I also version my dotfiles, and I don't like sticking this bit of configuration in the launchctl ether where I can't easily remember it's there. But launchctl is presumably one solution to this.
3) etc/Renviron.site won't help.
This one is surprising - I expected it to work:
% cat /usr/local/Cellar/r/3.2.2_1/R.framework/Resources/etc/Renviron.site
R_TEXI2DVICMD=/usr/local/bin/texi2dvi
TEXI2DVI=/usr/local/bin/texi2dvi
% \R CMD Rd2pdf myPackageDirectory
Hmm ... looks like a package
Converting Rd files to LaTeX
Creating pdf output from LaTeX ...
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'Rd2.tex' failed.
Messages:
sh: /usr/local/opt/texinfo/bin/texi2dvi: No such file or directory
So Renviron.site's settings aren't taking effect here. I'm getting impatient, so I didn't try to diagnose why.
3) Editing etc/Renviron as a last resort
So this finally works:
% grep TEXI2DVI /usr/local/Cellar/r/3.2.2_1/R.framework/Resources/etc/Renviron
TEXI2DVI=/usr/local/bin/texi2dvi ## Added by Ken
R_TEXI2DVICMD=${R_TEXI2DVICMD-${TEXI2DVI-'/usr/local/opt/texinfo/bin/texi2dvi'}}
I don't like it much, because I'll lose those settings next time I upgrade R. And R's documentation specifically says "do not change ‘R_HOME/etc/Renviron’ itself". But at least it works.

Related

How can I activate a '.js' script from R?

I have a '.js' script that I usually activate from the terminal using the command node script.js. As this is part of a process where I first do some data analysis in R, I want to avoid the manual step of opening the terminal and typing the command by simply having R do it for me. My goal would be something like this:
...R analysis
write.csv(df, "data.csv")
system('node script.js')
However, when I use that specific code, I get the error:
sh: 1: node: not found
Warning message:
In system("node script.js") : error in running command
Of course, the same command runs without problem if I type it directly on the terminal.
About my Software
I am using:
Linux computer with the PopOS!
RStudio 2021.09.1+372 "Ghost Orchid"
R version 4.0.4.
The error message node: not found indicates that it couldn't find the program node. It's likely in PATH in your terminal's shell, but not in system()'s shell (sh).
In your terminal, locate node by executing which node. This will show the full path to the executable. Use that full path in system() instead.
Alternatively, run echo $PATH in your terminal, and run system('echo $PATH') or Sys.getenv('PATH') in R. Add any missing directories to R's path with Sys.setenv(PATH = <your new path string>)
Note that system2() is recommended over system() nowadays - but for reasons unimportant for your case. See ?system and ?system2 for a comparison.
Examples
Terminal
$ which node
/usr/bin/node
R
system('/usr/bin/node script.js')
# alternatively:
system2('/usr/bin/node', c('script.js'))
or adapt your PATH permanently:
Terminal
% echo $PATH
/usr/local/bin:/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
R
> Sys.getenv('PATH')
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback"
> Sys.setenv(PATH = "/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback")

data.table fread error - gzip file - set temporary directory

I'm attempting to read a .gz-file using data.tables fread-function. I have tried the syntax suggested here:
dt = fread("gunzip -c myfile.gz")
but I get a verbose error message:
Error in fread("gunzip -c myfile.gz") :
File is empty: C:\Users\MARK~1.MUR\AppData\Local\Temp\RtmpIBawPA\file498c1c4114ef
In addition: Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c (gunzip -c myfile.gz) > C:\Users\MARK~1.MUR\AppData\Local\Temp\RtmpIBawPA\file498c1c4114ef' had status 1
2: In shell(paste("(", input, ") > ", tt, sep = "")) :
'(gunzip -c 180227.2101.2017.MRE.csv.gz) > C:\Users\MARK~1.MUR\AppData\Local\Temp\RtmpIBawPA\file498c1c4114ef' execution failed with error code 1
My guess here is that access to a temporary file is being denied by my IT masters (?). If this is the case how do I set the temporary file path to say the current directory for the unzip?
As you are on a Windows PC you probably don't have access to command line tools, which might be the reason for this.
A possible solution might be to unzip first and then read with fread. The following example works on my Windows VM:
write.csv(mtcars, 'mtcars.csv')
zip('mtcars.csv.zip', 'mtcars.csv')
unzip('mtcars.csv.zip')
fread('mtcars.csv')
For .gz files, you can use the gunzip function from R.utils. The following example works for me:
write.csv(mtcars, gzfile('mtcars2.csv.gz'))
library(R.utils)
gunzip('mtcars2.csv.gz')
fread('mtcars2.csv')
Consequently, you might need something like this:
library(R.utils)
gunzip('myfile.gz')
fread('myfile.csv')
Try read_csv() from the readr package, which handles .gz automatically:
dt = as.data.table(read_csv("myfile.gz"))
(or another read_* function if it's not a csv)

/usr/bin/texi2dvi: cannot create directory

After upgrading Ubuntu from 14.04 to 16.04, whenever I use
knit2pdf(input="ABC.Rnw", quiet = TRUE)
in RStudio Version 0.99.1197, I get the following error:
processing file: ./202Analysis.Rnw Error in texi2dvi(file = file, pdf
= TRUE, clean = clean, quiet = quiet, : Running 'texi2dvi' on 'ABC.tex' failed. Messages: mkdir: cannot create directory
‘Yaseen/XYZ’: No such file or directory /usr/bin/texi2dvi: cannot
create directory: Yaseen/XYZ
Edited
My directory ‘Yaseen/XYZ STU’ is containing spaces. Everything works fine after removing these spaces. However the same code was working fine with Ubuntu 14.04. How can I make this (directory names containing spaces) workable with Ubuntu 16.04?
Edited 2
This link says:
To build the PDF documentation, you will need a version of TeX Live or texinfo that includes texi2dvi (BEWARE: recent TeX Live, and some texinfo RPMs, do NOT include texi2dvi).
I agree with the previous comments in that the space in the directory seems to be the culprit.
Have you tried escaping the whitespace in your output directory?
knit2pdf(input="ABC.Rnw", quiet = TRUE, output = "Yaseen/XYZ\ STU")

How to identify LaTeX errors in .Rd R help files? [duplicate]

When building a package, I received the following warning:
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
I have no idea how to even begin diagnosing this. Is there a tool that tells me what .Rd file the problem is in?
I get no warnings about any of my Rd files in the checking documentation step....
Try R CMD Rd2pdf mypackage to create the manual, and possibly also set the --no-clean option to keep the temporary files. This should allow you to debug the LaTeX code triggering the error.
Though #Dirk's answer also helped me to fix the problem I would like to add a bit which might especially help recent updaters. That is, people who haven't encountered other LaTeX / R troubles after the update to 3.1.3 yet. The problem is little bit more general than just building. For me, on OS X the problem was that R CMD Rd2pdf as well as the R CMD CHECK expected texi2dvi to be in /usr/local/bin while it was in /usr/bin.
A symlink helped to fix the problem. On terminal type:
# to check whether the same issue exists for you
which texi2dvi
# if so
cd /usr/local/bin
ln -s /usr/bin/texi2dvi
Of course if the first line returns something else, you need to adapt the symlink here.
Concluding from the comments and from my own experience the problem often seems to be that some TeX fonts are missing, most often
inconsolata.sty and
upquote.sty
First you have to find the right directory where TeX fonts are stored - in my case this is:
C:\Program Files\R\R-3.3.0\share\texmf\tex\latex
Then you can download them here:
https://www.ctan.org/tex-archive/fonts/inconsolata/?lang=en
https://www.ctan.org/tex-archive/macros/latex/contrib/upquote?lang=en
Just copy them to the respective folder and in many cases the problem will be solved (in my case too). This should work for all operating systems.
...and another reason is that you haven't installed MikTex yet.
Download MikTex from here and follow the dialog prompts to install. I found the defaults to be reasonable and worked well for me.
Try to build your R package again. It should be OK now.
In my case, I had no error when running devtools::check() nor devtools::document() but when running R CMD check mypackage_version.tar.gz I got an error:
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
LaTeX errors found:
* checking PDF version of manual without hyperrefs or index ... ERROR
In this question in RStudio Community they point to a problem with LATEX installation.
I have the LATEX installation suggested in R markdown cookbook: TinyTex.
I fixed the issue by running in the R console
tinytex::latexmk(file = "../mypackage.Rcheck/mypackage-manual.tex")
This command automatically updated my LATEX installation so the output file mypackage-manual.pdf was created. After this, I did not get any other error related to PDF when running R CMD check:
* checking PDF version of manual ... OK
* DONE
If you are on Ubuntu just install Tex Live by this command:
apt-get install texlive and restart Rstudo if you use it.
First, #dirk-eddelbuettel's approach in the current question identified the missing tex package (which was "makeindex" in my case).
system("R CMD Rd2pdf --no-preview --output=./documentation-peek.pdf ." )
# ... <omitted pages of output> ...
# Warning in sys2(makeindex, shQuote(idxfile)) : '"makeindex"' not found
# Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
# unable to run 'makeindex' on 'Rd2.idx'
# Error in running tools::texi2pdf()
Then #pedro-lima's answer in https://stackoverflow.com/a/69968791/1082435 worked for my specific case.
tinytex::tlmgr_install("makeindex")
Oftentimes this error occur because of Unicode characters in the package. In this cases, the error message might look like this.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
! Package inputenc Error: Unicode character (U+009D)
(inputenc) not set up for use with LaTeX.
You can find any Unicode characters in your package using tools::showNonASCIIfile(). Here's a simple way to check for these characters in your functions and documentation:
# functions
functions <- list.files(path = './R', all.files = T, recursive = T, full.names = T)
lapply(X=functions, FUN = tools::showNonASCIIfile)
# documentation
docs <- list.files(path = './man', all.files = T, recursive = T, full.names = T)
lapply(X=docs, FUN = tools::showNonASCIIfile)

Error in untar( ) while using R

I am new to the R programming language and am having basic issues with it. I want to untar a file, but it has not been able to work for me.
Here is the code that I enter:
untar("CD_data.tar", exdir="data")
It then returns the following error message:
/bin/sh: /usr/bin/gnutar: No such file or directory
Warning message:
In untar("CD_data.tar", exdir = "data") :
‘/usr/bin/gnutar -xf 'CD_data.tar' -C 'data'’ returned error code 127
Please help! Thanks!
R on OS X 10.9 (Mavericks) seems to set a wrong TAR environment variable.
You can fix this by adding the following to your .Rprofile (or executing it manually):
Sys.setenv(TAR = '/usr/bin/tar')
Alternatively, you can provide the tar path as an argument when calling untar.
My 2 cents is that you are using a mac and have not installed tar. You are getting value 127 because the command is not found within your $PATH and it's not a built-in command (which is usually the case if you were in unix...
In other words you need to install tar.
Or run it in linux.

Resources