簡體   English   中英

從r包中提取文檔

[英]Extract documentation from an r package

我正在嘗試編寫一個R函數,該函數將從R包中提取導出的函數,並返回一個列表,其中包含每個函數,調用簽名以及文檔中的描述。

通過執行以下操作,可以相對輕松地獲取導出功能的列表:

# Extracts all exported function names from dplyr
library(dplyr) 
lsf.str("package:dplyr")

我還可以使用名稱獲取給定函數的調用簽名:

# Extract call signature for the join function in dplyr
lsf.str("package:dplyr")[10] %>%
  get() %>%
  deparse() %>%
  head(1)

我什至可以為每個功能自動打開幫助部分:

# Opens help for the join function in dplyr
help(lsf.str("package:dplyr")[10], package = "dplyr")

但是,如何返回一個包含該功能描述文本的字符串? 因此,對於dplyr中的join函數,它應該返回:

這些是分派給單個tbl方法的通用函數-有關單個數據源的詳細信息,請參見方法文檔。 x和y通常應來自同一數據源,但是如果copy為TRUE,則y將自動復制到與x相同的數據源。

有任何想法嗎?

library(tidyverse)

lsf.str("package:dplyr")[10] %>% 
  help("dplyr") %>%
  utils:::.getHelpFile() %>% 
  keep(~attr(.x, "Rd_tag") == "\\description") %>% 
  map(as.character) %>% 
  flatten_chr() %>% 
  paste0(., collapse="")
## [1] "\nThese are generic functions that dispatch to individual tbl methods - see the\nmethod documentation for details of individual data sources. x and\ny should usually be from the same data source, but if copy is\nTRUE, y will automatically be copied to the same source as x.\n"

刪除換行符是OP的一項練習:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM