R script window

 

Menu location: Continue in R on the right-click menu of a result in a report, the R button on the toolbar of a report, File > New R session for an empty R script, or File > Open of a StatsDirect notebook (.ipynb), whose R code opens here.

 

The R script window holds an R script and runs it with the R installed on your computer. Continue in R on a result writes the script of that result: the data as the analysis used them, read from the workbook, the settings, and the analysis as the R section of its help topic shows it. For a chart follow-on of a regression, such as the residual plots, the script draws the plots after the regression. The R button on the toolbar of a report does the same for the result the cursor is in, or for the latest result when the cursor is in none. The script is short and plain, so that you can read it, change it and run it again, or take it to R or RStudio. Every analysis whose help topic has an R section has such a script; for the others, Open data and settings in R gives the data and settings for you to add the analysis.

 

A script opened with Continue in R runs once as soon as it opens. File > New R session opens an empty window without starting R or running code. R starts when you first run code. A saved R script or the R code of an imported notebook (.ipynb) is not run until you choose Run.

 

Console and Script

Console lets you enter R commands and see their results and charts. Type after the prompt and press Enter to run a command; Shift+Enter adds a line. R shows + when it needs the rest of an expression. When R asks a question, enter your answer in the same input box. Up and Down recall previous commands when the input is a single line.

The Script tab holds code you can edit and save. Select part of it and choose Run selection, or leave nothing selected and choose Run script. The Console shows what R prints. Commands and prompts are blue, ordinary output is dark text, warnings and errors are red, and status notices are grey.

Console and Script use the same R workspace. Variables, loaded packages and completed calculations remain available when you run more code or switch tabs. For example, a variable created in Script can be used in a later Console command. Each R window has its own workspace.

 

Stop and Restart

Stop interrupts the current calculation, incomplete expression or question. It does not submit a blank answer. Variables assigned before the interruption remain available; completed changes to data or files are not undone. Wait for the ready prompt before entering another command.

Restart asks for confirmation, then starts a fresh R session. It clears the R workspace, including its variables and loaded packages, while keeping the Script text. Use it when you want a clean start or R does not respond to Stop.

Under More, Clear output clears the displayed output; it does not clear R variables. Output folder opens the R folder of your StatsDirect documents.

 

Charts and editing

Use Open chart to open a chart separately or Copy chart to copy it. Each button refers to the chart beside it, including an earlier version if later commands changed the plot. Ordinary Windows R charts use SVG, so they remain sharp when resized.

To put a copied chart in a report, place the cursor where you want it and choose Paste or press Ctrl+V. SVG remains vector SVG in a StatsDirect report, with a PNG alternative for applications that need a picture. The report keeps its own copy, so the R output file is not needed to reopen it. Press Enter with the picture selected, or with the cursor immediately after it, to continue writing below it. Undo and Redo include the paste and the new paragraph.

Save and Save As on the File menu keep the Script as an R file, which R and RStudio can open. Saving the script does not save the live R workspace. Closing the window ends its R session. Cut, Copy, Paste and Find work on the script as in any editor; right-click for its editing commands.

Open in R, under More, saves the script as an R file and opens it in RStudio when that is installed, otherwise in R's own window. AI opens the Script AI pane for questions about the script, its output and the methods. Suggested code is for you to review and run.

 

The first lines of a script say which workbook and sheet its data came from and when they were read, and note any column that has changed in the workbook since the analysis ran, or could not be read, so that you can put its values in by hand. This Windows console uses 64-bit R 4.6.x (tested with R 4.6.1 and 4.6.2). If R is missing, StatsDirect offers to install it on first use. Use More > Set up R to install a compatible R version when an older version is already installed.

 

Help on this window opens this topic while the window is empty; once a script of a result is in it, Help opens that result's topic, whose R section the script follows.