how to know if a macro exists in `execute R`?

jujujuju Member Posts: 39 Guru
edited November 2018 in Help

Hi the content is this:

- need to pass values as macros, since that seems to be the only way for web service

- use `execute R` to parse the macros, since it's more flexible to handle strings and lots of if...then...



First question is how to refer to macro in `execute R`. I did some tests and figured it out myself:

Suppose you set macro m1 to be x, then whenever your write %{m1} in `execute R`, the software will just literally replace %{m1} with x


For example:

rm_main = function()
x = 3
z = %{m1} # interpreted as z = x
print(z) # 3

xy = 3
z = %{m1}y # interpreted as z = xy
print(z) # 3


If want to pass a string 'x', either set macro m1 to be 'x',

or do z = '%{m1}' in `execute R`.



Now the second question is, if some macros are optional, how to test if they exist in `execute R`?

One solution is, fix it at the web service side - Always set macro to be NA if it's not used.

But can we handle omitted macro in `exexute R`? (So that the web service side can pass as many macros as they want)

This doesn't seem to work

exists('%{m1}')  # interpreted as exists('x'), can't test if the macro exists

Use `try`

rm_main = function()
try( z <- '%{m1}' )

when there's no macro m1, it stills throws error (even when using `try`)


How do I test the existance of a macro?

Any help's appreciated. Thank you~




Best Answer

  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,453 RM Data Scientist
    Solution Accepted



    what about defining them in the context with a default value of -1 or something. You can test in R on if (%{myMacro!=-1) or something


    Edit: Another option would be to do the handling in RM using either Branch or Handle Exception.



    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany


  • David_ADavid_A Administrator, Moderator, Employee, RMResearcher, Member Posts: 297 RM Research



    just to add some details:

    The reason why you get an exception in the R script is because macros are always evaluated first and the results are then applied by the corresponding Operator.

    Otherwise Martin's suggesting should work perfectly.




  • jujujuju Member Posts: 39 Guru

    nice trick! Thanks Martin

Sign In or Register to comment.