Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better way of extracting string from expression #35

Closed
hadley opened this issue Nov 10, 2016 · 2 comments

Comments

@hadley
Copy link
Member

commented Nov 10, 2016

Here's a sketch from me and @wch

x <- quote({
  # This is a comment
  a +      b
})

src <- attr(x, "srcref")
src_1 <- src[[1]]
src_n <- src[[length(src)]]

lines <-  attr(src_1, "srcfile")$lines
lines <- 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]
}
@wch

This comment has been minimized.

Copy link
Member

commented Nov 11, 2016

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 comment
  a +      b
})

src <- attr(x, "srcref")
src_1 <- src[[1]]
src_n <- src[[length(src)]]

lines <-  attr(src_1, "srcfile")$lines
lines <-  strsplit(lines, "\n")[[1]]     # <-- Here
lines <- 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 comment
  a +      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))  # <--- Here
lines <-  attr(src_1, "srcfile")$lines
lines <- 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"         
@jennybc

This comment has been minimized.

Copy link
Member

commented Nov 11, 2016

It appears that attr(src[[1]], "srcfile")$fixedNewlines can be consulted to determine the split status.

@jennybc jennybc closed this in 0271b9a Nov 12, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.