[Rd] \Sexpr[results=hide] produces \verb{ newlines }

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Jul 29 17:48:38 CEST 2021


Hello R-devel!

Here's an Rd file that produces a large empty area when converted to
HTML:

\name{repro}
\title{title}
\description{description}
\details{
  Hello
  \Sexpr[stage=build,results=hide]{
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    invisible(NULL)
    "" # workaround: remove results=hide and use the return value
  }
}

This seems to happen because \Sexpr gets expanded to \verb{ as many
newlines as there were code lines } by processRdChunk, by first storing
a newline for each line of the code:

https://github.com/wch/r-source/blob/d7a4ed9aaeee1f57c3c165aefa08b8d69dfe59fa/src/library/tools/R/RdConv2.R#L257

...and then the newlines get translated to \verb because res is
not empty:

https://github.com/wch/r-source/blob/d7a4ed9aaeee1f57c3c165aefa08b8d69dfe59fa/src/library/tools/R/RdConv2.R#L332

As long as Rd above doesn't stem from my misuse of \Sexpr, I would like
to propose the following patch, which seems to fix the problem:

Index: src/library/tools/R/RdConv2.R
===================================================================
--- src/library/tools/R/RdConv2.R	(revision 80675)
+++ src/library/tools/R/RdConv2.R	(working copy)
@@ -329,6 +329,8 @@
 	    }
 	} else if (options$results == "text")
 	    res <- tagged(err, "TEXT")
+	else if (options$results == "hide")
+	    res <- tagged("", "COMMENT")
 	else if (length(res)) {
 	    res <- lapply(as.list(res), function(x) tagged(x, "VERB"))
 	    res <- tagged(res, "\\verb")

There are probably other ways of fixing this problem, e.g. by only
populating res if options$results != "hide" or only appending newlines
if res is non-empty.

-- 
Best regards,
Ivan



More information about the R-devel mailing list