Programmer

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 Pygmentize 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 (5)

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 (2)

Permalink

Displaying Code in LaTeX

gioby of Bioinfo Blog! (an interesting read by the way) left a comment asking about displaying code in LaTeX documents. I've sort of been cludging around using \hspace's and \textcolor but I've always meant to figure out the right way to do things so this seemed like a good chance to figure out how to do it right.

LaTeX tends to ignore white space. This is good when you're writing papers but not so good when you're trying to show code where white space is an essential part (e.g. Python). Luckily there's a builtin verbatim environment in LaTeX that is equivalent to html's <pre>. So something like the following should preserve white space.

Code in LaTeX using verbatim
\begin{verbatim}
for i in range(1, 5):
  print i
else:
  print "The for loop is over"
\end{verbatim}

Unfortunately, you can't use any normal LaTeX commands inside verbatim (since they're displayed verbatim). But luckily there a handy package called fancyvrb that fixes this (the color package is also useful for adding colors). For example, if you wanted to highlight "for" in the above code, you can use the Verbatim (note the capital V) environment from fancyvrb:

Code in LaTeX using fancyvrb
\newcommand\codeHighlight[1]{\textcolor[rgb]{1,0,0}{\textbf{#1}}}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{for} i in range(1, 5):
  print i
else:
  print "The for loop is over"
\end{Verbatim}
Code in LaTeX using pygmentize

If you really want to get fancy, the Pygments package in Python will output syntax highlighted latex code with a command like: pygmentize -f latex -O full test.py >py.tex The LaTeX it outputs is a bit hard to read but it's not too bad (it helped me figure out the fancyvrb package) and it does make nice syntax highlighted output.

Here's an example LaTeX file with the three examples above and the pdf it generates if you're curious.

LaTeX
Programmer

Comments (8)

Permalink