Skip to contents

Parses the text an R script prints (as captured from source(script, echo = TRUE)) into tidy result tables, one per detected result block. Handles both one-line test output (t.test, cor.test, ...) and fixed-width text tables (summary(lm), aov, anova). The result matches the shape of read_stat_tables(), so it flows into the same STATO typing and ISA-JSON export.

Usage

read_r_output(text, source_label = NA_character_, code_lines = NULL)

Arguments

text

the captured console output: a character vector of lines, or a single string with embedded newlines

source_label

optional label (e.g. the script name) recorded as the analysis for one-line results

code_lines

optional character vector of the script's source lines (one element per line), used to recover the source line of each result via the echoed statement text. NULL (default) skips line attribution.

Value

a list with one element per detected result block, each a list of analysis (the test/section label), title, data (a tidy data.frame), line (1-based source line, or NA when code_lines was not supplied), line_seq (1-based counter of results sharing that line, or NA), and call_fn (the recognised statistical function that produced the block, e.g. "shapiro.test", or "") – the same structure read_stat_tables() returns, plus these three fields. call_fn lets stato_type_column() resolve statistic letters that are ambiguous in the printed output alone.

Details

When code_lines is supplied (the script's own text, one element per source line – what a run's code_text_list[[file]] already is), each result is additionally tagged with the 1-based source line it came from, recovered by matching the echoed > /+ statement text against the script. A single statement that prints several results (e.g. a for loop calling t.test() each iteration) yields several results sharing one line; these are distinguished by line_seq (1, 2, 3, ... within that line). Without code_lines the echo is not parsed and line/line_seq are NA.

Examples

out <- utils::capture.output(t.test(extra ~ group, data = sleep))
res <- read_r_output(out)
res[[1]]$data
#>   mean in group 1 mean in group 2
#> 1            0.75            2.33