Skip to content

Better way of extracting string from expression #35

Description

@hadley

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]
}

Activity

  1. wch commented on Nov 11, 2016

    @wch
    Member

    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"         
  2. jennybc commented on Nov 11, 2016

    @jennybc
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions