[R] R-code in html help pages: syntax highlighting

Romain Francois romain.francois at dbmail.com
Tue Mar 17 09:43:16 CET 2009


Wacek Kusnierczyk wrote:
> Romain Francois wrote:
>   
>> Duncan Murdoch wrote:
>>
>> That is an issue. I guess I will fall back on what the parser says and
>> infer on the scoping. Within the lines below, mean would be different
>> each time
>>
>> mean( 1:10 )
>> lapply( 1:10, mean)
>> mean <- (1+4) / 2
>> lapply( list( mean, median), function( f ) f( 1:10) )
>> { mean <- median; mean( 1:10 ) }
>>
>>     
>>> So if you've got high standards, it's probably quite hard.  On the
>>> other hand, if you're willing to accept the usual sort of errors that
>>> syntax highlighters make, it's not so bad, but not trivial.
>>>       
>
> it would be good to know what you mean by 'errors'.  syntax highlighters
> are, in principle, *syntax* highlighters.  in the example above, the
> first and sixth occurrences of 'mean' are in the operator position, the
> others are not -- would it be *syntactically* inappropriate to
> color-code the former in one way, and the latter in another?  
It would certainly be useful to make a difference between the first one 
(call to the mean function from base) and the last one.

If we have sufficient evidence of where the function lives, it would be 
useful for the syntax highlighter to present this evidence.

I'd like to see function highlighted differently for:
- function in standard R packages
- function exported from the NAMESPACE of the package being highlighted
- function not exported from the NAMESPACE
- function which belongs to a package on which this one depends
 
It makes the job trivial if we just don't care and put all function 
calls to the same basket.

But because of scoping rules, and tricks, there are going to be errors.

f <- function( ){
    assign( "mean", median )
    mean( 1:10)
}

f <- function( ){
    assign( paste("m", "e", "a", "n", sep = ""), median )
    mean( 1:10)
}

f <- function( ){
    eval( parse( text = 'assign( paste("m", "e", "a", "n", sep = ""), 
median )') )
    mean( 1:10)
}

... and so on.


> why should
> a *syntax* highlighter care about what's the object referred to by
> 'mean' in this or other context?
>
> otherwise, it would certainly be fun to have a semantic highlighter for r.
>
> (..)
>
>   
>>     
>>> (I'd guess it'll be fast enough: Brian Ripley reported that all the R
>>> code he wrote for conversions in R-devel was faster than the Perl
>>> code it was replacing.)
>>>       
>
> the 'benchmark' you are talking about is only as good as the perl
> scripts were.  i have had a look at a couple of perl scripts in the r
> sources, and it seemed like there was quite some space for improvement.
>
>
> vQ
>
>
>   


-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr




More information about the R-help mailing list