Please see the matahari website for full documentation:
knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(matahari)
There are three ways to use the matahari package
This vignette will walk through how to do each of these tasks.
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 computedpath
: The path to the file in focus on the RStudio editorcontents
: The file contents of the RStudio editor tab in focusselection
: The text that is highlighted in the RStudio editor tab in focusFor 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.
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) ")
This creates the same expr
and value
columns as the dance_start()
dance_stop()
workflow described above. In addition, it outputs any error
s, 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.
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"))
dance_recital(file)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.