从 .Rd 文件生成 .R 文件

generating .R file from .Rd file

我知道 roxygen2::roxygenise()devtools::document() 可以用来从 .R 文件生成 .Rd 文件.

例如Test.R如下

#' 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
}

那么roxygen2::roxygenise()devtools::document()的输出将是

% 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)
}

不过,我对反之亦然感兴趣。我确实有 .Rd,我想将其转换为 .R 文件。有什么想法!

Rd2roxygen 软件包将为您完成此操作:

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.

on CRAN; the author's documentation page is here

要转换单个 Rd 文件,您需要 运行

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

这会将 return 结果作为字符向量(writeLines() 将其保存到文件)。