R plotting has issues with producing text/number labels - r

I am having issues with my general R plotting functions. From a fresh R restart, and without loading any packages, I run this:
png(file ="mtcars.png", width = 1600, height = 1600, units = "px", res = 300)
hist(mtcars$mpg, breaks = 'FD', col = 'blue', xlab = 'MPG', main = 'MPG of mtcars Dataset')
dev.off()
And the result is what you see in this post. There's some weird squares where the labels should be. I am running R in a new docker image and have no idea what I forgot to install. Somebody help!
My dockerfile contains:
RUN conda install -y r-base && conda install -c bioconda -y r-devtools
RUN \
/opt/conda/lib/R/bin/R -e "getOption(\"unzip\") ; options(repos = list(CRAN=\"http://cran.rstudio.com/\")) ; Sys.getenv(\"TAR\") ; options(unzip = \"/opt/conda/bin/unzip\") ; \
Sys.setenv(TAR = \"/bin/tar\"); library(devtools) ; install.packages(c('Seurat','XML','ggplot2')) ; \
devtools::install_github(repo = 'satijalab/seurat-wrappers',quiet=T) ; \
if (!requireNamespace(\"BiocManager\", quietly = TRUE)) { \
install.packages(c(\"BiocManager\"), quiet = T) \
} ; \
BiocManager::install('monocle') ; devtools::install_github('hms-dbmi/conos')"

Related

R.matlab cannot set matlab function

I was trying to reproduce the setfunction example in R.matlab. My code is
library(R.matlab)
Matlab$startServer()
matlab <- Matlab()
isOpen <- open(matlab)
setFunction(matlab, " \
function [win, aver] = dice(B) \
%Play the dice game B times \
gains = [-1, 2, -3, 4, -5, 6]; \
plays = unidrnd(6, B, 1); \
win = sum(gains(plays)); \
aver = win/B; \
")
I encountered the error:
I also tried adding an end at the end of the matlab function, but still had the same error.
How can I avoid the error and set the matlab function within R?

How to get an R script to run in parallel on a mosix cluster?

I am trying to re-create the example given in part 3 of this paper, which performs a simple calculation across several instances managed by a cluster. The main calculation happens in this script, "sim.R":
# sim.R
# If the "batch" package has not been installed, run the line below:
# install.packages("batch", repos = "http://cran.cnr.Berkeley.edu")
seed <- 1000
n <- 50
nsim <- 10000
mu <- c(0, 0.5)
sd <- c(1, 1)
library("batch")
parseCommandArgs()
set.seed(seed)
pvalue <- rep(0,nsim)
for(i in 1:nsim) {
X <- rnorm(n = n, mean = mu[1], sd = sd[1])
Y <- rnorm(n = n, mean = mu[2], sd = sd[2])
pvalue[i] <- t.test(X, Y)$p.value
}
power <- mean(pvalue <= 0.05)
out <- data.frame(seed = seed, nsim = nsim, n = n,
mu = paste(mu, collapse = ","),
sd = paste(sd, collapse = ","), power = power)
outfilename <- paste("res", seed, ".csv", sep = "")
print(out)
write.csv(out, outfilename, row.names = FALSE)
To run multiple, parallel instances of sim.R, there is another script "param-sim.R"
library("batch")
seed <- 1000
for(i in 1:10) {
seed <- rbatch("sim.R", seed = seed, n = 25, mu = c(0, i / 10))
rbatch.local.run() # My understanding from the linked paper is that this line will do nothing if the script is run on a mosix cluster and not locally.
}
To run this on a mosix cluster, I use the following command from the terminal:
R --vanilla --args RBATCH mosix < param-sim.R
I would expect this output to generate 10 .csv files, labeled res1000.csv - res1009.csv. Instead, here's what I get (I am running this command in an Ubuntu environment):
$ R --vanilla --args RBATCH mosix < param-sim.R
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library("batch")
> seed <- 1000
> for(i in 1:10) {
+ seed <- rbatch("sim.R", seed = seed, n = 25, mu = c(0, i / 10))
+ rbatch.local.run()
+ }
nohup mosrun -e -b -q R --vanilla --args seed 1000 n 25 mu "c(0,0.1)" < sim.R > sim.Rout1000 &
rbatch.local.run: no commands have been batched.
nohup: redirecting stderr to stdout
nohup mosrun -e -b -q R --vanilla --args seed 1001 n 25 mu "c(0,0.2)" < sim.R > sim.Rout1001 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1002 n 25 mu "c(0,0.3)" < sim.R > sim.Rout1002 &
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1003 n 25 mu "c(0,0.4)" < sim.R > sim.Rout1003 & nohup:
redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1004 n 25 mu "c(0,0.5)" < sim.R > sim.Rout1004 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1005 n 25 mu "c(0,0.6)" < sim.R > sim.Rout1005 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1006 n 25 mu "c(0,0.7)" < sim.R > sim.Rout1006 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1007 n 25 mu "c(0,0.8)" < sim.R > sim.Rout1007 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1008 n 25 mu "c(0,0.9)" < sim.R > sim.Rout1008 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
nohup mosrun -e -b -q R --vanilla --args seed 1009 n 25 mu "c(0,1)" < sim.R > sim.Rout1009 &
nohup: redirecting stderr to stdout
rbatch.local.run: no commands have been batched.
>
nohup: redirecting stderr to stdout
No .csv files are generated, and each of the output files (i.e. sim.Rout1000) contains identical information:
mosrun - MOSIX Version 4.3.4
Usage: mosrun [location-options] [program-options] {program} [args]...
mosrun -S{maxjobs} [location-options] [program-options]
{commands-file}[,{failed-file}]
mosrun -R{filename} [-O{fd=filename}][,{fd2=fn2}]... [location-options]
mosrun -I{filename}
Location options - Node specification:
-b try to start on 'best' available node
-r{hostname} start on given host
-{a.b.c.d} start on the node of given IP address
-{n} start on given logical node number
-h start on home node
Other location options:
-F do not fail if requested node is not available
-L lock, disallow automatic migration
-l unlock, allowing automatic migration
-g disallow automatic freezing
-G allow automatic freezing
-m{mb} try to run only on nodes with >= mb free memory
-A {minutes} auto checkpoint interval in minutes (0-10000000)
-N {max} max. # of checkpoints before cycle (0-10000000)
Program options:
-e unsupported system calls produce -1/errno=ENOSYS
-w as -e, but print warnings for unsupported calls
-u unsupported system calls kill mosrun (default)
-d {0-10000} specify decay rate per second in parts of 10000
-c consider program as a pure CPU job (ignore I/O)
-n reverse '-c', so to include I/O considerations
-C{filename} test given checkpoint file
-X{/directory} declare private directory
-z program arguments start at argument #0 (not #1)
Which leads me to think that the program never ran or entered a cluster queue. I have also checked the system processes with the "top" command, and uncovered nothing. For the record, I have been able to successfully run simple C++ programs on a mosix cluster.
Have I missed a key detail to allow this program to work?

Compiling plplot with gfortran

Gfortran compilation fails with plplot graphics library.
FYI: Plplot is a graphics library with which one can plot directly from gfortran (among other languages).
I have installed the following packages (on Xubuntu 18.04)
sudo apt install gfortran libplplot15 libplplot-dev libplplotfortran0 plplot-driver-cairo plplot-driver-qt plplot-driver-wxwidgets plplot-driver-xwin plplot-doc
I updated the local database with the following command: sudo updatedb. When I ran the command locate plplot I get the following relevant lines (along with other lines)
/usr/lib/x86_64-linux-gnu/pkgconfig/plplot-fortran.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/plplot.pc
Then I tried to compile the fortran example code given here (relevant part is given below)
program x00f
use plfortrandemolib
integer, parameter :: NSIZE = 101
real(kind=pl_test_flt), dimension(NSIZE) :: x, y
real(kind=pl_test_flt) :: xmin = 0._pl_test_flt, xmax = 1._pl_test_flt, ymin = 0._pl_test_flt, ymax = 100._pl_test_flt
! integer :: i
integer :: plparseopts_rc
! Prepare data to be plotted.
x = arange(NSIZE) / real(NSIZE-1,pl_test_flt)
y = ymax * x**2
! Or alternatively, using a DO-loop
!do i = 1,NSIZE
! x(i) = real( i - 1, pl_test_flt ) / real( NSIZE - 1, pl_test_flt )
! y(i) = ymax * x(i)**2
!enddo
! Parse and process command line arguments
plparseopts_rc = plparseopts( PL_PARSE_FULL )
if(plparseopts_rc .ne. 0) stop "plparseopts error"
! Initialize plplot
call plinit
! Create a labelled box to hold the plot.
call plenv( xmin, xmax, ymin, ymax, 0, 0 )
call pllab( "x", "y=100 x#u2#d", "Simple PLplot demo of a 2D line plot" )
! Plot the data that was prepared above.
call plline( x, y )
! Close PLplot library
call plend
end program x00f
with the following command
gfortran x00f.f90 $(pkg-config --cflags --libs plplot-fortran)
The output of pkg-config --cflags --libs plplot-fortran is
-I/usr/include/plplot -I/usr/lib/x86_64-linux-gnu/fortran/modules/plplot -I/usr/include/plplot -lplplotfortran
The error that I get is the following:
/tmp/ccAQ0C7A.o: In function `MAIN__':
x00f.f90:(.text+0x65): undefined reference to `__plfortrandemolib_MOD_arange_1'
collect2: error: ld returned 1 exit status
Do I need to install any other packages or is the compilation command is incomplete? Any help will be appreciated.
Answering my own question for future SO users.
The correct compilation command for the above code is
gfortran x00f.f90 -lplfortrandemolib $(pkg-config --cflags --libs plplot-fortran)
Also check VladimirF's comment on the same.

R supress console output of a system or shell command

I have this windows-batchfile which I'm calling from R using the shell() command. This batchfile does some calculations and writes them on the disk but also on the screen. I'm interested in the disk-output, only. I cannot change the batchfile.
The batchfile might be something silly like:
#echo off
echo 1 + 2
#echo 1 + 2 > C:\TEMP\batchoutput.txt
exit
I tried
shell("batchfile.bat", invisible = TRUE)
1 + 2
shell("batchfile.bat", show.output.on.console = FALSE)
Error in system(cmd, intern = intern, wait = wait | intern, show.output.on.console = wait, :
formal argument "show.output.on.console" matched by multiple actual arguments
system("batchfile.bat", invisible = T)
1 + 2
system("batchfile.bat", show.output.on.console = F)
Warning message:
running command 'C:\TEMP\batchfile.bat' had status 1
Is there a way of supressing the console-output on R?
options(warn = -1)
shell("You command")
options(warn = 0)

How to evaluate this scheme in RWeka?

The scheme I am trying to evaluate is:
weka.classifiers.meta.AttributeSelectedClassifier -E "weka.attributeSelection.CfsSubsetEval " -S "weka.attributeSelection.BestFirst -D 1 -N 5" -W weka.classifiers.functions.SMOreg -- -C 1.0 -N 0 -I "weka.classifiers.functions.supportVector.RegSMOImproved -L 0.0010 -W 1 -P 1.0E-12 -T 0.0010 -V" -K "weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0"
i.e. I am trying to run an AttributeSelectedClassifier with an SMOreg classifier inside. Every other parameter is the default value of the respective classifier.
So the R code is:
optns <- Weka_control(W = "weka.classifiers.functions.SMOreg")
ASC <- make_Weka_classifier("weka/classifiers/meta/AttributeSelectedClassifier")
model <- ASC(class ~ ., data = as.data.frame(dat), control = optns)
evaluation <- evaluate_Weka_classifier(model, numFolds = 10)
evaluation
When I run the above R code I get this error:
Error in .jcall(evaluation, "D", x, ...) : java.lang.NullPointerException
The above error happens in RWeka's evaluate.R where it tries to call the WEKA methods: "pctCorrect", "pctIncorrect", "pctUnclassified", "kappa", "meanAbsoluteError","rootMeanSquaredError","relativeAbsoluteError","rootRelativeSquaredError"
I also tried manually specifying the default values using the Weka_control object like so:
optns <- Weka_control(E = "weka.attributeSelection.CfsSubsetEval ",
S = list("weka.attributeSelection.BestFirst", D = 1,N = 5),
W = list("weka.classifiers.functions.SMOreg", "--",
C=1.0, N=0,
I = list("weka.classifiers.functions.supportVector.RegSMOImproved",
L = 0.0010, W=1,P=1.0E-12,T=0.0010,V=TRUE),
K = list("weka.classifiers.functions.supportVector.PolyKernel",
C=250007, E=1.0)))
ASC <- make_Weka_classifier("weka/classifiers/meta/AttributeSelectedClassifier")
model <- ASC(class ~ ., data = as.data.frame(dat), control = optns)
evaluation <- evaluate_Weka_classifier(model, numFolds = 10)
evaluation
and I get this error:
Error in .jcall(classifier, "V", "buildClassifier", instances) :
java.lang.Exception: Can't find class called: weka.classifiers.functions.SMOreg -- -C 1 -N 0 -I weka.classifiers.functions.supportVector.RegSMOImproved -L 0.001 -W 1 -P 1e-12 -T 0.001 -V -K weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1
I tried your example but got a different error (where dat is my own data frame)
Error in model.frame.default(formula = class ~ ., data = dat) :
object is not a matrix
Your error may be not directly related to syntax of calling this Weka function but some issues with path setup.

Resources