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, the R button on the worksheet toolbar, File > New R session (on Mac, File > New Script) 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. The R button on the worksheet toolbar opens a new window with the worksheet's data as worksheet_data, if you choose to bring them; see Workbook for how the data arrive.

 

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. On Mac, File > New Script (Command-R) opens an empty R window on its Script tab and starts R straight away, without running any 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, the question appears in the transcript and you 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. On Mac, the R window has no More menu, Clear output or Output folder; R works in a folder of its own for each window, under ~/Library/Application Support/StatsDirect Viewer/R Sessions.

 

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. Charts in this window use SVG, on Windows and Mac, so they remain sharp when resized.

The separate chart viewer has −, a zoom percentage, + and Fit controls. Use them to enlarge a chart and scroll around it; click the percentage or Fit to show the complete chart again. On Windows, Ctrl++, Ctrl+− and Ctrl+0 provide the same commands; on Mac use the Command key. Right-click a chart for Copy Chart, Save Chart as SVG and Save Chart as PNG. Copying and saving always use the complete retained chart, regardless of the zoom or the visible area.

To put a copied chart in a report, place the cursor where you want it and choose Paste or press Ctrl+V (Command-V on Mac). 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.

R+ on the toolbar opens the script in RStudio when that is installed, as a new R file each time, named after the window and the time; otherwise it runs the script in R's own window, RGui on Windows or R.app on Mac, and opens it in R's editor. On Windows the file is in the R folder of your StatsDirect documents, whichever opens it. On Mac, RStudio's file is in StatsDirect's R Scripts folder, and R.app gets a temporary copy, removed when R.app ends unless you save changes to it in R's editor; with neither installed, the script is saved in R Scripts and the status line says so. A script that holds worksheet data keeps them. On Windows, saving it as analysis.R, with Save, Save As or R+, also writes analysis.worksheet-data.csv beside it, which the script reads; on Mac the values are written into the script itself. 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.2 or later (tested with R 4.6.2 and R 4.2.3), the newest installed. If R is missing, StatsDirect offers to install it on first use. Use More > Set up R to install a current R when only an older R is installed. On Mac, the console uses Apple silicon R 4.2 or later, the newest installed from CRAN or Homebrew. When only an older R is installed the console says so: install a current R from CRAN, then choose Restart. The first R window also downloads R's svglite package for its charts, which needs an internet connection.

 

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.