Hi,
I'd like to know if there is an equivalent for #ifdef (Clangage) in R. I
'd
like to do something like:
useDebug = defmacro(DEBUG, expr=(DEBUG==1))
if(useDebug(0)){
#Here I do not use debug mode
make_addition = function(a, b) {
c=a+b
plot(c)
return(c)
}
}else{
#here I use debug mode
c=a+b
}
I assume this would work... The problem is :
if I have 15 lines of code in if{ } bloc I have to copy and paste these15
lines into else{} bloc. These is too many lines in my file...
Is there something like (in C langage):
#define DEBUG 1
#ifndef DEBUG
make_addition = function(a, b) {
#endif
#ifdef DEBUG
a = 1
b = 2
#endif
c=a+b
plot(c)
#ifndef DEBUG
return(c)
}
#endif
my goal is to run my program WITHOUT calling my_addition(1, 2) (for
debugging). The main problem : all variables do not exist when just running
my _file.R , I have to call my_function if I want to see results.
I use RStudio
Thanks for your help
--
View this message in context:
http://r.789695.n4.nabble.com/is-there-an-equivalent-for-ifdef-C-langage-in-R-tp4289356p4289356.html
Sent from the R help mailing list archive at Nabble.com.
William Dunlap
2012-Jan-12 17:12 UTC
[R] is there an equivalent for #ifdef (C langage) in R
You could put your debug-only code into a function calls like
ifDebug(diagnosticPlot(c))
and
ifDebug({
tmp <- timeConsumingFunction(c)
stopifnot(tmp < 1.0)
})
When you want debug output define ifDebug as
ifDebug <- function(expr) force(expr)
and when you don't want it define it as
ifDebug <- function(expr) NULL
Because of lazy evaluation, the expression won't get evaluated
in the latter case.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf Of ikuzar
> Sent: Thursday, January 12, 2012 7:37 AM
> To: r-help at r-project.org
> Subject: [R] is there an equivalent for #ifdef (C langage) in R
>
> Hi,
> I'd like to know if there is an equivalent for #ifdef (Clangage) in R.
I 'd
> like to do something like:
>
> useDebug = defmacro(DEBUG, expr=(DEBUG==1))
>
> if(useDebug(0)){
> #Here I do not use debug mode
> make_addition = function(a, b) {
> c=a+b
> plot(c)
> return(c)
> }
> }else{
> #here I use debug mode
> c=a+b
> }
>
> I assume this would work... The problem is :
> if I have 15 lines of code in if{ } bloc I have to copy and paste these15
> lines into else{} bloc. These is too many lines in my file...
>
> Is there something like (in C langage):
>
> #define DEBUG 1
>
> #ifndef DEBUG
> make_addition = function(a, b) {
> #endif
> #ifdef DEBUG
> a = 1
> b = 2
> #endif
> c=a+b
> plot(c)
> #ifndef DEBUG
> return(c)
> }
> #endif
>
> my goal is to run my program WITHOUT calling my_addition(1, 2) (for
> debugging). The main problem : all variables do not exist when just
running
> my _file.R , I have to call my_function if I want to see results.
>
> I use RStudio
>
> Thanks for your help
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/is-there-an-equivalent-for-ifdef-C-
> langage-in-R-tp4289356p4289356.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Henrik Bengtsson
2012-Jan-12 23:09 UTC
[R] is there an equivalent for #ifdef (C langage) in R
You could generate an R script using an RSP template 'main.R.rsp'
containing:
<% DEBUG <- TRUE %>
<% if (!DEBUG) { %>
? make_addition = function(a, b) {
<% } %>
<% if (DEBUG) { %>
? a = 1
? b = 2
<% } %>
? c=a+b
? plot(c)
<% if (!DEBUG) { %>
? ? ? ?return(c)
? ?}
<% } %>
which you can compile into 'main.R' using
R.rsp::rsp("main.R.rsp").
You can compile and run it in one go by:
source(R.rsp::rsp("main.R.rsp"))
Changing DEBUG to FALSE generates the alternative main.R file. Note
that the *.R.rsp file is not parsed by R, so it is only the generated
main.R that has to be valid before it is passed to source().
My $.02
/Henrik
On Thu, Jan 12, 2012 at 7:36 AM, ikuzar <razuki at hotmail.fr>
wrote:> Hi,
> I'd like to know if there is an equivalent for #ifdef (Clangage) in R.
I 'd
> like to do something like:
>
> useDebug = defmacro(DEBUG, expr=(DEBUG==1))
>
> if(useDebug(0)){
> ? ?#Here I do not use debug mode
> ? ? make_addition = function(a, b) {
> ? ? ? ?c=a+b
> ? ? ? ?plot(c)
> ? ? ? ?return(c)
> ? ?}
> }else{
> ?#here I use debug mode
> ? ? c=a+b
> }
>
> I assume this would work... The problem is :
> ?if I have 15 lines of code in if{ } bloc I have to copy and paste these15
> lines into else{} bloc. These is too many lines in my file...
>
> Is there something like (in C langage):
>
> #define DEBUG ?1
>
> #ifndef DEBUG
> ? make_addition = function(a, b) {
> #endif
> #ifdef DEBUG
> ? a = 1
> ? b = 2
> #endif
> ? c=a+b
> ? plot(c)
> #ifndef DEBUG
> ? ? ? ?return(c)
> ? ?}
> #endif
>
> my goal is to run my program ?WITHOUT calling my_addition(1, 2) (for
> debugging). ?The main problem : all variables do not exist when just
running
> my _file.R ? , ?I have to call my_function if I want to see results.
>
> I use RStudio
>
> Thanks for your help
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/is-there-an-equivalent-for-ifdef-C-langage-in-R-tp4289356p4289356.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.