Handy Apply-based R Progress Bars

In a funny congruence with my post on R progress bars, Mark Heckmann just posted about some wrappers for the apply functions in R. He wrote up some functions that imitate sapply, lapply and apply but automatically add a progress bar so you can monitor the progress. They work very nice since you can just substitute his apply_pb’s in place of R’s standard apply’s. He says it’s a bit of a performance drag but after testing a bit it looks like they really shouldn’t add much overhead at all if there’s any major calculations inside the loop. Very handy.

Programmer
R

Comments (0)

Permalink

Progress Bars in R

Recently, I've had a lot of time consuming tasks running in R where it's nice to know how the computer is doing. I usually just output the name of the current iteration or a dot or something but I finally decided I should figure out how to make a nice progress bar in R. It turns out it's really simple since it's already builtin with the txtProgressBar function. So you can do something like:

R:
  1. numberSteps<-10
  2. pb <- txtProgressBar(min = 0, max = numberSteps, style = 3)
  3. for(i in 1:numberSteps){
  4.   setTxtProgressBar(pb, i)
  5.   Sys.sleep(1)
  6. }
  7. close(pb)

A text progress bar in R

That's good enough for me but there's also winProgressBar for a fancy Windows progress bar and tkProgressBar (in the tcltk package) if you really want to get fancy.

Programmer
R

Comments (1)

Permalink

Syntax highlighting in terminal

Syntax highlighted cat

I was just quickly flipping through code on a terminal and got to thinking that it would be pretty handy to be able to syntax highlight when using head or grep without having to open an editor. Luckily, I remembered Pygemtize was pretty handy when syntax highlighting in LaTeX so I wondered if they had an equivalent output for terminal. It turns out they do (in standard or 256 color no less) so in a couple minutes I had a really short script for highlighting code at the terminal saved it to bin/ccat and was ready to go. Pygmentize really is impressive. Here's the script if anyone else is looking to do the same:

BASH:
  1. #!/bin/bash#!/bin/bash
  2. if [ ! -t 0 ];then
  3.         file=/dev/stdin
  4. elif [ -f $1 ];then
  5.         file=$1
  6. else
  7.         echo "Usage: $0 code.c"
  8.         echo "or e.g. head code.c|$0"
  9.         exit 1
  10. fi
  11. pygmentize -f terminal -g $file

It just looks for stdin and if it doesn't find it it looks for a file name or takes direct input. Obviously it requires Pygmentize (which is really easy to install if you already have Python).

Bash/UNIX
Programmer

Comments (3)

Permalink

Happy (late) October 1st

China just had their national day (called GuoQinJie I'm told) on October 1st. It was their 60th anniversary so there was a pretty big parade. Which is actually a bit frustrating since I was in Tienanmen Square last October 1st and there was nothing but a bunch of tourists milling around (the rest of the trip was great anyway so no big deal). Apparently if you're ever planning on visiting Beijing on National Day, you need to make sure it's in a year ending in 9 or 4. Anyway, my wife came across this pretty cool time lapse video of the parade (the video page has it in HD):

Tourist

Comments (0)

Permalink

Tab Indented Standard Input Redirect in Bash

I managed to forget how to redirect standard input (when you want to feed a bunch of lines to a program) in a bash script while still indenting and had to go digging around for it. So I figured I'd make a note here so I don't forget again and for anyone else in the same boat. It's just <<- instead of <<. For example if you want to keep indentation within a loop:

BASH:
  1. for i in 1 2 3 4;do
  2.     cat<<-EOF
  3.         This is loop $i
  4.         More advanced stuff could go here
  5.     EOF
  6. done

You can use whatever you want to indicate the end of the input instead of EOF if it floats your boat (as long as you use the same thing both times) but unfortunately <<- doesn't work with spaces for indentation (although I'm a tab man myself).

Bash/UNIX
Programmer

Comments (0)

Permalink