May 14, 2019 - Install R and RStudio on Debian 9

Comments

Install R and RStudio on Debian 9

This can be real pain… however after a lot of googling I finally figures out how to do it.

Install lastest R version

In your /etc/apt/sources.list add:

deb http://cran.uni-muenster.de/bin/linux/debian stretch-cran35/

(or any other CRAN mirror)

Then import the GPG key of the mirror (in above case it’s gonna be FCAE2A0E115C3D8A):

# most likely have to install 'dirmngr' first:
apt-get install dirmngr

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys FCAE2A0E115C3D8A

Refresh and install latest R version:

apt-get update
apt-get install r-base r-recommended

Install latest nightly build of RStudio

Why do you need the nightly build? Because they’ve still not managed
to get rid off the very annoying libssl.so.1.0.0 dependency in their release builds!

Get the latest build for Debian9+ from: https://dailies.rstudio.com/

Then install it with:

dpkg -i rstudio-*.deb

Jan 31, 2019 - Neat Java 8 features - Concurrency API

Comments

Neat Java 8 features - Concurrency API

This, the last of three posts is about the Java Concurrency API

Previous posts:

Concurrency API

The Concurrency API has been around for longer, but got some extra features with Java 8.

If you want to run tasks in parallel, wrap them either in a ‘Runnable’ (no return value) or a ‘Callable’ (has a return value). Submit these tasks to an ExecutorService and check the returned ‘Futures’ for completion, respectively get the results from them.

Here’s an example:

ExecutorService es = Executors.newWorkStealingPool();
List<Future<String>> futures = new ArrayList<Future<String>>();
for (int i=0; i<10; i++) {
    Callable<String> task = () -> {
    TimeUnit.SECONDS.sleep(1);
    return "This callable was executed by "
        + Thread.currentThread().getName();
    };
    futures.add(es.submit(task));
}
        
while(!futures.stream().allMatch(f -> f.isDone())) {
    TimeUnit.MILLISECONDS.sleep(100);
}
        
futures.stream().forEach(f ->  { 
    try {
        System.out.println(f.get());
     } catch (Exception e) {
         e.printStackTrace();
     }
});

Here’s another example using a CompletionService. With the CompletionService you don’t have to loop over the Futures and check if they are ready, you can simple take the next one which is ready from the service.

Random random = new Random();
        
CompletionService<String> cs = new ExecutorCompletionService<String>(Executors.newWorkStealingPool());
        
for (int i=0; i<10; i++) {
    Callable<String> task = () -> {
    // wait for 1 to 10 sec
    TimeUnit.SECONDS.sleep(random.nextInt(10)+1);
    return "This callable was executed by "
        + Thread.currentThread().getName();
    };
    cs.submit(task);
}

int completed = 0;
while (completed < 10) {
    // take the next one which is ready
    Future<String> fut = cs.take();
    System.out.println("Another one completed: "+fut.get());
    completed++;
}

Jan 22, 2019 - Virtualbox tips and tricks

Comments

First things to do on a fresh virtualbox guest

This is for a Debian 9 system; might also work on newer versions and Ubuntu.

Installing ‘guest additions’

You need to install the ‘build essentials’ first:

apt-get -y install build-essential module-assistant
m-a prepare

‘Insert’ the guest addtions CD. The I usually copy the contents over into the root directory and run it from there.

mount /media/cdrom0
cp -r /media/cdrom0 /root/
cd /root/cdrom0
./VBoxLinuxAdditions.run

Setup access to the ‘shared’ directory

The user needs to be in the ‘vboxsf’ group:

usermod -a -G vboxsf [USERNAME]

I also usually create a symlink in the user’s home directory for simplicity:

ln -s /media/sf_[SHARE NAME] /home/[USERNAME]/[SHARE NAME]

Here’s everything wrapped up in a script:

#!/bin/bash
if [[ $EUID -ne 0 ]]
then
	echo "This script must run as root user"
	exit 1
fi

echo "Installing stuff needed for virtualbox guest additions installation"

apt-get -y install build-essential module-assistant
m-a prepare

echo "Setting up permissions for shared directory"
echo "Shared directory name: (just hit return to skip this step if you don't have one)"
read dirname

if [[ ! -z "$dirname" ]]
then
	echo "Your username:"
	read username
	
	echo "Adding user to vboxsf group"
	usermod -a -G vboxsf $username
	
	link=/home/$username/$dirname
	if [[ ! -L ${link} ]]
	then
		echo "Creating symlink $link to shared directory"
		ln -s /media/sf_$dirname $link
		chown $username:$username $link
	else
		echo "$link already exists."
fi

echo "Installing vbox guest additions (if CD is inserted)"
if mount /media/cdrom0
then
	cp -r /media/cdrom0 /root/
	cd /root/cdrom0
	./VBoxLinuxAdditions.run
	cd ..
	rm -rf /root/cdrom0
	echo "Finished. Hit return to reboot."
	read xxx
	shutdown -r now
else
	echo "vbox guest additions CD not found."
	echo "Finished."