Description Usage Arguments Details Value Author(s) See Also Examples
Add or remove comment characters to a line
1 2 |
stream |
A character vector, each element typically (but not necessarily) containing the text
from a single line in a file, which can be generated via |
at |
A vector of integers or a character string that designates the line(s) that will be commented.
If |
add |
A logical indicating whether comments are added to a single
line ( |
type |
A character string uniquely indicating the programming language: R, C, Java, html, tex, and SAS.
Customized commenting can be achieved by providing a character vector of length 2, where |
warn |
If |
... |
Additional named arguments to |
Some languages provide a way to comment multiple lines of code with a single pair of beginning and ending commenting
symbols. However, sed_comment
only adds/removes comment symbols to/from a single lines.
When comments are added by sed_comment
, the comment symbols are placed at the beginning (and, if applicable, at the end) of
the selected lines. When comments are removed, the first instances of the beginning (and, if applicable, ending) comment symbols
are removed from the selected lines.
The new stream
with the commented (or uncommented) lines. If the commenting fails because at
is
specified incorrectly, stream
is returned unchanged.
Landon Sego
sed_insert
, sed_replace
, sed_substitute
, streamEdit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | ######################################################################
# Let's create a stream to demonstrate the commenting symbol in each
# language
######################################################################
demoStream <- c("An R comment",
"A C comment",
"An html comment",
"A tex comment",
"A SAS comment",
"A custom comment")
a <- sed_comment(demoStream, "R", type = "R")
a <- sed_comment(a, "C", type = "C")
a <- sed_comment(a, "html", type = "h")
a <- sed_comment(a, "tex", type = "t")
a <- sed_comment(a, "SAS", type = "S")
a <- sed_comment(a, "custom", type = c("&", ";"))
# Compare before and after
as.stream(demoStream)
a
######################################################################
# Various examples
######################################################################
aStream <- c("Here's a line to comment",
"# A line to uncomment",
" <!-- Another commented line --> ",
"And some comments * embedded in the line ;")
as.stream(aStream)
# Comment the first line in C style
stream <- sed_comment(aStream, "to comment", type = "C")
# Comment the first line with a custom style
a <- sed_comment(aStream, "to comment", type = c("&&", "##"))
a
# Remove the custom comments
a <- sed_comment(a, 1, add = FALSE, type = c("&&", "##"))
a
# Remove the R comment from the 2nd line
a <- sed_comment(a, 2, add = FALSE, type = "R")
a
# Remove the html comments
a <- sed_comment(a, "Another", add = FALSE, type = "html")
a
# Remove the SAS comments
sed_comment(a, "embedded", add = FALSE, type = "SAS")
# Comment every line in Java style
b <- sed_comment(aStream, "comment", type = "Java")
b
# Remove the Java comments from the second and fourth lines
sed_comment(b, c(2, 4), add = FALSE, type = "Java")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.