This document describes how to use the R programming language from within the application. The application uses an interpreter known as Renjin to integrate with R.
Complete the following steps to see R in action:
- Start the application.
- Click File → New to create a new file.
- Click File → Save As.
- Set Name to:
addition.Rmd
- Click Save.
Setting the file name extension tells the application what processor to use when transforming the contents for display in the preview pane. Continue by typing in the following text, including the backticks:
`r#1 + 1`
The preview pane shows the result of 1
plus 1
:
2.0
Being able to run R code while editing an R Markdown document is convenient. Having the ability to call functions is where the power of R can be leveraged.
Complete the following steps to call an R function from your own library:
- Click File → New to create a new file.
- Click File → Save As.
- Browse to your home directory.
- Set Name to:
library.R
. - Click Save.
- Set the contents to:
sum <- function( a, b ) { a + b }
- Click the Save icon.
- Click R → Script.
- Set the R Startup Script contents to:
source( 'library.R' );
- Click OK.
- Create a new file.
- Set the contents to:
`r#sum( 5, 5 )`
- Save the file as
sum.R
.
The preview panel shows the result of calling the sum
function:
10.0
This shows how the bootstrap script can load library.R
, which defines
a sum
function that is called by name in the Markdown document.
R files may be sourced from any directory, not just the user's home directory. Accomplish this as follows:
- Click R → Directory.
- Set Directory to a different directory.
- Click OK.
- Create the directory if it does not exist.
- Move
library.R
into the directory. - Append a new function to
library.R
as follows:mul <- function( a, b ) { a * b }
- Click R → Script.
- Set the R Startup Script contents to:
setwd( '$application.r.working.directory$' ); source( 'library.R' );
- Change
sum.Rmd
to:`r#mul( 5, 5 )`
- Close the file
sum.Rmd
. - Confirm saving the file when prompted.
- Re-open
sum.Rmd
.
The preview panel shows:
25.0
Calling setwd
using '$application.r.working.directory$'
changes the
working directory where the R engine searches for source files.
To see how variable definitions work in R, try the following:
- Create a new file.
- Change the contents to (use spaces not tabs):
project: title: Project Title author: Author Name
- Save the file as
definitions.yaml
. - Click File → Open.
- Set Source Files to Definition Files.
- Select
definitions.yaml
. - Click Open.
- Open
sum.Rmd
if it is not already open. - Type:
je
- Press
Ctrl+Space
The editor inserts the following text (matches je
against Project):
`r#x( v$project$title )`
The preview panel shows:
r#x( 'Project Title' )
This is because the application inserts definition reference names based
on the type of file being edited. By default, the R engine does not have
a function named x
defined.
Continue as follows:
- Click R → Script.
- Append the following:
x <- function( s ) { tryCatch( { r = eval( parse( text = s ) ) ifelse( is.atomic( r ), r, s ); }, warning = function( w ) { s }, error = function( e ) { s } ) }
- Click OK.
- Close and re-open
sum.Rmd
.
The preview panel shows:
25.0
Project Title
The x
function attempts to evaluate the expression defined by the YAML
variable. This means that the YAML definitions can also include expressions
that R is capable of evaluating.
While the x
function can be defined within the R Startup Script, it is
better practice to put it into its own library so that it can be reused
outside of the application.