Try loading an uninstalled library from a Jupyter R 4.0.1 notebook

In [1]:
library("cowsay")
Error in library("cowsay"): there is no package called ‘cowsay’
Traceback:

1. library("cowsay")

It is worth trying first because a lot of things are pre-installed:

In [10]:
# library() # Not run here for space

If you try installing something from within the R notebook on Jupyter, it won't work:

In [3]:
install.packages("cowsay")
Installing package into ‘/scale_wlg_persistent/filesets/opt_nesi/CS400_centos7_bdw/IRkernel/1.1.1-gimkl-2020a-R-4.0.1’
(as ‘lib’ is unspecified)

Warning message in install.packages("cowsay"):
“'lib = "/scale_wlg_persistent/filesets/opt_nesi/CS400_centos7_bdw/IRkernel/1.1.1-gimkl-2020a-R-4.0.1"' is not writable”
Error in install.packages("cowsay"): unable to install packages
Traceback:

1. install.packages("cowsay")
2. stop("unable to install packages")

This is the trap! The solution is not too complicated but there is no other easy work around.

You actually need to:

  1. Go in the terminal (can be done from the jupyter notebook)
  2. Find and load the version of R used on jupyter notebook
  3. Install your package
module spider R # find the version
module load R/4.0.1-gimkl-2020a #load the version

Open R by running R in the terminal and you should now be able to install and load the package (after choosing a CRAN session)

R
install.packages("cowsay")
library("cowsay")

If you now get back into the notebook or the console, you can access this package.

In [4]:
library("cowsay")
In [5]:
say("Moo")
 -------------- 
Moo 
 --------------
    \
      \
        \
            |\___/|
          ==) ^Y^ (==
            \  ^  /
             )=*=(
            /     \
            |     |
           /| | | |\
           \| | |_|/\
      jgs  //_// ___/
               \_)
  

Very good, but I was led to believe it would be a cow:

In [6]:
say("Moo",by="cow")
 ----- 
Moo 
 ------ 
    \   ^__^ 
     \  (oo)\ ________ 
        (__)\         )\ /\ 
             ||------w|
             ||      ||
In [7]:
say("PuRRRRRRing",by="bigcat")
 ----- 
PuRRRRRRing 
 ------ 
    \   
     \
                \`*-.
                 )  _`-.
                .  : `. .
                : _   '  \
                ; *` _.   `*-._
                `-.-'          `-.
                  ;       `       `.
                  :.       .       \
                  .\  .   :   .-'   .
                  '  `+.;  ;  '      :
                  :  '  |    ;       ;-.
                  ; '   : :`-:     _.`* ;
               .*' /  .*' ; .*`- +'  `*'
     [bug]     `*-*   `*-*  `*-*'
    
In [8]:
say("Oh hello there",by="random",by_color="pink")

 ----- 
Oh hello there 
 ------ 
    \   
     \  
      \
                ________
           __--´      ° `--__
       __-´     °      °     `-__
     (´    °    °          °     `)
     (° °|    |°         ° |    | )
      `'''''''''`|'''''|´''''''''´
                 |     |
                 |:::::|
               /:|:::::|:\
              /::|:::::|::\
                 |     |
                 |^   ^|
                 |  _  | [FK]
                 |_____|
In [9]:
say("Oh hello there",by="random",by_color="pink")

 ----- 
Oh hello there 
 ------ 
    \   
     \
     _[_]_
      (")
  >--( : )--<
    (__:__) [nosig]
  

Okay, we are getting distracted here.

Be aware that a package installed for one version of R would need to be re-installed for a different R version. If you remember having installed a package but R cannot find it, you are likely to be using the wrong R version.

R jobs

A job file cannot be an Rscript. You will prepare a bash job file that run an Rscript.

Using nano or the jupyter text editor we can create a basic R script making a simple plot. Let's call it testscript.R and fill it with:

png(filename="plot.png")  # This line redirects plots from screen to plot.png file.

# Define the cars vector with 5 values
cars <- c(1, 3, 6, 4, 9)

# Graph the cars vector with all defaults
plot(cars)

Now let's make a bash file that can run it run_testscript.sh

#!/bin/bash -e
#SBATCH --account uoo00116 # CHANGE
#SBATCH --job-name    runtestscriptR
#SBATCH --time        00:01:00
#SBATCH --mem         512MB
#SBATCH --qos debug
module load R/4.0.1-gimkl-2020a

# Help R to flush errors and show overall job progress by printing
# "executing" and "finished" statements.
echo "Executing R ..."
srun Rscript testscript.R
echo "R finished."

You can now submit that run_testscript.sh file that will run the testscript.R.

sbatch run_testscript.sh
squeue -u $USER #will show you th queue of jobs, if it is emopty, it ran or bugged

You should have a plot.png file in your directory.

In summary, I hope you'll remember the cowsay R package and maybe other things!