rroxygen2rd

generating .R file from .Rd file


I know roxygen2::roxygenise() or devtools::document() cane be used to generate .Rd file from .R file.

For example, if Test.R is as follow

#' Add together two numbers
#'
#' @param x A number
#' @param y A number
#' @return The sum of \code{x} and \code{y}
#' @examples
#' add(1, 1)
#' add(10, 1)
add <- function(x, y) {
  x + y
}

Then the output of roxygen2::roxygenise() or devtools::document() will be

% Generated by roxygen2 (3.2.0): do not edit by hand
\name{add}
\alias{add}
\title{Add together two numbers}
\usage{
add(x, y)
}
\arguments{
  \item{x}{A number}

  \item{y}{A number}
}
\value{
The sum of \code{x} and \code{y}
}
\description{
Add together two numbers
}
\examples{
add(1, 1)
add(10, 1)
}

However, I'm interested in vice versa. I do have .Rd which I want to convert into .R files. Any thoughts!


Solution

  • The Rd2roxygen package will do this for you:

    It can parse an Rd file to a list, create the 'roxygen' documentation and update the original R script (e.g. the one containing the definition of the function) accordingly.

    It's on CRAN; the author's documentation page is here.

    To convert a single Rd file you'd run

    library(Rd2roxygen)
    create_roxygen(parse_file("my_rd_file.Rd"))
    

    which would return the results as a character vector (writeLines() to save it to a file).