matahari

Please see the matahari website for full documentation:

library(matahari)

There are three ways to use the matahari package

  1. Record R code as it is typed and output a tidy data frame of the contents
  2. Input a character string of R code and output a tidy data frame of the contents
  3. Input an R file containing R code and output a tidy data frame of the contents

This vignette will walk through how to do each of these tasks.

Record R code as it is typed

To record R code as it is typed, use the dance_start() and dance_stop() functions.

# Start logging your R commands run in the console
dance_start()

4 + 4
"wow!"
mean(1:10)

# Pause logging
dance_stop()

# Look at your log as a tidy data frame
dance_tbl()
#> # A tibble: 6 x 6
#>   expr       value             path      contents  selection dt                 
#>   <list>     <list>            <list>    <list>    <list>    <dttm>             
#> 1 <language> <S3: sessionInfo> <lgl [1]> <lgl [1]> <lgl [1]> 2019-05-14 08:15:24
#> 2 <language> <lgl [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2019-05-14 08:15:24
#> 3 <language> <lgl [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2019-05-14 08:15:25
#> 4 <chr [1]>  <lgl [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2019-05-14 08:15:25
#> 5 <language> <lgl [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2019-05-14 08:15:25
#> 6 <language> <S3: sessionInfo> <lgl [1]> <lgl [1]> <lgl [1]> 2019-05-14 08:15:25

Each time dance_start() is run, a data frame logging the subsequent code is updated. To start over with a new data frame, you can use dance_remove() and then re-run dance_start().

dance_remove()

By default, this just records the R calls typed into the R console, populating the expr column of the data frame. You can optionally also record the following by setting the parameter of the name to TRUE. Some of these rely on using RStudio as your IDE.

  • value: The values that are computed
  • path: The path to the file in focus on the RStudio editor
  • contents: The file contents of the RStudio editor tab in focus
  • selection: The text that is highlighted in the RStudio editor tab in focus

For example, the same code with value = TRUE results in the following tidy data frame.

# Start logging your R commands run in the console
dance_start(value = TRUE)

4 + 4
"wow!"
mean(1:10)

# Pause logging
dance_stop()

# Look at your log as a tidy data frame
dance_tbl()
#> # A tibble: 6 x 6
#>   expr       value             path      contents  selection dt                 
#>   <list>     <list>            <list>    <list>    <list>    <dttm>             
#> 1 <language> <S3: sessionInfo> <lgl [1]> <lgl [1]> <lgl [1]> 2018-06-23 15:26:06
#> 2 <language> <int [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2018-06-23 15:26:06
#> 3 <language> <dbl [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2018-06-23 15:26:07
#> 4 <chr [1]>  <chr [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2018-06-23 15:26:08
#> 5 <language> <dbl [1]>         <lgl [1]> <lgl [1]> <lgl [1]> 2018-06-23 15:26:08
#> 6 <language> <S3: sessionInfo> <lgl [1]> <lgl [1]> <lgl [1]> 2018-06-23 15:26:09

Notice now the value column is now populated.

Input a character string of R code

Alternatively you can input a string of R code and output a tidy data frame, using the dance_recital() function.

dance_recital("
4 + 4
'wow!'
mean(1:10)
             ")
#> # A tibble: 3 × 6
#>   expr       value     error  output    warnings  messages 
#>   <list>     <list>    <list> <list>    <list>    <list>   
#> 1 <language> <dbl [1]> <NULL> <chr [1]> <chr [0]> <chr [0]>
#> 2 <chr [1]>  <chr [1]> <NULL> <chr [1]> <chr [0]> <chr [0]>
#> 3 <language> <dbl [1]> <NULL> <chr [1]> <chr [0]> <chr [0]>

This creates the same expr and value columns as the dance_start() dance_stop() workflow described above. In addition, it outputs any errors, output, warnings, or messages generated by the R code in the character string. Notice this data frame does not have the session information, since it is not called interactively from the R console. By default, dance_recital() will evaluate all R code passed to it to retrieve the value column. If you would like to just output the expr column, you can set evaluate = FALSE. This may be especially useful if you are analyzing lots of code and it would take a long time to run all commands.

Input a .R file

The same dance_recital() function can be used to analyze a .R file. Instead of including the code as a character string, pass the path of the .R file. An example file is included in this package.

(file <- system.file("test", "sample_code.R", package = "matahari"))
#> [1] "/tmp/RtmpOVFYS6/Rinstc1fcd3fa25/matahari/test/sample_code.R"
dance_recital(file)
#> # A tibble: 7 × 6
#>   expr       value     error      output    warnings  messages 
#>   <list>     <list>    <list>     <list>    <list>    <list>   
#> 1 <language> <dbl [1]> <NULL>     <chr [1]> <chr [0]> <chr [0]>
#> 2 <chr [1]>  <chr [1]> <NULL>     <chr [1]> <chr [0]> <chr [0]>
#> 3 <language> <dbl [1]> <NULL>     <chr [1]> <chr [0]> <chr [0]>
#> 4 <language> <NULL>    <smplErrr> <NULL>    <NULL>    <NULL>   
#> 5 <language> <chr [1]> <NULL>     <chr [1]> <chr [1]> <chr [0]>
#> 6 <language> <NULL>    <NULL>     <chr [1]> <chr [0]> <chr [1]>
#> 7 <language> <NULL>    <NULL>     <chr [1]> <chr [0]> <chr [0]>