Martin Martin - 6 months ago 35
R Question

Conditional execution of code segments

I am working on a data set using both my laptop and a cloud facility. I want to compute some "computation-heavy" code chunks only when working on the cloud.

So far, I have chosen a not very elegant way to do so. I have added

prefixes to segments which I want to execute only when in the cloud. I then simply remove these prefixes and run the script when in the cloud.

Now my question: is there a way that I can select in the beginning of the script once whether to execute these segments or not and then skip these segments if the argument is set to "false"? I have tried with if conditions but this is very cumbersome.

Answer Source

To further explain the comment of docendo discimus, just define a parameter at the beginning of your script:

execpart <- TRUE #and change to FALSE if you don't want to execute

Then wrap the whole part of your script which should only be executed situationally in:

## your script

You could even define multiple parameters for different parts of your script at the beginning. That would give you the option to set up the execution of your script with a few quick changes.

Note that if looks for TRUE/FALSE, so you do not need to specify (execpart == TRUE) in your if-condition.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download