從野外捕獲的 reprex 中恢複幹淨、可運行的代碼並將其寫入用戶的剪貼板。代碼也會以不可見的方式返回,並可選擇寫入文件。三個不同的函數可解決各種形式的 wild-caught 表示。
用法
reprex_invert(
input = NULL,
wd = NULL,
venue = c("gh", "r"),
comment = opt("#>"),
outfile = deprecated()
)
reprex_clean(
input = NULL,
wd = NULL,
comment = opt("#>"),
outfile = deprecated()
)
reprex_rescue(
input = NULL,
wd = NULL,
prompt = getOption("prompt"),
continue = getOption("continue"),
outfile = deprecated()
)
參數
- input
-
特點。如果長度為 1 並且缺少終止換行符,則解釋為包含 reprex 的文件的路徑。否則,假設將 reprex 保存為字符向量。如果未提供,則參考剪貼板進行輸入。如果剪貼板不可用並且我們在 RStudio 中,則使用當前選擇。
- wd
-
當
input
不是文件路徑時參考的可選文件路徑。 (默認情況下,所有工作都在會話臨時目錄的子目錄中悄悄完成。)wd
最常見的用法是設置wd = "."
,這意味著“在當前工作目錄中代表正確的位置”。如果您確實必須使用本地文件演示某些內容,請執行此操作。 - venue
-
特點。必須是以下之一(不區分大小寫):
-
"gh" 代表 GitHub-Flavored Markdown ,默認值
-
"r" 用於可運行的 R 腳本,帶注釋的輸出交錯。對於 Slack code snippets 也很有用;從 "Type" 下拉菜單中選擇 "R" 以享受漂亮的語法突出顯示。
-
Rich Text Format 的 "rtf"(un-reprexing 不支持)
-
"html" 適用於適合包含在較大 HTML 文檔中的 HTML 片段(不支持 un-reprexing)
-
"slack" 用於粘貼到 Slack 消息中。針對選擇退出 Slack 所見即所得接口的用戶進行了優化。轉到首選項 > 高級 > 輸入選項,然後選擇“使用標記格式化消息”。 (如果需要針對所見即所得進行優化的第二個 Slack 場地,請提出問題進行討論。)
-
"so"為Stack Overflow Markdown。注意:這隻是 "gh" 的別名,因為 Stack Overflow 於 2019 年 1 月開始支持 CommonMark 風格的隔離代碼塊。
-
"ds" 用於話語,例如 community.rstudio.com 。注意:這目前隻是 "gh" 的別名。
-
- comment
-
匹配注釋輸出行的正則表達式
- outfile
-
有利於
wd
或提供文件路徑input
。要在當前工作目錄中表示,請使用wd = "."
現在,而不是outfile = NA
. - prompt
-
字符,R 命令開頭的提示符
- continue
-
字符,續行提示符
職能
-
reprex_invert()
:嘗試逆轉reprex()
的效果。當venue = "r"
時,這隻是調用reprex_clean()
。 -
reprex_clean()
:假設 R 代碼是頂級的,可能與注釋輸出交錯,例如從 GitHub 複製的顯示表示或reprex(..., venue = "R")
的直接輸出。此函數刪除注釋輸出。 -
reprex_rescue()
:假設 R 代碼行以提示開頭,並且打印輸出是頂級的,例如,您從 R 控製台複製/粘貼得到的內容。刪除輸出行並從包含 R 命令的行中刪除提示。
例子
if (FALSE) {
# a roundtrip: R code --> rendered reprex, as gfm --> R code
original <- file.path(tempdir(), "original.R")
writeLines(glue::glue("
#' Some text
#+ chunk-label-and-options-cannot-be-recovered, message = TRUE
(x <- 1:4)
#' More text
y <- 2:5
x + y"), con = original)
reprex(input = original, html_preview = FALSE, advertise = FALSE)
reprexed <- sub("[.]R$", "_reprex.md", original)
writeLines(readLines(reprexed))
unreprexed <- reprex_invert(input = reprexed)
writeLines(unreprexed)
# clean up
file.remove(
list.files(dirname(original), pattern = "original", full.names = TRUE)
)
}
if (FALSE) {
# a roundtrip: R code --> rendered reprex, as R code --> original R code
code_in <- c(
"# a regular comment, which is retained",
"(x <- 1:4)",
"median(x)"
)
reprexed <- reprex(input = code_in, venue = "r", advertise = FALSE)
writeLines(reprexed)
code_out <- reprex_clean(input = reprexed)
writeLines(code_out)
identical(code_in, code_out)
}
if (FALSE) {
# rescue a reprex that was copied from a live R session
from_r_console <- c(
"> # a regular comment, which is retained",
"> (x <- 1:4)",
"[1] 1 2 3 4",
"> median(x)",
"[1] 2.5"
)
rescued <- reprex_rescue(input = from_r_console)
writeLines(rescued)
}
相關用法
- R reprex reprex_render 在新的 R 會話中渲染文檔
- R reprex reprex 渲染一個reprex
- R reprex reprex_locale 在特定區域設置中渲染 reprex
- R reprex reprex_document 代表輸出格式
- R SparkR repartition用法及代碼示例
- R SparkR repartitionByRange用法及代碼示例
- R SparkR read.stream用法及代碼示例
- R readr datasource 創建源對象。
- R readr melt_delim 返回分隔文件中每個標記的熔化數據(包括 csv 和 tsv)
- R readr read_rds 讀/寫 RDS 文件。
- R readr read_lines 從文件中讀取/寫入行
- R readr parse_number 靈活地解析數字
- R SparkR refreshByPath用法及代碼示例
- R readr read_fwf 將固定寬度文件讀入 tibble
- R readxl read_excel 讀取 xls 和 xlsx 文件
- R SparkR refreshTable用法及代碼示例
- R readr read_builtin 從包中讀取內置對象
- R readr Tokenizers 分詞器。
- R readr melt_table 返回空格分隔文件中每個標記的熔化數據
- R readr date_names 創建或檢索日期名稱
- R SparkR read.jdbc用法及代碼示例
- R readr type_convert 重新轉換現有 DataFrame 中的字符列
- R readxl excel_sheets 列出 Excel 電子表格中的所有工作表
- R readxl readxl_example 獲取 readxl 示例的路徑
- R readxl excel_format 確定文件格式
注:本文由純淨天空篩選整理自Jennifer Bryan等大神的英文原創作品 Un-render a reprex。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。