Reputation: 4926
I have a very long script R that plots very complicated data. I only use the plots to have a visual idea of what I am doing but I can compute the results without the plots and obviously not plotting anything makes things much faster. Occasionally, however, I still need to visualize what the program does to keep debugging it.
To achieve this plotting 'on or off' switch I am following this strategy.
For each line that has commands relevant to the plotting functions of the script, I have a specific commented tag #toplot
at the end of each relevant line. Using the power of regex substitution I then comment / uncomment these lines with the following commands.
The sample code:
a <- c(1:10)
b <- a/sin(a)
png('sin.png') #toplot
plot(b) #toplot
dev.off() #toplot
print(b)
To comment the 'tagged' lines:
:%s/.\+#toplot/###commline###\0/g
I get this:
a <- c(1:10)
b <- a/sin(a)
###commline### png('sin.png') #toplot
###commline### plot(b) #toplot
###commline### dev.off() #toplot
print(b)
To uncomment them:
:%s/###commline###//g
I get this:
a <- c(1:10)
b <- a/sin(a)
png('sin.png') #toplot
plot(b) #toplot
dev.off() #toplot
print(b)
I am no computer scientist so I don't know if there is a better, more elegant way of performing these kind of operations.
EDIT: It is important to mention that for plotting my data I need to go through many rounds of calculations and transformations so the different kinds of data fit in the plotting device. To perform these operations I use the history, I go up and down depending what I need.
Upvotes: 1
Views: 497
Reputation: 172550
Your approach looks fine to me.
If you can come up with a regular expression that captures all plot-related lines, you could do away with the #toplot
marker, and let the comment substitution directly work on that instead.
You didn't mention whether you re-type the substitutions or use the history. I would definitely define a buffer-local command (and/or mapping) for that:
autocmd FileType r command! -buffer Comment %s/.\+#toplot/###commline###\0/g
autocmd FileType r command! -buffer Uncomment %s/###commline###//g
(Or put the :commands!
into ~/.vim/ftplugin/r_commands.vim
.)
If you properly define the 'comments'
setting for your filetype (e.g. add b:###commline###
) and 'commentstring'
, you may also be able to use one of the general comment plugins (like The NERD Commenter), which offer nice mappings to toggle a comment on/off.
Upvotes: 1
Reputation: 13510
This is ok, but isn't it easier to wrap each plotting command with a condition?
Upvotes: 0