title: Processing example files with roxygen2: backslashes are duplicated (\dontrun becomes \\dontrun)
body: <h2>Actual question</h2>
  
  <p>How can I avoid that <code>\dontrun{</code> in a <strong>separate file</strong> containing examples becomes <code>\\dontrun{</code> in the respective Rd file after roxygenizing? </p>
  
  <p>I did find a workaround, but feel as if I'm maybe just missing something obvious, i.e. some setting of <code>roxigenize()</code>.</p>
  
  <h2>Details</h2>
  
  <p>I think I noticed a possible bug or, IMHO, at least an undesired behavior when processing examples with <a href="http://cran.r-project.org/web/packages/roxygen2/index.html">roxygen2</a> that are stored in a <strong>separate file</strong> (as opposed to stating it right within the actual roxygen code).</p>
  
  <p>The issue is that the line <code>\dontrun{</code> in the respective example files becomes <code>\\dontrun{</code> in the resulting Rd file after roxygenizing.</p>
  
  <p>Below you'll find an illustration of the behavior along with a simple workaround </p>
  
  <h2>1) Ensure directories</h2>
  
  <pre><code>dir.create("src", recursive=TRUE, showWarnings=FALSE)
  dir.create("package", recursive=TRUE, showWarnings=FALSE)
  
  # Ensure empty package directory
  subdirs &lt;- list.files("package", full.names=TRUE)
  if (length(subdirs)) {
      sapply(subdirs, unlink, recursive=TRUE)
  }
  </code></pre>
  
  <h2>2) Create example functions with two different ways of embedding examples</h2>
  
  <pre><code>foo1 &lt;- function(x) {message("I'm foo #1"); return(TRUE)}
  roxy.1 &lt;- c(
      "#' Title foo1()",
      "#'", 
      "#' Description foo1().",
      "##' This line is commented out",
      "#'", 
      "#' @param x Some R object that doesn't matter.",
      "#' @return \\code{TRUE}.",
      "#' @references \\url{http://www.something.com/}",
      "#' @author John Doe \\email{john.doe@@something.com}",
      "#' @seealso \\code{\\link{foo2}}",
      "#' @example inst/examples/foo1.R"
  )
  ex.1 &lt;- c(
      "\\dontrun{",
      "foo1()",
      "}"
  )
  
  foo2 &lt;- function(y) {message("I'm foo #2"); return(FALSE)}
  roxy.2 &lt;- c(
      "#' Title foo2()",
      "#'", 
      "#' Description foo2().",
      "##' This line is commented out",
      "#'", 
      "#' @param y Some R object that doesn't matter.",
      "#' @return \\code{FALSE}.",
      "#' @references \\url{http://www.something.com/}",
      "#' @author John Doe \\email{john.doe@@something.com}",
      "#' @seealso \\code{\\link{foo1}}",
      "#' @examples", 
      "#' \\dontrun{",
      "#' foo2()}",
      "#' }"
  )
  
  write(roxy.1, file="src/foo1.R")
  write(c("foo1 &lt;-", deparse(foo1)), file="src/foo1.R", append=TRUE)
  write(roxy.2, file="src/foo2.R")
  write(c("foo2 &lt;-", deparse(foo2)), file="src/foo2.R", append=TRUE)
  </code></pre>
  
  <h2>3) Create package skeleton</h2>
  
  <pre><code>package.skeleton(name="test", path="package", 
      code_files=c("src/foo1.R", "src/foo2.R"))
  </code></pre>
  
  <h2>4) Create separate example file for <code>foo1()</code></h2>
  
  <pre><code>dir.create("package/test/inst/examples", recursive=TRUE, showWarnings=FALSE)
  write(ex.1, file="package/test/inst/examples/foo1.R")
  </code></pre>
  
  <h2>5) Roxygenize</h2>
  
  <pre><code>require("roxygen2")
  roxygenize(
      package.dir="package/test",
      overwrite=TRUE, 
      unlink.target=FALSE,
      roclets = c("collate", "namespace", "rd")
  )
  </code></pre>
  
  <h2>6) Check package</h2>
  
  <pre><code>shell("R CMD check package/test", intern=FALSE)
  </code></pre>
  
  <p>Truncated output of R CMD check which shows that there's a problem with <code>\dontrun{</code> in <code>./package/test/man/foo1.Rd</code></p>
  
  <pre><code>[...]
  Warning: parse error in file 'test-Ex.R':
  1: unexpected input
  19: 
  20: \
      ^
  * checking examples ... ERROR
  Running examples in 'test-Ex.R' failed
  The error most likely occurred in:
  
  &gt; ### Name: foo1
  &gt; ### Title: Title foo1()
  &gt; ### Aliases: foo1
  &gt; 
  &gt; ### ** Examples
  &gt; 
  &gt; \dontrun{
  Error: unexpected input in "\"
  Execution halted
  Warning message:
  In shell(expr, intern = FALSE) :
    'R CMD check package/test' execution failed with error code 1
  &gt; 
  </code></pre>
  
  <h2>7) Workaround</h2>
  
  <pre><code>patchRdFiles &lt;- function( 
      path="package",
      name,
      ...
  ) {
      path &lt;- file.path(path, name, "man")
      if (!file.exists(path)) {
          stop(paste("Invalid directory path: '", path, "'", sep=""))
      }
      files &lt;- list.files(path, full.names=TRUE)  
  #ii=files[1]    
      .dead &lt;- sapply(files, function(ii) {
          cnt &lt;- readLines(ii, warn=FALSE)
          if (length(grep("\\\\\\\\dontrun", cnt, perl=TRUE))) {
              message(paste("Correcting: '", ii, "'", sep=""))
              write(gsub("\\\\dontrun", "\\dontrun", cnt), file=ii)
          }
          return(NULL)
      })
      return(TRUE)
  }
  </code></pre>
  
  <p>This will remove all duplicated backslashes in Rd files:</p>
  
  <pre><code>patchRdFiles(name="test")
  </code></pre>
  
  <p>8) Checking package again</p>
  
  <pre><code># CHECK PACKAGE AGAIN
  path &lt;- "package/test"
  expr &lt;- paste("R CMD check", path)
  shell(expr, intern=FALSE)
  </code></pre>
  
  <p>Again the truncated output of R CMD check. The Rd file in question passed the check now. The current error is caused by an incomplete <code>./package/test/man/test-package.Rd</code>, which is fine at this point</p>
  
  <pre><code>[...]
  Warning: parse error in file 'test-Ex.R':
  11: unexpected symbol
  56: 
  57: ~~ simple examples
                ^
  * checking examples ... ERROR
  Running examples in 'test-Ex.R' failed
  The error most likely occurred in:
  
  &gt; ### Name: test-package
  &gt; ### Title: What the package does (short line) ~~ package title ~~
  &gt; ### Aliases: test-package test
  &gt; ### Keywords: package
  &gt; 
  &gt; ### ** Examples
  &gt; 
  &gt; ~~ simple examples of the most important functions ~~
  Error: unexpected symbol in "~~ simple examples"
  Execution halted
  Warning message:
  In shell(expr, intern = FALSE) :
    'R CMD check package/test' execution failed with error code 1
  &gt; 
  </code></pre>