Skip to contents

This function takes an input string, detects R code and comments, and returns a character vector containing the R code and comments. The input string is split into lines based on newline characters, and each line is examined for R code and comment patterns. Only the lines that match either of these patterns are returned.





A character string containing R code and comments, mixed with other text. The string may contain multiple lines separated by newline characters.


A character vector containing R code and comment lines extracted from the input string. Each element in the vector corresponds to one line of code or a comment.


example_string <-
"This is a text string with R code and comments.\n
# A comment\n
x <- 5\
ny = 10\n
z <- x + y\n
Another line of text."
#> [1] "# A comment" "x <- 5"      "ny = 10"     "z <- x + y"