Maintaining a local repository for both Windows and Linux - r

I have some R code that I'd like to share with other people in my office, and also run periodically on our servers. We all have Windows 7 desktops, and the servers run Red Hat Enterprise Linux.
I've been through the docs, and I'm stuck. None of the following have all the necessary steps, detail the correct folder structure, or tell me how to build a Linux package, or build a Windows package on Linux.
http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf
http://cran.r-project.org/doc/manuals/R-admin.html#Setting-up-a-package-repository
Creating a local R package repository
So I have my code in git.
$ mkdir ~/daveStuff
$ cd ~/daveStuff
$ git init
$ git remote add origin git#davez0r.co:/opt/git/daveStuff.git
$ git pull origin master
Now in my home directory I have this folder structure:
daveStuff
|-- DESCRIPTION
|-- R
|-- stuff.R
|-- exec
|-- script.R
My description file looks like this:
Package: daveStuff
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2014-02-03
Author: Who wrote it
Maintainer: Who to complain to <yourfault#somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
I'm running apache on one of my servers. So I added this:
/var/www/html/R/src/contrib/3.0/
This correctly maps to the following, where I read any files I put there:
http://davez0r.co/R/src/contrib/3.0/
What I'd like to be able to do is the following, from either Windows or Linux:
> install.packages("daveStuff", repos="http://davez0r.co/R", type="source")
> library(daveStuff)
So first step is that I need to turn my library into a package.
$ cd ~ # one under the "daveStuff" directory
$ R CMD build daveStuff
This creates a zip file:
~/daveStuff_1.0.tar.gz
Now I copy that file to my repository location:
$ cp ~/daveStuff_1.0.tar.gz /var/www/html/R/src/contrib/3.0/
Now if I go like this:
> install.packages("daveStuff", repos="http://davez0r.co/R", type="source")
Warning in install.packages :
unable to access index for repository http://davez0r.co/R/src/contrib
It gives me that error message saying that it can't find the package. So I create a package manifest:
$ cd /var/www/html/R/src/contrib # one under where I put the archive
$ Rscript -e 'tools::write_PACKAGES(".", type="source", subdirs=TRUE)'
This gives me a PACKAGES file:
Package: daveStuff
Version: 1.0
MD5sum: 817bbfedeb218ce0331dd7108408c5e6
NeedsCompilation: no
Path: ./3.0
Now it works when I try to load it:
> install.packages("daveStuff", repos="http://davez0r.co/R", type="source")
Unresolved issues:
I've lost the scripts that were in the exec directory.
Should I wrap these in functions and include them in the library?
Should I stick with source packages?
How do I make Windows binary packages on Linux?

It seems to me that you skipped one final step.
One needs
A local package (which you have)
Tarballs for Linux and binary packages for Windows (not sure)
A local repository (which you started)
Meta-data on your repository (which you seem to miss)
I do that at work for similar setup (some Windows, lots of Linux) via a simple script:
#!/bin/bash
## see e.g.
## http://cran.r-project.org/doc/manuals/R-admin.html\
## #Setting-up-a-package-repository
## http://stackoverflow.com/questions/2905650/creating-a-local-cran-repository
ver=3.00
rsync -vu *tar.gz /SomeServer/R/src/contrib/
rsync -vu *zip /SomeServer/R/bin/windows/contrib/${ver}/
cd /SomeServer/R/src/contrib/
r -e 'tools::write_PACKAGES(".", type="source")'
cd /SomeServer/R/bin/windows/contrib/${ver}/
r -e 'tools::write_PACKAGES(".", type="win.binary")'
I use littler's r binary here, you could equally well use Rscript.

Related

Documenting external data in R package for the inst/extdata folder?

I am working on a package in R, and am looking for a way to document the sources of external data stored in the inst/extdata folder. I know that data in the /data folder can be documented with roxygen as per this SO post.
The trouble seems to be that external data is not exported into the namespace of the package, and therefore tying an roxygen help document to it poses an issue. Is there a way to overcome this and to document external data similar to the method for items in the /data folder?
Here is the reproducible (on linux) example of solution I provided in comments.
Code below will create minimal package
defining dummy DESCRIPTION file
example ext/data/data1.csv csv data file
example man/data1.Rd R documentation file
Then it will build and install package.
mkdir -p my.pkg
cat > my.pkg/DESCRIPTION <<EOL
Package: my.pkg
Type: Package
Title: My pkg
Version: 1.0.0
Description: Example my pkg.
License: GPL-3
EOL
mkdir -p my.pkg/inst/extdata
cat > my.pkg/inst/extdata/data1.sv <<EOL
a,b,c
1,a,2.5
2,b,5.5
EOL
mkdir my.pkg/man
cat > my.pkg/man/data1.Rd <<EOL
\name{data1}
\alias{data1}
\alias{data5}
\title{
my data
}
\description{
desc of data.
}
EOL
R CMD build my.pkg
R CMD INSTALL my.pkg_1.0.0.tar.gz
Now see that manual can be found
Rscript -e 'library(my.pkg); ?data1'
Rscript -e 'library(my.pkg); ?data5'
As you can see we can refer to documentation of extdata using any name defined in alias inside Rd file.

Creating a portable version of R for Mac (and installing package from source for this version)

I am trying to create a completely portable version of R for Mac that I can send to users with no R on their system and they can essentially double click a command file and it launches a Shiny application. I'll need to be able to install packages including some built from source (and some from GitHub).
I am using the script from this GitHub repository (https://github.com/dirkschumacher/r-shiny-electron/blob/master/get-r-mac.sh) as a starting point (it's also pasted below), creating a version of R, but (A) I find that when I try to launch R it gives me an error not finding etc/ldpaths and (B) when I try to launch Rscript it runs my system version -- I run `Rscript -e 'print(R.version)' and it prints out 4.0 which is my system version of R rather than the version 3.5.1 which the shell script has downloaded and processed.
I've experimented with editing the "R" executable and altering R_HOME and R_HOME_DIR but it still runs into issues when I try to install packages to the 3.5.1 directory.
Can anyone provide some guidance?
(By the way docker is not an option, this needs to be as simple as possible end-users with limited technical skills. So having them install docker etc won't be an option)
#!/usr/bin/env bash
set -e
# Download and extract the main Mac Resources directory
# Requires xar and cpio, both installed in the Dockerfile
mkdir -p r-mac
curl -o r-mac/latest_r.pkg \
https://cloud.r-project.org/bin/macosx/R-3.5.1.pkg
cd r-mac
xar -xf latest_r.pkg
rm -r r-1.pkg Resources tcltk8.pkg texinfo5.pkg Distribution latest_r.pkg
cat r.pkg/Payload | gunzip -dc | cpio -i
mv R.framework/Versions/Current/Resources/* .
rm -r r.pkg R.framework
# Patch the main R script
sed -i.bak '/^R_HOME_DIR=/d' bin/R
sed -i.bak 's;/Library/Frameworks/R.framework/Resources;${R_HOME};g' \
bin/R
chmod +x bin/R
rm -f bin/R.bak
# Remove unneccessary files TODO: What else
rm -r doc tests
rm -r lib/*.dSYM
Happy to help you get this working for your shiny app. You can use this github repo for Electron wrapping R/Shiny... just clone, and replace the app.R (for your other packages you need to install them in the local R folder after cloning and then running R from the command line out of the R-Portable-Mac/bin folder...
Try it with the Hello World app.R that is included first
https://github.com/ColumbusCollaboratory/electron-quick-start
And, then installing your packages in the local R-Portable-Mac folder runtime. Included packages by default...
https://github.com/ColumbusCollaboratory/electron-quick-start/tree/master/R-Portable-Mac/library
Your packages will show up here after install.packages() from the command line using the local R-Mac-Portable runtime.
We have been working on a R Addin for this also...
https://github.com/ColumbusCollaboratory/photon
But, note the add-in is still a work in progress and doesn't work with compiled R packages; still have to go into the local R folder and runtime on the command line and install the packages directly into the local R folder libpath as discussed above.
Give it a try and let us know through Github issues if you have any questions and issues. And, if you've already posted out there, sorry we haven't responded as of yet. Would love to communicate through the photon Add-In for this to get it working with compiling packages (into the libPath)--if you have the time to help. Thanks!

cannot open shared object file: No such file or directory Rglpk

I have installed the R package "Rglpk" manually in following manner as root user on ec2-instance of Redhat 7:
mkdir -p GLPK
wget http://ftp.gnu.org/gnu/glpk/glpk-4.47.tar.gz
tar xvf glpk-4.47.tar.gz
cd glpk-4.47
./configure --prefix=/home/ec2-user/GLPK
make
make install
cd ..
wget https://cran.r-project.org/src/contrib/Rglpk_0.6-3.tar.gz
tar xvf Rglpk_0.6-3.tar.gz
mv glpk-4.47 /home/ec2-user/Rglpk/src/GLPK
export LD_LIBRARY_PATH=/home/ec2-user/GLPK/lib
export LIBRARY_PATH=/home/ec2-user/GLPK/lib
export CPATH=/home/ec2-user/GLPK/include
R CMD INSTALL Rglpk
The issue is when i am calling this library in R cli with root as user,it works fine but when i switch to my user i.e. ec2-user and once call this library in R cli ,it starts giving following error for all users,even for root.
library("Rglpk")
Loading required package: slam
Error: package or namespace load failed for ‘Rglpk’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/usr/lib64/R/library/Rglpk/libs/Rglpk.so':
libglpk.so.0: cannot open shared object file: No such file or directory
The file is still present in that location:
0(ec2-user#resuerdsfdfsfdn02 [~])$ cd /usr/lib64/R/library/Rglpk/libs/
0(ec2-user#resuerdsfdfsfdn02 [/usr/lib64/R/library/Rglpk/libs])$ ls
Rglpk.so*
The problem is that libglpk.so.0 isn't in the path in the RStudio server's R environment, even though it's in the R environment that you call from your terminal session. It's less than ideal, but a solution is to put the following line in your .Rprofile file:
dyn.load("/home/ec2-user/GLPK/lib/libglpk.so.0")
This will load the shared library from the path you've specified, instead of having R infer the path from your environment. If your RStudio-server is being run from a different account than the default ec2-user profile on your instance, just replace 'ec2-user' in the above path with whatever username you're using. Once that's done, you should be able to call 'Rglpk' from your RStudio-server session.
The main issue lies that whenever Rglpk or any other R packages are called,they are not able to find files such as libglpk.so.0 as the environment variables were executed locally which points to location of it till the user cli exists.Thus set the environment variables system-wide so that this library can access irrespective of users:
1)Edit /etc/bashrc
2)Place the following variables in it at last:
export LD_LIBRARY_PATH=/home/ec2-user/GLPK/lib
export LIBRARY_PATH=/home/ec2-user/GLPK/lib
export CPATH=/home/ec2-user/GLPK/include
3)Reload the file:
source /etc/bashrc
From my experience dyn.load loads the package but is not enough for using functions. So I use:
dyn.load("/home/ec2-user/GLPK/lib/libglpk.so.40")
Sys.getenv("LD_LIBRARY_PATH")
Sys.setenv(LD_LIBRARY_PATH=paste0(Sys.getenv("LD_LIBRARY_PATH"), ":", "/home/ec2-user/GLPK/lib"))
For INSTALL I had to use:
export PKG_CFLAGS='-I/home/user/GLPK/include'
export PKG_LIBS='-L/home/user/GLPK/lib'

How to use local R installation on HPC with qsub

I work with a cluster where it is not possible to globally install a specific R version. Given that I built a specific version for R on folder:
<generic_path>/R/R-X.Y.Z
and I installed some packages locally on:
<generic_path/R/packages
how can I set, in a shell script (bash), the environment variables and aliases to run this specific R version, loading the packages from the local package directory?
Option 1:
Using a shell script for HPC (in my case a qsub script), this is possible by running a shell script (e.g. in bash), which contains the following lines:
alias R="<path_to_R>/R/R-X.Y.Z/bin/R"
export R_LIBS="<path_to_R>/R/packages"
export PATH="<path_to_R>/R/R-X.Y.Z/bin:${PATH}"
The script (here I named it makeenv.sh) may be run inside the qsub script with:
source makeenv.sh
Option 2: Depending on your HPC system, you might have module avail, module load commands, if so then use:
myBsubFile.sh
#!/bin/bash
# some #BSUB headers...
# ...
module load /R/R-X.Y.Z
Rscript myRcode.R
Then load libraries in the R script as:
myRcode.R
library("data.table", lib.loc = "path/to/my/libs")
# some more R code...

Unable to find an installation of R on the system; Unable to locate R binary by scanning standard locations

I installed R on redhat using the following commands:
/configure --enable-R-shlib –with-readline=no –with-x=no
make clean
make
make install
I have the R_HOME installed in /local/home/UserX/R-3.2.3. So I added it to the Path (#echo $PATH : /local/home/UserX/R-3.2.3/bin/:/sbin:/bin:/usr/sbin:/usr/bin).
After installing Rstudio Server, when I execute sudo rstudio-server verify-installation. I got :
**Unable to find an installation of R on the system (which R didn't return valid output); Unable to locate R binary by scanning standard locations
rstudio-server start/running, process 13900**
which R : /local/home/UserX/R-3.2.3/bin/R
ls -la /usr/bin/R : ls: cannot access /usr/bin/R: No such file or directory
ls -la /usr/local/bin/R : ls: cannot access /usr/local/bin/R: No such file or directory
ls -la /opt/local/bin/R : ls: cannot access /opt/local/bin/R: No such file or directory
1) R is in the EPEL repo https://fedoraproject.org/wiki/EPEL , R-3.3.0 . ... 18 packages are available ... https://dl.fedoraproject.org/pub/epel/7/x86_64/r/ ... # yum install R-core R-core-devel
2) Or use your R : Add the PATH → export PATH=/local/home/UserX/R-3.2.3/bin:$PATH , .... where $PATH will do for /sbin:/bin:/usr/sbin:/usr/bin .... etc. default.
Note : .bin/:/sbin is a syntax error in your "PATH try".
I noticed that a normal user can't access to the R_Home, because it was installed under a home directory of a sudoer user. I reinstalled R on another directory where any user can access to it and that solved my problem.

Resources