2 Replies Latest reply on Nov 21, 2017 11:45 PM by Paul Denton

    Is it possible to use parameters within R scripts in calculated fileds?

    Kirk Wythers

      I am trying to replace hard coded arguments in an integrated R Tableau calculated field with a couple Tableau parameters. In the example below I am using R's hampel function from the pracma library from an example posted by Bora Beran. The two argument s are k = 7 and t0 = 3 (in this case, k is the width of a moving window and t0 is the number of standard deviations to apply). I would like to replace k = 7 and t0 = 3 with a parameters so that the user can choose the moving window width and the number of standard deviations to apply.

       

      if script_real("set.seed(8421);library(pracma);

      a <- rep(1, length(.arg1));

      a[hampel(.arg1,k=7, t0=3)$ind]=0;a;",

      avg([values])) == 0

      then "Outlier"

      else "OK"

      end

       

      I created the paramters "moving_window_width" and "sigma", displayed them with "show parameter control". I have tried substituting [moving_window_width] and [sigma] for the 7 and 3 respectively, like this:

       

       

      if script_real("set.seed(8421);library(pracma);

      a <- rep(1, length(.arg1));

      a[hampel(.arg1,k=[moving_window_width], t0=[sigma])$ind]=0;a;",

      avg([values])) == 0

      then "Outlier"

      else "OK"

      end

       

      Neither of these approaches work. In fact Tableau doesn't seem to recognize the parameters anywhere within this bit of code:

       

      if script_real("set.seed(8421);library(pracma);

      a <- rep(1, length(.arg1));

      a[hampel(.arg1,k=7, t0=3)$ind]=0;a;",

       

      I am attaching a .twbx fora simple example. Any ideas would be most appreciated.

       

      Kirk

        • 1. Re: Is it possible to use parameters within R scripts in calculated fileds?
          Santiago Sanchez

          Hi Kirk,

           

          Great question! You can pass parameters into R scripts using a similar approach as other arguments (.arg1, for example). There's a catch though. When using parameters, after indicating the argument number, you need to add a [1]. In the example below, [Periods] is a parameter and we call it on the script as .arg2[1].

           

          SCRIPT_REAL("library(forecast);

          jjearnts <- ts(.arg1,deltat=1/52,start=c(2008,12));

          fcast <- forecast(jjearnts, h=.arg2[1]);

          n<-length(.arg1);

          append(.arg1[(.arg2[1]+1):n],fcast$mean, after = n-.arg2[1])",

          SUM([Sales]),[Periods])

           

          I haven't tested your script but something like this may work:

           

          if script_real("set.seed(8421);library(pracma);

          a <- rep(1, length(.arg1));

          a[hampel(.arg1,k=.arg2[1], t0=.arg3[1])$ind]=0;a;",

          avg([values]), [moving_window_width], [sigma]) == 0

          then "Outlier"

          else "OK"

          end

           

          Hope this helps!

          2 of 2 people found this helpful
          • 2. Re: Is it possible to use parameters within R scripts in calculated fileds?
            Paul Denton

            Thanks Santiago,

            your solution helped me too, I was trying to pass a parameter .arg5 into k-means, I needed the [1] adding to the end to make it work.

             

            result <- kmeans(data.frame(.arg1,.arg2,.arg3,.arg4),.arg5[1]); result$cluster;

             

            regards Paul