This project has moved and is read-only. For the latest updates, please go here.

Taking user input for R parameters

Jun 30, 2014 at 11:51 AM

I was able to integrate R with Microstrategy so that R metrices are calculated on R engine. I have another problem that for one metric, we need to take time parameter from the end user. Has someone tried something like this before?
Any help on this would be appreciated.

Jul 31, 2014 at 10:07 PM
To take a time parameter from an end-user, you have one challenge and two options:

The challenge is that we don't allow passing date/time data types between MicroStrategy and R, mainly due to the complexity involved with various date-time formats that can be used and the fact that most algorithms deal with dates and times as transformed numeric values (e.g., hours since midnight, day of week, months since 1/1/2000). The good news is that R has features such as the strptime and as.POSIXct functions which can handle a wide variety of data conversions to and from date/time formats (type "?strptime" and "?as.POSIXct" at an R Console command prompt for more details). You should be able to use these functions to convert numeric or string input from the end user into the required date/time object you need for your script.

You have two options for getting the input from the end-user:
  • Use a function parameter: For scalar values, you can use a function parameter so the end user can edit the metric expression to set the value. Here's an example where the parameter StringParam1 contains the time input from the user: RScript<_RScriptFile="MyAnalytic.R", StringParam1="12/23/2013">(X, Y).
  • Use a function argument: This also works for scalar values and users who can edit metric expressions using a static value as an input to the analytic; for example, RScript<_RScriptFile="MyAnalytic.R">(X, Y, "12/23/2013"). In addition, the argument could also take in a MicroStrategy metric that returned a scalar or a vector-- and if that metric contained a prompt, then the user would be prompted for the value.
Hope this helps, if not, please reply back.

Good luck!
Oct 28, 2014 at 6:39 PM

thanks for the reply. Could not try this for a while as we got busy with some other work.
I am trying this for few days now, but am not able to pass user value prompt to R script metric.
When I passed an existing metric as parameter, it works. But when I take the metric on which there is a value prompt, the value provided by user does not pass to R script.

The value needed from user is a numeric value and not a date.
Can you please help with this? I just need to pass user provided number to R script as 'target'.

thanks in advance,
Oct 28, 2014 at 7:32 PM
Hi Harsh,

Can you clarify the exact behavior you're trying to accomplish? You say, "When I passed an existing metric as parameter, it works." Just to be clear, values can be passed into an R script in one of two ways: Either as Inputs which must be metrics that appear in between the parentheses of a metric expression, or as Parameters which are static values that appear in between the angle brackets of metric expression.

For example, here's the metric expression for the SeasonalForecasting.R script that comes with the R Integration Pack:

RScript<_RScriptFile="SeasonalForecasting.R", _InputNames="Target, Trend, Season", StringParam9="">(Target, Trend, Season)

So, when you say that it worked when you passed an existing metric as a parameter, you must really mean you passed in a metric as an Input, for example Target in the script above. And replacing Target with a different metric, one that is based on a value prompt that the user provides, then that number from the user should work as well.

Or, are you trying to pass in this value to a Parameter, as StringParam9 (or a NumericParam for a number). Parameters cannot take in values from a metric, they are defined by the static values in the metric expression.

If you can clarify what you're trying to do, hopefully we can help you better.


Oct 28, 2014 at 8:11 PM
Hi Rick,
Sorry for the confusion. I am not using parameter anywhere.
Our R script has an input numeric metric which should be provided by users running dashboard in MSTR.
For that, we added two reports as datasets in a dashboard, one containing core metrices and other containing metrices with value prompts.
Then we defined R metric as in
RScript<_RScriptFile="SeasonalForecasting.R", _InputNames="Target, Trend, Season">(Target, Trend, Season) where we set Season input as the metric with prompt. This passes null values to R script, which we checked by logging in the script.
If we pass one of the core metric as input in Season, the values are received in R script.
Please let me know if any other information would be relevant and I will provide it. The script is huge with more than 20 inputs, so I have avoided pasting it.

Jun 17, 2016 at 9:19 PM
Edited Jun 17, 2016 at 9:20 PM
I am trying to solve a closely related issue. I want to pass data as vector inputs and additionally pass an image file name as a scalar . If I use a parameter, the image file name has to be static, but I want it to be a dynamic text value created in a scalar metric. If I try adding a scalar metric input with the list of vector metric inputs, the RSCript call returns only null output values. Even though DeployR() lets one specify mixed vector and scalar inputs in the function signatures it creates, the available calling functions RScript, RScriptU, RScriptAgg, RScriptAggU, and RScriptSimple seem able to only accept either all vector inputs or all scalar inputs. It seems like to accomplish what I need, either of two enhancements are needed, and both would be desirable to have. One: Enable the _Params parameter to take metrics or attribute values, not just static values. Two: Have an RScript calling function that can in fact handle signatures with mixed vector and scalar arguments as created by DeployR().
Jun 17, 2016 at 10:17 PM
The workaround I am using is to pass the desired scalar value as a vector with all the rows the same and then take the first element of the vector as my scalar in R.