texi2dvi() error when running a .Rnw script with LaunchControl - r

I'm trying to compile a knitr script on a timer using LaunchControl (a launchd GUI for scheduling cron-like jobs on OSX).
I have a dispatcher.R script that does this:
#!/Library/Frameworks/R.framework/Resources/Rscript
library("knitr")
setwd("~/somedirectory")
knit2pdf("my_script.Rnw", output= "my_script.tex")
When I run it interactively from in RStudio, my_script.Rnw works great. I get the desired PDF output. However, when launchd runs the dispatcher.R script I get this error:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'my_script.tex' failed.
Execution halted
The .tex file gets generated, but then it doesn't compile. I a would say it was problem with my LaTeX installation path, but since it works using knit2pdf() I'm not sure. What could be the issue?
Still working on this. Updates:
No .log file gets produced with knit2pdf() via LaunchControl, but I get a .tex file and /figure folder.
I updated MacTex and also tried a minimal example of an empty document and I got the same error about texi2dvi.
When I run knit2pdf("my_script.Rnw", output = "my_script.tex") using LaunchControl and then go back to RStudio and run texi2dvi("my_script.tex", pdf = TRUE), then I get the desired outcome.
The problem reproduces on Sierra and Yosemite
On Sierra there is an additional error about In my_script_latex_pkg("framed", system.file("misc", "framed.sty", package = "knitr")) : unable to find LaTeX package 'framed'; will use a copy from knitr
I tried Sys.setenv(PATH = paste(Sys.getenv("PATH"),"/usr/texbin",sep=":")) and it didn't help.
Running $ Rscript dispatcher.R from the command line works just fine. The PDF compiles.
Running a bash script with Rscript dispatcher.Rin LaunchControl does not work; same error about texi2dvi.

To run a .Rnw file using LaunchControl for task scheduling, create the following files in the same directory. Then, run the *.sh script in the scheduler. Voila! The problem I was encountering in my original post was the LaunchControl doesn't (by default, at least) read ~/.bash_profile, so adding the PATH variable into the .sh script resolves this.
1) Your *.Rnw script
This is any knitr script that you can compile without issue from RStudio.
2) A *.R script
#!/Library/Frameworks/R.framework/Resources/Rscript
library("knitr")
setwd("~/some_directory")
knit2pdf("yourscript.Rnw", output = "yourscript.tex")
3) A *.sh script
Make sure that you have the PATH variable to your LaTeX installation.
#! /bin/bash
PATH="/usr/texbin:${PATH}"
export PATH
Rscript yourscript_dispatcher.R
This solution works on OSX Yosemite 10.10.5 on R version 3.3.2 (2016-10-31).

Related

system function usage from Rstudio in linux environment

I have the below line in my R script which is working when I invoke R from command line in Linux environment,
system('spectre run.scs -f psfascii +l mylog.txt >> /dev/null')
But the same line is not working when I run this script by launching RStudio from the command line,
spectre is a valid command which works when R is invoked, but when rstudio the command is not getting recognized.
Point to Note: The spectre command is from an external module apart from R, which I have loaded in the linux terminal like below before calling R and rstudio.
EDITED with PATH from R and RStudio
When I run which spectre from the command line and check its path,
> Sys.getenv("PATH")
[1] "/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/bin:/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/arch:/tool/eda/apps/mentorCalibre/2014.2_33.25/aoi_cal_2014.2_33.25/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/tools/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/share/oa/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/dfII/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/bin:/tool/pandora64/bin:/tool/pandora64/sbin:/tool/pandora/bin:/tool/pandora/sbin:/usr/lib64/qt-3.3/bin:/bin:/usr/bin:/opt/puppetlabs/bin"
But when I check the path in R studio,
> Sys.getenv("PATH")
[1] "/tool/pandora64/.package/qt-5.5.1/bin:/tool/pandora64/.package/R-3.4.3/bin:/tool/pandora64/.package/rstudio-99.9.9/app/bin:/bin:/usr/bin:/tool/eda/apps/cadenceMMSIM/13.11.292:/tool/eda/apps/cadenceMMSIM/13.11.292"
Answer was suggested by #r2evans from the comment section,
Changed the PATH in Rstudio by copying the path from R environment in command line,
Sys.setenv(PATH="/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/bin:/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/arch:/tool/eda/apps/mentorCalibre/2014.2_33.25/aoi_cal_2014.2_33.25/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/tools/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/share/oa/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/dfII/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/bin:/tool/pandora64/bin:/tool/pandora64/sbin:/tool/pandora/bin:/tool/pandora/sbin:/usr/lib64/qt-3.3/bin:/bin:/usr/bin:/opt/puppetlabs/bin")

File not found when using Rscript command

I am trying to run a R script on Linux using Rscript command. However, it gives me an error stating that no such file or directory.
Weirdly, when I use command "less" on that R script, it works and displays the script on the screen.
Can anyone explaining this to me ?

RStudio does not execute `system` commands from Rprofile.site

Using Rstudio (Version 1.0.143) under Ubuntu (16.04), if I add system("echo 'Hello world'") to my /usr/lib/R/etc/Rprofile.site file, I have no Hello world message displayed in R studio at startup.
If I start R from the terminal I have the Hello world message.
It seems that R studio ignores the system commands from Rprofile.site (idem from the .Rprofile file in the home directory)
Is it possible to enable system commands executions or is there a good reason to avoid this behavior ?
For short, RStudio doesn't source Rprofile.site file at all. (I use Ubuntu 17.04 and compile R-3.4.1 myself)
test Rprofile.site
To simply the question, you can use x=1 rather system("echo 'Hello world'") in the Rprofile.site file. Then you open RStudio:
> x
Error: object 'x' not found
That tells you whether RStudio ignores the system() commands in Rprofile.site or R studio ignores the entire Rprofile.site file.
test system()
If you find that your RStudio source theRprofile.site file instead. You can try system("echo haha > x") or system2('echo', 'haha', stdout = T) -> x.
That tells you whether RStudio ignores the system() commands or you can't see the message due to other reasons.
what RStudio says
Finally, see https://support.rstudio.com/hc/en-us/community/posts/200643758-Rprofile-site-
We don't actually implement the code for sourcing Rprofile.site (R does)
by Ian Pylvainen, Support Engineer at RStudio, Inc.

Rscript: There is no package called ...?

I want to run R files in batch mode using Rscript, however it does not seem to be loading the libraries that I need. The specific error I am getting is:
Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted
However I do have the package timeSeries and can load it from Rstudio, RGui, and R from the command line no problem. The issue seems to only be when running a script using Rscript.
My system/environment variables are configured as:
C:\Program Files\R\R-3.1.0\bin\x64 (Appended to PATH)
R_HOME = C:\Program Files\R\R-3.1.0
R_User = Patrick
I am running the same version of R in RStudio, RGui, and R from command line. I've also checked .Library from these three sources and got the same output as well.
How can I run Rscript from command line with the packages that I am using (and have installed) in R?
EDIT:
I am using Rscript via Rscript script.r at the windows command line in the directory where script.r is located.
The output of Rscript -e print(.Library) is [1] "C:/PROGRA~1/R/R-31~1.0/library"
which is consistent with the other three options that I mentioned: [1] "C:/PROGRA~1/R/R-31~1.0/library"
However, if I put this in my script:
print(.libPaths())
library(timeSeries) #This is the package that failed to load
I get an output of:
[1] "C:/Program Files/R/R-3.1.0/library"
Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted
The corresponding call in RStudio gives an additional path to where the package is actually installed:
> print(.libPaths())
[1] "C:/Users/Patrick/Documents/R/win-library/3.1" "C:/Program Files/R/R-3.1.0/library"
In short, the value returned by calling Sys.getenv('R_LIBS_USER') in R.exe needs to be the same as the value returned by calling this at the command line:
Rscript.exe -e "Sys.getenv('R_LIBS_USER')"
and the above value needs to be included in this command line call:
Rscript.exe -e ".libPaths()"
Note that the values of R_LIBS_USER may be differ between R.exe and Rscript.exe if the value of R_USER is changed, either in the .Rprofile or the in target field of user's shortcut to R.exe, and in general, I find that the user library (i.e. .libPaths()[2]) is simply not set in Rscript.exe
Since I'm fond of setting R_USER to my USERPROFILE, I include the following block in at the top of .R files that I wish to run on mulitiple computers or in Rscript.exe's .Rprofile (i.e. Rscript -e "path.expand('~/.Rprofile')"):
# =====================================================================
# For compatibility with Rscript.exe:
# =====================================================================
if(length(.libPaths()) == 1){
# We're in Rscript.exe
possible_lib_paths <- file.path(Sys.getenv(c('USERPROFILE','R_USER')),
"R","win-library",
paste(R.version$major,
substr(R.version$minor,1,1),
sep='.'))
indx <- which(file.exists(possible_lib_paths))
if(length(indx)){
.libPaths(possible_lib_paths[indx[1]])
}
# CLEAN UP
rm(indx,possible_lib_paths)
}
# =====================================================================
As mentioned in the comments, it seems Rscript doesn't recognize the library path defaults automatically. I am writing an R script that needs to be source-able from the command line on different people's computers, so I came up with this more general workaround:
First store the default library path in a variable (Rscript-sourced functions can find this, they just don't automatiocally)
Then include that path in the library() call with lib.loc = argument.
This should work regardless of what the path is on a given computer.
library.path <- .libPaths()
library("timeseries", lib.loc = library.path)
Thanks again to #flodel above for putting me on the right path
This answer will not help the original asker (pbreach), but it may help someone else who stumbles across this question and has a similar problem to me.
I have many bash .sh script files which call RScript to execute .R files. My operating system is Windows 10, and I execute those bash files using cygwin.
Everything had been working fine until yesterday, when I finally upgraded my R from Revolution R 8.0.1 beta to Microsoft R Open 3.4.1. After that upgrade, every bash script that called RScript failed due to the exact same reason asked here (e.g. Error in library(zoo) : there is no package called 'zoo').
Investigation revealed that RScript actually worked fine if called from a DOS shell instead of from a cygwin bash shell.
For example, if I execute this in a DOS shell
C:\Progra~1\Microsoft\ROpen~1\R-3.4.1\bin\x64\Rscript.exe -e ".libPaths()"
I see the output
[1] "C:/Users/HaroldFinch/Documents/R/win-library/3.4"
[2] "C:/Program Files/Microsoft/R Open/R-3.4.1/library"
I eventually discovered the reason. As explained in the R FAQ, to define its home directory, R will first use the R_USER environment variable if defined, else it will use HOME environment variable if defined, else it will use the Windows "personal" directory.
My Windows configuration does not define either R_USER or HOME environment variables. So, in the DOS shell case, R uses my Windows "personal" directory (C:/Users/HaroldFinch/Documents). That is good, because that is where all my libraries are installed (C:/Users/HaroldFinch/Documents/R/win-library/3.4).
In contrast, cygwin defines and exports a HOME environment variable that points to my cygwin user directory, which lacks any R stuff. Hence, RScript called from cygwin had a wrong R home directory, and so failed to load libraries.
There are probably many ways to solve this. I decided to have my bash script set a R_USER environment variable which points to my Windows user directory.
For example, if I execute this in a cygwin bash shell:
R_USER="C:/Users/HaroldFinch/Documents"
export R_USER
/cygdrive/c/Progra~1/Microsoft/ROpen~1/R-3.4.1/bin/x64/Rscript.exe -e ".libPaths()"
I see the output
[1] "C:/Users/HaroldFinch/Documents/R/win-library/3.4"
[2] "C:/Program Files/Microsoft/R Open/R-3.4.1/library"
which is exactly the same output now as the DOS shell example above.
Another cause is packrat. If you are running with packrat, RStudio turns it on for you when you open the project. RScript does not, so you need a packrat::on() early in your script (before the library calls).
As the others have already pointed out, the problem is that Rscript.exe cannot recognise the win-library folder. The easiest solution for me was to explicitly set the path to the library folder by adding:
.libPaths("C:/Users/Benutzer1/Documents/R/win-library/4.0")
to my program. Then it loads all the packages from the win-library folder and it is still capable of loading packages from the standard library folder.

Problems executing script from command line in R. Error message: cannot find path specified

I have been trying to execute a simple test.R
setwd("C:\Users\jdd\Documents")
test <- 2*6598
filename = "test.csv"
write.csv(test,file=filename)
Via the following command line command in Windows:
"C:\Program Files\R\R-2.15.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\Users\jdd\Documents\test.R"
When I execute this I get the following error:
The system cannot find the path specified.
I have been trying to work out a solution on the basis of the provided error message, but failed so far. Wondering if somebody can help me so I can execute the script directly from the command line. Thanks
Thanks #sebastian-c! I tried to use RScript, which I investigated before. However, the problem was a different one. Appears that in my installation there is a R.exe and Rscript.exe file in .\bin, but also one in .\bin\x64. The first one is not working properly, but the second one is. The comment made by #Roland is very important as well, since once working I got this error message!
The following command did the job:
"C:\Program Files\R\R-2.15.2\bin\x64\Rscript.exe" "C:\Users\jdd\Documents\test.R"
and the corrected text.R is:
setwd("C:\\Users\\jdd\\Documents")
test <- 2*6598
filename = "test.csv"
write.csv(test,file=filename)
As mentioned here, it might has something to do with 64bit version of R.
The problem is that Rscript.exe itself is attempting to access a missing file on the system. The obvious fix is explicitly add 'x64' to the path of the other Rscript.exe that was installed:
"C:\Program Files\R\R-2.15.2\bin\x64\Rscript.exe" --version
R scripting front-end version 3.0.2 (2013-09-25)

Resources