check if a program is installed - r

I'm writing a function that uses pandoc in R through the command line. How can I use R to check if pandoc installed (I also assume it would have to be on the path which may be an issue for windows users)?

I don't have pandoc to install , but generally I test if a program is installed like this :
pandoc.installed <- system('pandoc -v')==0
For example to test if java is installed:
java.installed <- system('java -version') ==0
java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
> java.installed
[1] TRUE

This suggestion is based entirely on my personal experience with this question that RStudio can't seem to read what's in my .bashrc file on my Ubuntu system. I have installed Pandoc using the cabal install pandoc method described here since there were features I needed from more recent versions of Pandoc than were available with Ubuntu's package manager. Running R from the terminal could detect Pandoc as expected using Sys.which, but when using RStudio, it could not. I have no idea if this is a problem with Windows users or not though!
One alternative/workaround in this case is actually creating a vector of typical paths where you expect the Pandoc executable to be found (under the presumption that many users don't really futz around with where they install programs). This info is, again, available at the install page linked above, plus the typical C:\\PROGRA~1\\... path for Windows. Thus, you might have something like the following as the paths to Pandoc:
myPaths <- c("pandoc",
"~/.cabal/bin/pandoc",
"~/Library/Haskell/bin/pandoc",
"C:\\PROGRA~1\\Pandoc\\bin\\pandoc")
# Maybe a .exe is required for that last one?
# Don't think so, but not a regular Windows user
Which you can use with Sys.which() (for example, Sys.which(myPaths)) and some of the other ideas already shared.
If the first option is uniquely matched, then there's no problem: you can use system calls to Pandoc directly.
If any of the other options are uniquely matched, you can write your functions in such a way that you paste in the full path to the executable in your system call instead of just "pandoc".
If the first option and any of the other options are matched, then you can just select the first option and proceed.
If none are matched, prompt the user for the path to their Pandoc installation or provide a message on how to install Pandoc.

I suppose you could use Sys.which and see if the result is an empty string.
pandoc.location <- Sys.which("pandoc")
if(pandoc.location == ""){
print("pandoc not available")
}else{
print("pandoc available")
}

Related

How to build qpdf on Windows?

When running the checks for my R-package (via devtools::check()) I face the warning ''qpdf' is needed for checks on size reduction of PDFs. I found this question were it was suggested (if I understood the answer correctly) to run Sys.which(Sys.getenv("R_QPDF", "qpdf")) and see whether qpdf is found or not. In my case this just returns
qpdf
""
so, I think I didn't install qpdf correctly. Unfortunately it seems to be quite complicated to install qpdf on Windows. My first side question is: does it really is so painful and complicated to install qpdf for Windows or is there an easy solution?
I've followed the instructions until it is said to add C:\MinGW-w64\bin and C:\MinGW-w64\lib\mingw to the PATH variable. But then I don't find further specific instructions to install qpdf, only about how to build qpdf with different other programs. The second side question is: is my assumption correct that after I've build qpdf it is installed? But the real question is: What is the best way to build qpdf? I tried the ./config-mingw32 and ./config-mingw64 commands from the section "Building with MinGW" in my C:\MinGW\msys\1.0\bin\bash.exe but got the error messages ./config-mingw32: No such file or directory and have no idea how to fix this issue.
I'm using Windows 10, R version 3.3.2 Patched (2017-01-07 r71934) -- "Sincere Pumpkin Patch" and RStudio 1.0.136.
You basically do not need to build the file on windows. Please follow three steps below:
Download qpdf for windows from https://sourceforge.net/projects/qpdf/?source=typ_redirect
Extract files in a temp folder
Copy the contents of the bin folder to %SystemRoot%\System32
job done!
Sys.which(Sys.getenv("R_QPDF", "qpdf"))
qpdf
"C:\\WINDOWS\\SYSTEM32\\qpdf.exe"
To flesh out an answer provided elsewhere:
If you are running the 32-bit version of R, it is important that you download the 32-bit version of qpdf, which is the version linked from the SourceForge homepage. If you are running a 64-bit installation of R, you will need to do a bit of digging to locate the 64-bit version of qpdf, which is buried a little more deeply (version 10.0.1 is listed here).
Rather than copying files to C:/Windows/System32, a potentially safer option is to extracted the zipped qpdf directory to C:\Program Files. If you do this, you'll need to add C:\Program Files\qpdf-version_number\bin to your system PATH under the environment variables.
To do this within R, run Sys.setenv('PATH' = paste0('C:\Program Files\qpdf-version_numer\bin;', Sys.getenv('PATH')))
To do this in Windows, open the start menu, type "edit the system environment variables" to open the System Properties, and at the bottom of the "Advanced" tab click "Environment variables". Find the "Path" entry under "System variables" and click "Edit". Then, re-start R so it picks up the modified PATH.
One further step may be required to convince Windows that pqdf is safe to run.
Navigate to C:\Program Files\qpdf-version_numer\bin and execute qpdf.exe (by double-clicking). Windows 10 throws up a security warning, as it's an unrecognized executable file. You'll need to use the more options link to find the button to run the program. This done, Windows will recognize the file as safe to run and allow other software, including R, to use it.

Architect / StatET: Launching R Console was cancelled because R engine failed

I recently upgraded to Architect version 0.9.7 on my Win 7 machine. However, after starting Architect, the RJ console wouldn't launch and I got the error message:
‘Launching the R Console was cancelled, because It seems starting the R engine failed.”
Please make sure that R package 'rj' (2.0 or compatible) is installed and
that the R library paths are set correctly for the R environment
configuration 'Embedded R Server'.
I installed the packages ‘rj’ and ‘rj.gd’ through the RTerm run console (which does happen to work) within Architect using the command suggested on www.walware.de:
install.packages(c("rj", "rj.gd"), repos="http://download.walware.de/rj-2.0")
Then, I restarted windows, and restarted Architect. Same error message. I zoomed in on the details of the error message and noticed the following line:
SEVERE: Path to rj package not found. Use R_LIBS or java property
'de.walware.rj.rpkg.path' to specify the location.
So I opened an RTerm run console to check whether any of my .libPaths() locations contains the directories ‘rj’ or ‘rj.gd’. Oddly enough, they do. My first .libPaths path contains both packages. So that doesn’t seem to be the reason.
How could this be fixed?
I received prompt and helpful feedback from Open Analytics (thanks!), who informed me that the problem is due to changes in Java 8. With Java 7, Architect works fine. The next release of Architect will incorporate the changes in Java 8.
In order to get it work with Java 7, enforce the use of a specific Java Runtime, by using the -vm argument in the architect.ini file as follows:
-vm
C:\Program Files\Java\jre7\bin\javaw.exe
(mind the line break, which is required)
This is particularly useful if you have several Java versions on your system where the default Java is not suitable for use with Architect. The architect.ini file is found directly at the top level of the installation folder of Architect. You can find out where this is by looking at the Properties of the Architect shortcut on your desktop.
N.B. the -vm argument should be put before the -vmargs argument otherwise it is simply ignored!
N.B. examples of -vm specification on GNU/Linux and Mac OS X:
http://wiki.eclipse.org/Eclipse.ini#-vm_value:_Linux_Example
http://wiki.eclipse.org/Eclipse.ini#-vm_value:_Mac_OS_X_Example

How do I initialize LLVM's external symbolizer?

When compiling with -fsanitize=memory I get WARNING: Trying to symbolize code, but external symbolizer is not initialized! when running the program. How do I initialize the external symbolizer?
I solved my own problem using MSAN_SYMBOLIZER_PATH=$(which llvm-symbolizer-3.4) ./a.out. The problem is that Ubuntu postfixes the version number but the binary doesn't know that. Of course you need to use MSAN instead of ASAN when using the memory sanitizer.
You are supposed to be able to set the ASAN_FILTER environment variable to point at a symbolizer, but I could not get it to work. However, you can redirect stderr into a symbolizer after the fact. You'll still get the warnings about the uninitialized symbolizer, but the filenames and line numbers will be correct.
You can use asan_symbolizer.py as the external symbolizer. After downloading it from that link (to /tmp, for example), invoke your program like so (in bash, for this example):
./myprogram 2>&1 | /tmp/asan_symbolize.py | c++filt
On my Ubuntu system, the issue is that LLVM's tools are installed under /usr/bin with version suffixes (like llvm-symbolizer-4.0), and the sanitizer tools are looking for them without version suffixes.
LLVM also installs its binaries to, e.g., /usr/lib/llvm-4.0/bin; the tools under /usr/bin are actually just symlinks. So an easy solution is to add the appropriate /usr/lib/llvm-*/bin directory to your path when working with sanitizers.
I received such warning when I run program debug version (compiled with -fsanitize=address) on Linux machine that didn't contain clang installation. The problem disappeared after I installed clang from devtoolset.

Platform neutral way to check if a program exists (e.g. pdfcrop) while creating vignette

I have a vignette which uses pdfcrop.
This is not going to be available on every computer or OS: The R maintainers have told me that pdfcrop is on their Debian systems, but apparently not others, for example. So I'd like to include in the vignette some logic to figure out if this program is available and use it if possible (I'm using knitr to build the vignette and hence knit_hooks$set(crop = hook_pdfcrop) to activate it).
I know I can use .Platform to get the OS, and if I'm on unix then I can use which pdfcrop via system() to tell me where/if the program is installed, but I don't know how to make this process general for OSX, linux, Windows etc, and I'm not sure how to properly grab the return value of which or the corresponding commands for other platforms.
Put another way, I'm trying to do something like this question but I'm not checking for R packages, I'm checking for non-R programs. I'm turning to SO since I have neither the knowledge nor the platforms to check it on.
R has a function Sys.which() for just this purpose. It is, in the words of its help page, "an interface to the system command ‘which’, or to an emulation on Windows".
Here's what a call to it looks like on my own Windows machine:
Sys.which("pdfcrop")
# pdfcrop
# "C:\\PROGRA~2\\MIKTEX~1.9\\miktex\\bin\\pdfcrop.exe"
To test whether an executable exists, you could do something like this:
Sys.which("pdfcrop") != ""
# pdfcrop
# TRUE
Sys.which("pdfpop") != ""
# pdfpop
# FALSE

How do I get Rtools 3.0 working for R 3.0? R can't find the installation

I'm trying to get R to recognize that I have installed Rtools.exe. I've tried installing it in several different locations. However, whenever I restart R and load devtools I get the same error:
> library(devtools)
WARNING: Rtools is required to build R packages, but is not currently installed.
Please download and install Rtools 3.0 from http://cran.r-project.org/bin/windows
/Rtools/ and then run find_rtools().
Naturally, whenever I try the function find_rtools() I get the same error message. I'm done trying to figure this out with help files, as the devtools help doesn't offer much. I've tried installing in several of the locations returned by get_path() but the problem persists.
FYI, currently my R is installed at D:/R.
Has anyone else had this problem, and more importantly, can anyone help me?
I'm sorry if this is a duplicate, and I've tried to include as many relevant details as possible so if you need any clarification please let me know.
Thanks,
Nick
Grab the batch file Rpathset.bat from batchfiles, edit the SET statements in it appropriately and then run R like this from the Windows console:
Rpathset
Rgui
I ran into this issue running a Windows 7 machine without admin rights. I leveraged G. Grothendieck's response, with a few additional steps.
Get Rpathset.bat from https://code.google.com/p/batchfiles/
Edit the Set statements in Rpathset.bat to your paths
Run CMD and change directories to where Rpathset.bat is stored
In the command prompt, run rpathset.bat > mypath.txt
Open mypath.txt with a text editor
Click Windows Start and search for environment
Click Edit environment variables for your account
Click path and then edit
Copy and paste the entire path from mypath.txt into the Windows path (everything after the path statement)
Restart RStudio and check that devtools::find_rtools() is TRUE
This file rtools.r contains the code for find_rtools(). It looks like it is trying to find ls and gcc in your system path (and then does some more checks based on the version) and if that fails it tries to look up SOFTWARE\R-core\Rtools in the the Windows registry.
As Thomas mentioned here, "Rtools is not a package." This is what I did to install Rtools. My RStudio is running R version 3.1.3. So I downloaded Rtools31.exe from https://cran.r-project.org/bin/windows/Rtools/. I executed the exe file, selected all the default options in the Setup Wizard and installed. (I had admin privilege on the computer I was using but I am not sure if that makes any difference.) To check that it was properly installed, you can enter find_rtools() in the R prompt which should return TRUE if Rtools installed properly.
For R 3.0, you probably just need to download Rtools30.exe and follow the same steps above.
I ran into this problem trying to install Rtools33 to use Hadley Wickham's lineprof package. I got that error multiple times. I'm running Windows 7 on a laptop.
I was able to fix the problem by running the Rtools installation file as the windows Admin (i.e. right-click the Rtools33.exe and choose "Run as administrator")
I also made sure to point the path to the R source directory, which I kept in C:\Rtools\ (which contains C:\Rtools\bin\ and C:\Rtools\gcc-4.6.3\).
This solved the problem for me and I am now able to install lineprof from GitHub and make use of it.

Resources