[R] Line Directives in Sweave Document

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Jun 28 17:05:55 CEST 2023


On 28/06/2023 1:39 a.m., Iris Simmons wrote:
> Hello,
> 
> 
> I'm trying to demonstrate the behaviour of my R package and how line
> directives change that behaviour. So I've got an R chunk like this:
> 
> <<keep.source=TRUE>>=
> {
> #line 1 "file1.R"
>      fun <- function() {
>          pkg::fun()
>      }
> 
> 
> #line 1 "file2.R"
>      fun()
> }
> @
> 
> but when it is rendered, the line directives do not appear:
> 
>> {
> +     fun <- function() {
> +         pkg::fun()
> +     }
> +
> +
> +     fun()
> + }
> 
> I tried faking a line directive by writing it twice:
> 
> <<keep.source=TRUE>>=
> {
>   #line 1 "file1.R"
> #line 1 "file1.R"
>      fun <- function() {
>          pkg::fun()
>      }
> 
> 
>   #line 1 "file2.R"
> #line 1 "file2.R"
>      fun()
> }
> @
> 
> but then the rendered fake line directive looks bad because it's out
> by one space so if it is copy-pasted, it wouldn't actually function as
> shown.
> 
> So is there an option I can set to have the line directives displayed,
> or something else I can change to get it working? Thank you!

Maybe if you put a zero-width space before the fake version, instead of 
a regular space?  That's \u200B in Unicode; there are various other 
encodings of it.  Put it after the # so that it isn't a syntax error, i.e.

#\u200Bline 1 "file1.R"

or if that shows the escape, print that string and copy the actual 
character into your document.

Duncan Murdoch



More information about the R-help mailing list