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