moodymudskipper / ask

ask R anything
Other
72 stars 2 forks source link

contexts #1

Closed moodymudskipper closed 1 month ago

moodymudskipper commented 2 months ago

I think we need a concept of nested contexts.

The general function ask() could take a context argument, which is a nested list with named elements. A context object, once flattened into a string, is a natural language representation of structured data that describes the context.

A simple example :

context <- list(
  "File: R/file1.R" = readLines("R/file1.R"),
  "File: R/file1.R" = readLines("R/file1.R")
)

flatten_context(context)
#> # File: R/file1.R ---
#> a <- 1
#> b <- a + 1
#>
#> # File: R/file2.R ---
#> c <- 3

# a helper
context_files <- function(paths) {
  out <- lapply(paths, readLines)
  names(out) <- paste("File:", paths, "---")
}

Then we can use it in a bigger context :

context <- list(
  "Session Info" = capture.output(session.info()),
  "files" = context_files(c("R/file1.R", "R/file2.R"))
)

The formatting of nested items depend of the depth, so now we would have :

#> # Session Info ---
#> ...
#> # Files ---
#> ## File: R/file1.R ---
#> ...
#>
#> ## File: R/file2.R ---
#> ...

Maybe some delimitation formats would be more effective than others ?

chatgpt itself suggests delimiters that vary with depth:

Top level: ---
Second level: ~~~
Third level: +++