highlight: Syntax highlight R code

Description Usage Arguments Value Examples

View source: R/highlight.R

Description

Syntax highlight R code

Usage

1
highlight(code, style = default_style())

Arguments

code

Character vector, each element is one line of code.

style

Style functions, see default_style().

Value

Character vector, the highlighted code.

Examples

1
2

Example output

Attaching package: 'prettycode'

The following object is masked from 'package:base':

    print.function

 [1] "function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE, "               
 [2] "    pattern, sorted = TRUE) "                                                              
 [3] "{"                                                                                         
 [4] "    if (!missing(name)) {"                                                                 
 [5] "        pos <- tryCatch(name, error = function(e) e)"                                      
 [6] "        if (inherits(pos, \"error\")) {"                                                   
 [7] "            name <- substitute(name)"                                                      
 [8] "            if (!is.character(name)) "                                                     
 [9] "                name <- deparse(name)"                                                     
[10] "            warning(gettextf(\"%s converted to character string\", "                       
[11] "                sQuote(name)), domain = NA)"                                               
[12] "            pos <- name"                                                                   
[13] "        }"                                                                                 
[14] "    }"                                                                                     
[15] "    all.names <- .Internal(ls(envir, all.names, sorted))"                                  
[16] "    if (!missing(pattern)) {"                                                              
[17] "        if ((ll <- length(grep(\"[\", pattern, fixed = TRUE))) && "                        
[18] "            ll != length(grep(\"]\", pattern, fixed = TRUE))) {"                           
[19] "            if (pattern == \"[\") {"                                                       
[20] "                pattern <- \"\\\\[\""                                                      
[21] "                warning(\"replaced regular expression pattern '[' by  '\\\\\\\\['\")"      
[22] "            }"                                                                             
[23] "            else if (length(grep(\"[^\\\\\\\\]\\\\[<-\", pattern))) {"                     
[24] "                pattern <- sub(\"\\\\[<-\", \"\\\\\\\\\\\\[<-\", pattern)"                 
[25] "                warning(\"replaced '[<-' by '\\\\\\\\[<-' in regular expression pattern\")"
[26] "            }"                                                                             
[27] "        }"                                                                                 
[28] "        grep(pattern, all.names, value = TRUE)"                                            
[29] "    }"                                                                                     
[30] "    else all.names"                                                                        
[31] "}"                                                                                         
function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE, 
    pattern, sorted = TRUE) 
{
    if (!missing(name)) {
        pos <- tryCatch(name, error = function(e) e)
        if (inherits(pos, "error")) {
            name <- substitute(name)
            if (!is.character(name)) 
                name <- deparse(name)
            warning(gettextf("%s converted to character string", 
                sQuote(name)), domain = NA)
            pos <- name
        }
    }
    all.names <- .Internal(ls(envir, all.names, sorted))
    if (!missing(pattern)) {
        if ((ll <- length(grep("[", pattern, fixed = TRUE))) && 
            ll != length(grep("]", pattern, fixed = TRUE))) {
            if (pattern == "[") {
                pattern <- "\\["
                warning("replaced regular expression pattern '[' by  '\\\\['")
            }
            else if (length(grep("[^\\\\]\\[<-", pattern))) {
                pattern <- sub("\\[<-", "\\\\\\[<-", pattern)
                warning("replaced '[<-' by '\\\\[<-' in regular expression pattern")
            }
        }
        grep(pattern, all.names, value = TRUE)
    }
    else all.names
}

prettycode documentation built on Dec. 16, 2019, 5:42 p.m.