The main point in this example is the string manipulation we use to cre-
ate the filenamefname. For more details about the graphics operations used
in this example, refer to Section 12.3.
Thepaste()function concatenates the string"q"with the string form of
the numberi. For example, wheni=2,thevariablefnamewill beq 2 .pdf.
However, that isn’t quite what we want. On Linux systems, filenames with
embedded spaces create headaches, so we want to remove the spaces. One
solution is to use thesepargument, specifying an empty string for the separa-
tor, as follows:
1 for (i in 1:5) {
2 fname <- paste("q",i,".pdf",sep="")
3 pdf(fname)
4 hist(rnorm(100,sd=i))
5 dev.off()
6 }
Another approach is to employ thesprintf()function, borrowed from C:
1 for (i in 1:5) {
2 fname <- sprintf("q%d.pdf",i)
3 pdf(fname)
4 hist(rnorm(100,sd=i))
5 dev.off()
6 }
For floating-point quantities, note also the difference between%fand%g
formats:
> sprintf("abc%fdef",1.5)
[1] "abc1.500000def"
> sprintf("abc%gdef",1.5)
[1] "abc1.5def"
The%gformat eliminated the superfluous zeros.
11.3 Use of String Utilities in the edtdbg Debugging Tool............................
The internal code of theedtdbgdebugging tool, which will be discussed in
Section 13.4, makes heavy use of string utilities. A typical example of such
usage is thedgbsendeditcmd()function:
# send command to editor
dbgsendeditcmd <- function(cmd) {
syscmd <- paste("vim --remote-send ",cmd," --servername ",vimserver,sep="")
system(syscmd)
}
String Manipulation 257