Run R scripts with Minitab Statistical Software

Learn about the different ways that you can run R from Minitab Statistical Software.
You can run R scripts from Minitab in three ways:
  • Run the RSCR command in the Command Line pane.
  • Run a Minitab exec that includes the RSCR command.
  • Customize the Minitab interface to run a Minitab exec that includes the RSCR command.

The RSCR command extends Minitab's functionality with R but does not make Minitab an interactive R environment. As such, use Minitab only with R scripts that complete without interaction. For example, the readline() function, which accepts input from the keyboard while a script runs, does not work in Minitab. Interactivity in R takes many other forms, such as plot() and hist().

For more information on Minitab's R package, including R code examples, go to R API Reference.

All the files referenced in this guide are available in this .ZIP file: r_guide_files.zip.

Note

Install and run R before you attempt to integrate R with Minitab Statistical Software. For assistance with the installation of R, please consult with your organization's technical support department. Minitab Technical Support cannot assist with the installation of R.

Run R scripts from the Command Line pane

You can run the RSCR command in the Command Line pane. For general information about the Command Line pane, go to Command Line/History pane. For general information about using session commands, go to Session Command help.

RSCR ["filename.R"] ["Args"...]

Runs the R script that you specify.

The default file extension for R scripts is .R. If the file extension is .R, you do not need to type the file extension.

Minitab looks for R scripts in the default folder if you do not specify a file path. The default folder location depends on your operating system. For more information, go to Default folders for R files for Minitab.

The optional argument Args allows you to pass arguments to the R script through commandArgs(trailingOnly = TRUE). Args can be any text values separated by a space. Enclose arguments in quotation marks. The default value is Null, which indicates that the script does not receive any arguments.

In general, you use arguments to bring data from Minitab into R. You can enter arguments in several ways. For example, you can use arguments that are identifiers for columns, matrices, or constants.

Minitab Session Command Value in R Usage
RSCR "test.R" "C1"
"C1"
Use the following function to retrieve the column:
mtbr::mtb_get_column(commandArgs(trailingOnly = TRUE)[1])
RSCR "test.R" "M1"
"M1"
Use the following function to retrieve the matrix:
mtbr::mtb_get_matrix(commandArgs(trailingOnly = TRUE)[1])
RSCR "test.R" "K1"
"K1"
Use the following function to retrieve the constant:
mtbr::mtb_get_constant(commandArgs(trailingOnly = TRUE)[1])

You can also use arguments that are the names of columns, matrices, or constants in Minitab.

Minitab Session Command Value in R Usage
RSCR "test.R" "My Column"
"My Column"
Use the following function to retrieve the column:
mtbr::mtb_get_column(commandArgs(trailingOnly = TRUE)[1])
RSCR "test.R" "My Matrix"
"My Matrix"
Use the following function to retrieve the matrix:
mtbr::mtb_get_matrix(commandArgs(trailingOnly = TRUE)[1])
RSCR "test.R" "My Constant"
"My Constant"
Use the following function to retrieve the constant:
mtbr::mtb_get_constant(commandArgs(trailingOnly = TRUE)[1])

You can also specify arguments to pass text to use in your R code. You can pass text directly or in a constant.

Minitab Session Command Value in R Usage
RSCR "test.R" "Text not Stored"
"Text not Stored"
This case passed a value that cannot be used with an mtbr 'get' command. However, Args are not limited to only passing columns, matrices, and constants.
LET K1 = "Text in Constant"
RSCR "test.R" K1
"Text in Constant"
This case highlights that, although RSCR does not accept arguments that are not text values, you can pass a constant to RSCR as long as the constant is defined as a text value.

When you pass more than one argument, you can access the arguments in order from the list of arguments.

Minitab Session Command Value in R Usage
RSCR "test.R" "C1" "C2" "M1" "K3" "10"
"C1"
"C2"
"M1"
"K3"
"10"
This case is an example of passing multiple Args, where you would access them by using the following functions in R:
mtbr::mtb_get_column(commandArgs(trailingOnly = TRUE)[1])
mtbr::mtb_get_column(commandArgs(trailingOnly = TRUE)[2])
mtbr::mtb_get_matrix(commandArgs(trailingOnly = TRUE)[3])
mtbr::mtb_get_constant(commandArgs(trailingOnly = TRUE)[4])
as.numeric(commandArgs(trailingOnly = TRUE)[5])

Use test.R to see output from the example text for the following subcommands. Make sure that the test.R file is in Minitab's folder for R scripts.
File Description
test.R A sample R script that is used throughout this guide. When you pass arguments to the script, the results include a list of the values of the arguments. When you run the script with the argument "ArgToBePrintedToStdErr", the script writes the name of the argument to the stderr file. When you run the script with the argument "ArgToBePrintedToStdOut", the script writes the name of the argument to the stdout file. Use the subcommands that follow to control whether the contents of these files appear in Minitab's Output pane.
NOSERR
Specifies to not display text from the standard error (message(), warning(), or stop()) console output in the Output pane in Minitab. The warning console output is where R error messages display when you run your code in an R integrated development environment. However, you can use R to save other results in the warning file. For example, by default:
RSCR "test.R" "ArgToBePrintedToStdErr".
Produces the following results that include the warning console output:

R Script

These results are from external software.
The following arguments were passed to R: ArgToBePrintedToStdErr

R standard error

The following arguments were printed to Stderr: 'ArgToBePrintedToStdErr'
The following session commands exclude the warning console output:
RSCR "test.R" "ArgToBePrintedToStdErr";
NOSERR.
The session commands produce the following results:

R Script

These results are from external software.
The following arguments were passed to R: ArgToBePrintedToStdErr
SOUT
Specifies to display text from the standard console output (print()) in the Output pane in Minitab. The standard console output is where the results of commands like print() display in an R integrated development environment. For example, by default:
RSCR "test.R" "ArgToBePrintedToStdOut".
Produces the following results that exclude the console output:

R Script

These results are from external software.
The following arguments were passed to R: ArgToBePrintedToStdOut
The following session commands include the console output:
RSCR "test.R" "ArgToBePrintedToStdOut";
SOUT.
The session commands produce the following results:

R Script

These results are from external software.
The following arguments were passed to R: ArgToBePrintedToStdOut

R standard output

[1] "The following arguments were printed to Stdout: 'ArgToBePrintedToStdOut'"

Run R scripts from a Minitab exec file

Use the following file to perform the steps in this section:
File Description
test.R A sample R script that is used throughout this guide. When you pass arguments to the script, the results include a list of the values of the arguments.
REXEC.MTB A sample Minitab exec file that includes the session commands to run the test.R script with 2 arguments.

Execs are text files that contain Minitab session commands. The RSCR command runs R in a Minitab exec. With exec files, you can easily run commands without re-typing them, and you can assign the exec to a custom button in Minitab. For more information about Minitab execs, go to Minitab Macros Help. To run an exec, choose File > Run an Exec.

Suppose you create the exec REXEC.MTB. The exec file contains the following Minitab command:
RSCR "test.R" "Arg1" "Arg2"
To run the R script with the exec, use the following steps:
  1. Choose File > Run an Exec.
  2. Click Select File.
  3. Select REXEC.MTB.
  4. Click Open.
The script displays the values of the arguments in Minitab, and the exec produces the following results:

R Script

These results are from external software.
The following arguments were passed to R: Arg1, Arg2

Run R scripts from the Minitab interface

If you have a Minitab exec file, you can create a custom button or menu that runs the exec. For general information on how to customize the interface in Minitab, go to Customize menus, toolbars and shortcut keys.

You can use the following steps to create a custom button that runs an exec:
  1. Choose View > Customize.
  2. Click the Tools tab.
  3. On the Tools tab, click the New (Insert) button .
  4. Type a name for the command, then press the Enter key.
  5. Click the Open button .
  6. From the file type drop-down list, select All Files (*.*).
  7. Browse to and select an exec file.
  8. Click Open.
  9. Choose View > Customize again.
  10. On the Commands tab, under Categories, select Tools.
  11. While the Customize dialog box is open, drag the new command to where you want it to appear on the Minitab menu or toolbar.
  12. Click Close.

In addition to customizing Minitab's interface, you can use a COM-compliant language to create custom dialog boxes and analyses. For information on how to customize Minitab through COM, go to Minitab Automation.

Stop the RSCR command

You can stop a R script and keep Minitab open, which prevents the loss of any edits to your Minitab project since your last save. The method to stop a R script depends on your operating system.
Microsoft® Windows

Press Ctrl + Alt + Delete to open the Microsoft® Windows Task Manager. Then, end the R process.