sed_comment: Add or remove comment characters to a line

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/sed_comment.R

Description

Add or remove comment characters to a line

Usage

1
2
sed_comment(stream, at, add = TRUE, type = c("R", "C", "Java", "html",
  "tex", "SAS"), warn = FALSE, ...)

Arguments

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 readLines.

at

A vector of integers or a character string that designates the line(s) that will be commented. If at is numeric, it designates the line(s) (or elements) in stream that will be commented. The numeric value(s) of at must be in [1:length(stream)]. If at is a character string, the line(s) in stream that contain the string at are commented.

add

A logical indicating whether comments are added to a single line (TRUE), or removed (FALSE).

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 type[1] designates the beginning comment character and type[2] designates the ending comment character.

warn

If TRUE, warning messages are produced if commenting (or uncommenting) fails due to mispecifification of at.

...

Additional named arguments to grep, which are applicable if at is a character string. In other words, grep is used to search for the instances of at.

Details

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.

Value

The new stream with the commented (or uncommented) lines. If the commenting fails because at is specified incorrectly, stream is returned unchanged.

Author(s)

Landon Sego

See Also

sed_insert, sed_replace, sed_substitute, streamEdit

Examples

 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")

rsed documentation built on May 1, 2019, 10:40 p.m.