You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that attr(src_1, "srcfile")$lines gives a string with \n characters in it instead of a char vector where each element represents one line. Running the code in the above example has someNAs:
lines# [1] "\n # This is a comment\n a + b\n})\n"# [2] NA # [3] NA
So you need to add a strsplit():
x<- quote({
# This is a commenta+b
})
src<- attr(x, "srcref")
src_1<-src[[1]]
src_n<-src[[length(src)]]
lines<- attr(src_1, "srcfile")$lineslines<- strsplit(lines, "\n")[[1]] # <-- Herelines<-lines[seq(src_1[1], src_n[3])]
lines[1] <- substring(lines[1], src_1[5] +1, nchar(lines[1]))
if (lines[1] ==""|| grepl("^\\s+$", lines[1])) {
lines<-lines[-1]
}
lines# [1] " # This is a comment" " a + b"
Oddly, if you access the source ref before the lines, it causes the string to be split up into a char vector, as a side effect -- just like if you called strsplit() on it.
x<- quote({
# This is a commenta+b
})
src<- attr(x, "srcref")
src_1<-src[[1]]
src_n<-src[[length(src)]]
srcref(attr(src_1, "srcfile"), c(1,1,1,1,1,1,1,1)) # <--- Herelines<- attr(src_1, "srcfile")$lineslines<-lines[seq(src_1[1], src_n[3])]
lines[1] <- substring(lines[1], src_1[5] +1, nchar(lines[1]))
if (lines[1] ==""|| grepl("^\\s+$", lines[1])) {
lines<-lines[-1]
}
lines# [1] " # This is a comment" " a + b"
Here's a sketch from me and @wch
The text was updated successfully, but these errors were encountered: