Running r function with rscript fails after loading package "methods" [duplicate] - r

I've recently compiled R on Ubuntu Server 16.04. I'm attempting to run an R Script using the Rscript function, but I'm running into errors.
When I run the script using Rscript, the output shows the functions as() and new() from the methods package included in the R distribution cannot be found. When I run R interactively, I'm able to use these functions without any problem.
Any help would be tremendously appreciated!

It is a design bug in Rscript: it does not load methods on startup.
Two fixes:
Add library(methods) to your script, or
Do sudo apt-get install r-cran-littler and use r instead of Rscript.
r has been loading methods since day one as this behaviour of Rscript never really made sense.

Related

Installing a package in R inside a script

I'm a beginner with R. I want to launch R scripts at specific moments, managing this with Linux's cron and launching the scripts as Rscript name_of_the_script.
I have installed tidyverse in Rstudio, with install.packages("tidyverse"). Ok, but I guess that installation is specific to the Rstudio environment. When working in a script (not using Rstudio), and launching that script with Rscript, the library tidyverse is not installed. Even worse, I couldn't install it in the script with install.packages("tidyverse").
What do you suggest? Thanks.
The library is, in fact, probably installed. It is difficult to be sure of what is the problem without more details, but I would guess that you did not load your library in your script. Try to add the following at the beginning on the first line of your script
library(tidyverse)
The solution is you simply use require() to load your package without worrying about the path.
require(dplyr)

How do I debug a segfault which only occurs during vignette building?

I have Rcpp-based C++ code with a memory access bug which is only revealed when compiling vignettes (both in classic R and devtools manners).
The crashing code runs fine on its own in vanilla R environment.
I've tried things like:
R -d gdb --vanilla -e "devtools::build()"
R -d gdb --vanilla -e "devtools::check()"
but after digging deep I find that both devtools and R start new processes to build the vignettes during either building or checking a package.
I therefore modified /usr/lib/R to always be invoked -d gdb -ex run but this wouldn't run because it messed up the complicated argument passing between each of the steps.
I've no idea how to proceed at this point as I can't even reproduce the error with a simple R command invoking the compiled code. Could this be an unexpected interaction of Rcpp or other packages which differs between the vignettes building and vanilla interactive environment?

Run R Script - Ubuntu Server

I've recently compiled R on Ubuntu Server 16.04. I'm attempting to run an R Script using the Rscript function, but I'm running into errors.
When I run the script using Rscript, the output shows the functions as() and new() from the methods package included in the R distribution cannot be found. When I run R interactively, I'm able to use these functions without any problem.
Any help would be tremendously appreciated!
It is a design bug in Rscript: it does not load methods on startup.
Two fixes:
Add library(methods) to your script, or
Do sudo apt-get install r-cran-littler and use r instead of Rscript.
r has been loading methods since day one as this behaviour of Rscript never really made sense.

Why does building an R package stop my code from working?

It seams that upon installing my custom R package code stops to work? Why?
In detail, I have a tiny R package goEnrichment (click to see the Github repo). The R package includes just just two functions and some binary data.
Also there are two test R-scripts that I run after installing the R package with R CMD INSTALL goEnrichment. One test script fails while the other works.
The only difference between the two scripts is that in the working version I require the necessary libraries and source the functions file manually, while in the other failing script I require my goEnrichment library directly.
Note, that both scripts are started directly from their directory goEnrichment/exec.
Start the working test with cd goEnrichment/exec && Rscript testGoEnrichment_works.R. Excerpt:
# This is the WORKING version
require(GOstats)
require(GSEABase)
require(RMySQL)
source( "../R/goEnrichment.R" )
Start the failing test script with cd goEnrichment/exec && Rscript testGoEnrichment_fail.R. Excerpt:
# This version FAILS
require(goEnrichment)
The error I get is somewhat uninformative to me:
'dimnames' applied to non-array.
The built R package fails on two platforms, that is on Debian Wheezy 64 with R 3.0.2 and on Mac OS X Yosemite with R 3.1.1.
I really have no clue why this happens. I checked for file format and unexpected characters using Vim. The cause does not seem to be the DESCRIPTION file either, because all libraries, goEnrichment depends on, are imported.
Does any one have a clue what causes this very weird error? Help will be much appreciated.
After much testing I found the source of the problem.
For some reason when requiring my package with
require(goEnrichment)
the generic function
summary.GOHyperGResult
is not loaded, as it should be. The method however is available when the packages, goEnrichment depends upon, are required manually. As to why this is, I had no time to investigate. I believe the here reported error is related to this bug.
Thanks to Tyler for your efforts, very much!

Cannot load forecast or RcppArmadillo packages from Script

When I run the commands library(forecast) and library(RcppArmadillo), there is no error. However, when I try to run these commands from a script, my script is unable to execute.
There are a few nuances to this case. Firstly, I only encounter this error on machines with R version > 2.14. I successfully ran this script on R 2.13.0, and encountered the error on machines with R version 2.14.1 and 2.14.2.
Additionally, the command find.package("forecast"), and similarly find.package("RcppArmadillo") does not return an error from inside the script, meaning that the script can locate the packages but cannot load them.
I have successfully ran a script replacing library(forecast) with the following 6 other packages: Rcpp, tseries,fracdiff,qcc,quadprog, zoo, and parallel.
Does anyone know why I cannot load these two packages from a script even though I can load them in the R console, and why this error occurs only for R versions > 2.14? My initial thought was that it might be related to the fact that these packages depend on R>=2.14.
Thank You Very Much.
We found that the issue was that RcppArmadillo depends on the Rlapack.dll file, which the script was not able to locate when loading the library. The solution was to copy the Rlapack.dll file to the library/RcppArmadillo/libs/i386 subfolder. After making this change, we were able to successfully execute our script.
We have two questions about this issue.
1) Why, when executing via R console, were we able to locate the Rlapack.dll file, when, during script execution, we were unable to find the dependent file?
2) Why did this issue occur only in R > 2.14? Was this a result of an update to either the RcppArmadillo or forecast packages?

Resources