Giter Club home page Giter Club logo

highlight's Introduction

highlight

highlight is a syntax highlighter for R code.

Installation

You can install highlight from github with:

# install.packages("devtools")
devtools::install_github("romainfrancois/highlight")

Typical highlighting (lestrade)

Typical syntax highlighting is only interested in whether a token is a function call, a keyword, a string, ... this is what the lestrade detective does:

highlight( file = "css_file.R", detective = lestrade )

This will look like this:

This differentiates function calls, formal arguments, used arguments ... because lestrade identified these tokens as such.

semantic highlighting (sherlock)

Traditional highlighting only reveals the obvious, so the package also benefits from the investigation of sherlock for semantic highlighting. In semantic highlighting, every symbol gets a different color.

highlight( file = "css_file.R", detective = sherlock )

With this we can quickly skim through the file and see e.g. the different uses of filename.

In rmarkdown

To use in rmarkdown you can use the hl_hook_source and hl_hook_document hooks. Unfortunately this does not work for README on github so this document uses screenshots.

knitr::knit_hooks$set( 
  source = hl_hook_source, 
  document = hl_hook_css
)

highlight's People

Contributors

romainfrancois avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

highlight's Issues

obfuscating

similar to #13 but when we want to hide some code, like in teaching

For example

iris %>%
  ...hide...(group_by)( Species ) %>%
  ...hide...(summarise_all)( mean )

would replace each character with eg. ▪, so that we would see:

iris %>%
 ▪▪▪▪▪▪▪▪( Species ) %>%
 ▪▪▪▪▪▪▪▪▪▪▪▪▪( mean )

need a way to control htmlEscape

needed for r-lib/pkgdown#521

So that detectives can already format text in

 data <- getParseData( parse( text = x, keep.source = TRUE) ) %>% 
    detective(...) %>% 
    filter(terminal) %>% 
    mutate( text = htmlEscape(text) )

Declare C++11 need

From BR:

This concerns packages

BDgraph Branching CAMAN DALY MCMCglmm PopGenome RCPmod Rsomoclu SGCS
SSN SpeciesMix TraMineR WhopGenome adaptivetau climdex.pcic dbarts
devEMF distory dynaTree evtree fishMod gbm gmp hdlm highlight hint
mixAK monomvn multic mvPot protViz rBeta2009 simLife
spatialsegregation stringi topicmodels wtcrsk

These use functions/macros which are not part of C++98: see the logs
at http://www.stats.ox.ac.uk/pub/bdr/C++Solaris .

Functions

erf expm1 floorf fmin fminf fmax lgamma lround loglp round snprintf
strcasecmp trunc

were introduced in C99 and so are part of C++11 but not C++98 and the
Solaris C++ compiler does not provide prototypes for them: unfortunately
g++ does not warn of this. (R is currently including from R.h legacy
headers such as math.h to work around this, but that will be changed
for R 3.4.0.)

R's Rmath.h provides fmax2 fmin2 fround ftrunc gammafn : please use
them as appropriate. Also, erf can be replaced by a call to R's pnorm (the translation is in the R help file for pnorm).

C++98's floor has a method for floats, so floorf is not needed in C++ .

Constants M_LN2 (gmp), M_SQRT2 (mixAK) and M_PI4 (simLife) are not
C++98. You can get them by including Rmath.h (or copy them from there).

For wtcrsk, isnan is not C++98, but R provides ISNAN for use in C++.

For SSN, strdup is a GNU/POSIX extension to C and not in any C++
standard hence not declared in according to the standard.
Substitutes are widely available: we suggest you use one.

For the rest, the best solution would seem to be to declare that C++11
is used: see 'Writing R Extensions'.

Please submit a corrected update, preferably within 4 weeks.

Manual style pre processing

For example

iris %>%
  span(group_by, class = "bg_yellow" )( Species ) %>%
  summarise_all( mean )

would apply the css class bg_yellow to group_by and make span disappear of the call tree

Compilation failure on CentOS

Hi Romain,

I'm having trouble getting highlight to install on the Harvard HPC cluster (CentOS):

Installing highlight
'/home/XXX/miniconda3/lib/R/bin/R' --no-site-file --no-environ --no-save  \
  --no-restore --quiet CMD INSTALL  \
  '/tmp/RtmpKed5AP/devtoolsda844bf8257/romainfrancois-highlight-b888782'  \
  --library='/home/XXX/R/library' --install-tests 

* installing *source* package ‘highlight’ ...
** libs
g++ -std=c++11 -I/home/XXX/miniconda3/lib/R/include -DNDEBUG  -I/home/XXX/miniconda3/include   -std=c++11 -fpic  -I/home/XXX/miniconda3/include -c ASBeautifier.cpp -o ASBeautifier.o
g++ -std=c++11 -I/home/XXX/miniconda3/lib/R/include -DNDEBUG  -I/home/XXX/miniconda3/include   -std=c++11 -fpic  -I/home/XXX/miniconda3/include -c ASEnhancer.cpp -o ASEnhancer.o
g++ -std=c++11 -I/home/XXX/miniconda3/lib/R/include -DNDEBUG  -I/home/XXX/miniconda3/include   -std=c++11 -fpic  -I/home/XXX/miniconda3/include -c ASFormatter.cpp -o ASFormatter.o
g++ -std=c++11 -I/home/XXX/miniconda3/lib/R/include -DNDEBUG  -I/home/XXX/miniconda3/include   -std=c++11 -fpic  -I/home/XXX/miniconda3/include -c ASResource.cpp -o ASResource.o
g++ -std=c++11 -I/home/XXX/miniconda3/lib/R/include -DNDEBUG  -I/home/XXX/miniconda3/include   -std=c++11 -fpic  -I/home/XXX/miniconda3/include -c ASStreamIterator.cpp -o ASStreamIterator.o
In file included from ASStreamIterator.cpp:2:0:
ASStreamIterator.h:31:20: fatal error: astyle.h: No such file or directory
 #include <astyle.h>
                    ^
compilation terminated.
make: *** [ASStreamIterator.o] Error 1
ERROR: compilation failed for package ‘highlight’
* removing ‘/home/XXX/R/library/highlight’
Installation failed: Command failed (1)

Here's my ~/.R/Makevars:

CC=gcc -std=gnu99
CXX=g++
PKG_CXXFLAGS=-std=c++11

Any thoughts on how to get this to compile? Thanks!

Inconsistent behaviour between `lestrade` and `sherlock`

highlight::sherlock returns a new function, whereas highlight::lestrade returns data(?).

This behavour is inconsistent (and also means the sherlock example in README.md is incorrect):

# Works
highlight::highlight(file = "test.R", detective = highlight::lestrade)

# Doesn't work
highlight::highlight(file = "test.R", detective = highlight::sherlock)
#> Error in UseMethod("filter_") : 
#>   no applicable method for 'filter_' applied to an object of class "function"

# Works
highlight::highlight(file = "test.R", detective = highlight::sherlock())

Another top-level function which sets boxes=TRUE

There is no way to pass options to a vignette driver.

I just discussed this with Duncan, he suggested writing a new driver as in Section 1.4.2 of Writing R Extensions. But the ::: issue rears its head again: I can't just wrap the highest level function from highlight as it needs internal functions from the package which are not exported. And which I can't use in another package.

Can you add something, say, highlightWithBoxes(), as a high-level driver which just sets it to true?

I do know about redefining Hchunk but it seems silly to copy that to Rnw file after Rnw file.

Ability to add a link

In the data frame that the detective makes, add an href column, that is picked up in the .cpp code to generate a <a>.

This is a better workaround than #16

Where are CRAN releases coming from?

The last documented release in NEWS is 0.4.5 from 2015-04-15. Then 0.4.7 a few days later on CRAN (though not documented).

Since April 2015, there have been 3 additional releases: main page, archive

  • 0.4.7.1 - 2017-03-21
  • 0.4.7.2 - 2017-10-04
  • 0.4.7.3 - 2019-12-01 (this week!)

This package has ORPHANED status, and the repo hasn't had commits since Feb 26, 2018. Are you pushing these releases @romainfrancois? It seems kind of scary that the package is getting updated with no information about what is changing.

It also appears related to this issue that someone opened this week for pkgdown: r-lib/pkgdown#1192

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.