I must be re-inventing the wheel here -- what package/function should I be using instead? #rstats
The idea is -- for environments where we don't have an IDE / ctrl+Enter-friendly execution environment, source() a script but expression-by-expression (a la browser()/debug()).
So I can do .DebuggedScript(r_script)() and then start 'n' my way through it.
=> More informations about this toot | More toots from michaelchirico@fosstodon.org
@michaelchirico You can simplify this a bit with
library(readr) x <- parse(text = paste("browser()", "{", read_file("script.R"), "}", sep = "\n")) eval(x)
For base R only, you could replace the read_file
call with
paste(readLines("script.R"), collapse = "\n")
Maybe you could write a helper function to add the initial browser();{
at a line of your choice to avoid debugging the whole script.
=> More informations about this toot | More toots from HeathrTurnr@fosstodon.org
@HeathrTurnr IMO readChar() should be the better choice here, it's just clunky for now...
https://stat.ethz.ch/pipermail/r-devel/2024-January/083148.html
=> More informations about this toot | More toots from michaelchirico@fosstodon.org
@michaelchirico Interesting this came up recently - it does seem like there should be a neater way to do this. But Duncan Murdoch has a point about Windows-style newlines and I think it matters here, e.g.
x <- parse(text = paste("browser()", "{", "x <- 1:3\ny <- rnorm(x)", "}", sep = "\n"))
works, but
x <- parse(text = paste("browser()", "{", "x <- 1:3\r\ny <- rnorm(x)", "}", sep = "\n"))
throws an error.
=> More informations about this toot | More toots from HeathrTurnr@fosstodon.org
@HeathrTurnr I think it's a useful thing to keep in mind, the behavior you found here is strange. parse() on windows files works fine... anyway, for your approach, I'd do:
parse(text = c(
'browser()',
'{',
readLines(f),
'}'
))
and let parse() handle the concatenation part as needed.
=> More informations about this toot | More toots from michaelchirico@fosstodon.org
@michaelchirico Ah yes that’s even simpler!
=> More informations about this toot | More toots from HeathrTurnr@fosstodon.org This content has been proxied by September (3851b).Proxy Information
text/gemini