Conda Install R packages Tcl Error – solved

When I try to install R packages in conda, sometimes I get the following error:

install.packages("vegan")
. . .
Error: .onLoad failed in loadNamespace() for 'tcltk', details:
  call: fun(libname, pkgname)
  error: Can't find a usable init.tcl in the following directories: 
    /opt/anaconda1anaconda2anaconda3/lib/tcl8.5 ./lib/tcl8.5 ./lib/tcl8.5 ./library ./library ./tcl8.5.18/library ./tcl8.5.18/library

This is happening because install.packages is trying to paint the repository picker window – I don’t understand why it’s not using the command-line repository picker.

My workaround was to set the working directory to

~/miniconda2

so that

./lib/tcl8.5/init.tcl

was on my path. Then it painted the repository-picker just fine, and I was able to install my update.

An alternative workaround might have been to ssh to the same box (without X forwarding).

bash get directory for script

First, some warnings from the BashFAQ:

  • Your script does not actually have a location! Wherever the bytes end up coming from, there is no “one canonical path” for it. Never.

  • $0 is NOT the answer to your problem. If you think it is, you can either stop reading and write more bugs, or you can accept this and read on.

The BashFAQ also describes BASH_SOURCE and the applicable caveats.  Here’s some workable, albeit fallible, code from http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in:

# find directory where script may reside
SOURCE="${BASH_SOURCE[0]}"
# resolve $SOURCE till not a symlink
while [ -h "$SOURCE" ]; do 
  DIR="$( cd -P "$(dirname "$SOURCE")" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd && \
  echo x)"
DIR="${DIR%x}"
echo Script $0 resides in directory $DIR

docker subverts ufw

The Problem

I found on Ubuntu that Docker modifies iptables such that ufw cannot effectively control incoming connections, as documented here:

http://blog.viktorpetersson.com/post/101707677489/the-dangers-of-ufw-docker

A Workaround

This link also points out that it is possible to override this behavior by adding the --iptables=false option to the command line that starts the Docker daemon. For a systemd example, this option is appended to the ExecStart line in
/lib/systemd/system/docker.service:

ExecStart=/usr/bin/dockerd -H fd:// –iptables=false

A Side-Effect of the Workaround

On Debian, with dockerd lauched with the --iptables=false option, I tried to do “docker build” for a DockerFile that included:

RUN npm install -g ethercalc pm2

But, this failed with:

npm info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443

So, I had to

  • restart dockerd without the --iptables=false option
  • do the docker build
  • restart dockerd with the --iptables=false option

I would like to find a more elegant solution!!

Update 1:

On a Debian without any bridges, I don’t notice a difference in the output of ‘iptables -L’ with or without the option set.  So, I’m a bit stumped. Could this issue only affect Ubuntu? Could it only affect machines that have bridges?

Update 2:

I tried this again with my Ubuntu machine that does indeed have bridges, and I found that ‘iptables -L’ output is unaffected, yet, when I started dockerd without the --iptables=false option, my machine accepted connections even when ufw was set to reject all incoming connections.  So, I’m still stumped.

Percent complete R function

R function to print percent complete.

If you use
method = message
you may want to use
suffix = ” % complete”

pctComplete <- function(
  progress = 0, last.progress = 0,
  total = 100, increment = 10,
  suffix = " ", method = cat, ...) 
{
  if (increment < 1)
    increment <- 1
  if (total < 0)
    total <- 100
  pct <- round(progress * 100 / total)
  if ( pct >= increment 
               + last.progress
               - last.progress%% increment )
  {
    method(paste("", pct, suffix, sep = ""),
           ...)
  }
  pct
}

 

aggregate free memory and swap

free -b reveals the total, used, free, shared, cache, and available memory.

Here’s the awk code to extract the total of the free physical memory and the free swap.

First, observe the free memory and swap, in bytes

# free -b | \
awk 'BEGIN{s=0}; NR<2{print $3}; NR>1{print $4}'
free
293203968
21441761280

Now, total the free memory and swap

# free -b | \
awk 'BEGIN{s=0};NR>1{n=$4;s=s+n;};END{print s}'
21741248512

The sum is slightly different because the second command was run at a different time.

Why I chose MDI during R installation

When installing R on a machine that is shared among several users, some of which are accustomed to the Mac OS, I had to choose between the Multiple Document Interface (MDI) and the Single Document Interface (SDI). The MDI article on Wikipedia pointed out that the MDI interface relates all documents to a common menu bar, which is what Mac users are accustomed to; Windows users probably have encountered both, since Office applications have used at times (depending on the generation) SDI and MDI. Therefore, to make the experience less alien for Mac users on a Windows machine, I installed R configured to use MDI.

Ubuntu temperature monitor

Background

I am trying to figure out how to monitor temperature on my Ubuntu system, and I found this thread:
https://ubuntuforums.org/showthread.php?t=2178610
So I started installing the packages recommended in response #5, but response #4 may be good, too.

Synopsis
$ sudo apt-get install lm-sensors hddtemp
$ sudo sensors-detect
$ sudo /etc/init.d/kmod start
[ ok ] Starting kmod (via systemctl): kmod.service.
$ sensors
acpitz-virtual-0
Adapter: Virtual device
temp1:        +54.0°C  (crit = +120.0°C)

coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +53.0°C  (high = +100.0°C, crit = +100.0°C)
Core 0:         +52.0°C  (high = +100.0°C, crit = +100.0°C)
Core 1:         +51.0°C  (high = +100.0°C, crit = +100.0°C)

sed script for key:value to csv

I had files with one key-value pair per line, e.g.:

Acquired Name: foo
Acquired Date: 1066-10-14T10:00:00Z
etc.

I wanted to extract all of the fields to one CSV line per file.

This is the sed script that I used. As it happens, this would also work if all of the input files were concatenated (because I used the ‘d’ command rather than ‘q’).

/Acquired Name:/ { 
  s/.*Acquired Name: \(.*\)./"\1"/ ; h ; }
/Acquired Date:/ {
  s/.*Acquired Date: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; }
/Acquired Time:/ {
  s/.*Acquired Time: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Job Code:/ {
  s/.*Job Code: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Instrument:/ {
  s/.*Instrument: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Bottle Number:/ {
  s/.*Bottle Number: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Inlet Method:/ {
  s/.*Inlet Method: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/MS Method:/ {
  s/.*MS Method: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Tune Method:/ {
  s/.*Tune Method: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; p ; d ; }

The manual for sed
<https://www.gnu.org/software/sed/manual/sed.html>
explains the commands used:

  • d – Delete the pattern space; immediately start next cycle with next input line.
  • h – Copy the pattern space to the hold space
  • G – Append the hold space to the pattern space
  • p – Print the pattern space
  • s/pattern/replacement/ – Substitute pattern with replacement
  • s/pattern/replacement/M – Do multi-line substitution; see this
  • x – Exchange the pattern space and the hold space

 

colordiff

From http://linux.die.net/man/1/colordiff

colordiff is a wrapper for diff and produces the same output as diff but with coloured syntax highlighting at the command line to improve readability.

The output is similar to how a diff-generated patch might appear in Vim or Emacs with the appropriate syntax highlighting options enabled.

The colour schemes can be read from a central configuration file or from a local user ~/.colordiffrc file.

To enjoy on Ubuntu:

sudo apt install colordiff

Check these if you cannot ssh

In addition to checking that /etc/ssh/sshd_config is correct, you will want to check that the permissions on the user’s home directory and ~/.ssh are correct. I have gathered that:

  • The home directory must only be owned by AND only be writable by the login user.
  • The .ssh directory must only be accessible by the user.
  • The authorized_keys file must be only writable by the user.
[foo@foo-nas ~/.ssh]$ ls -la -D ""
total 14
drwx------  2 foo  foogrp    3  .
drwxr-x---  5 foo  foogrp   15  ..
-rw-r--r--  1 foo  foogrp  395  authorized_keys