r - What are the caveats of using source versus parse & eval? -
short version
can replace
source(filename, local = true, encoding = 'utf-8')
with
eval(parse(filename, encoding = 'utf-8'))
without risk of breakage, make utf-8 source files work on windows?
long version
i loading specific source files via
source(filename, local = true, encoding = 'utf-8')
however, known this not work on windows, full stop.
as workaround, joe cheng suggested using instead
eval(parse(filename, encoding = 'utf-8'))
this seems work quite well1 after consulting source code of source
, don’t understand how differ in 1 crucial detail:
both source
, sys.source
not parse
, eval
file content. instead, parse file content , iterate manually on parsed expressions, , eval
them 1 one. not understand why necessary in sys.source
(source
@ least uses show verbose diagnostics, if instructed; sys.source
nothing of kind):
for (i in seq_along(exprs)) eval(exprs[i], envir)
what purpose of eval
ing statements separately? , why iterating on indices instead directly on sub-expressions? other caveats there?
to clarify: not concerned additional parameters of source
, parse
, of may set via options.
1 reason source
tripped encoding parse
isn’t boils down fact source
attempts convert input text. parse
no such thing, reads file’s byte content as-is , marks encoding
utf-8
in memory.
this not full answer addresses seq_along
part of question, lengthy include comments.
one key difference between seq_along
followed [
vs using for in x
approach (which believe similar seq_along
followed [[
instead of [
) former preserves expression. here example illustrate difference:
> txt <- "x <- 1 + 1 + # abnormal expression + 2 * + 3 + " > x <- parse(text=txt, keep.source=true) > > for(i in x) print(i) x <- 1 + 1 2 * 3 > for(i in seq_along(x)) print(x[i]) expression(x <- 1 + 1) expression(2 * 3)
alternatively:
> attributes(x[[2]]) null > attributes(x[2]) $srcref $srcref[[1]] 2 * 3
whether has practical impact when comparing eval(parse(..., keep.source=t))
, can could, can't imagine situation does.
note subsetting expression separately leads srcref
business getting subset, conceivably useful (...maybe?).
Comments
Post a Comment