當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R reprex reprex_document 代表輸出格式

這是專門為製作 "reprexes" 而設計的 R Markdown 輸出格式,通常通過 reprex() 函數創建,最終使用 reprex_render() 呈現文檔。它是 rmarkdown::md_document() 的大幅修改版本。這些論點有不同的影響範圍:

  • venue 可能會影響輸入準備和 reprex_render()

  • 在渲染之前將內容添加到主輸入:

  • 影響 knit 包或塊選項:

    • style

    • comment

    • tidyverse_quiet

RStudio 用戶可以使用內置模板創建 reprex_document() 格式的新 R Markdown 文檔。執行“文件”>“新建文件”>“R Markdown ...”>“從模板”,然後選擇以下選項之一:

  • 代表(最小)

  • reprex(很多函數)

兩者都在 YAML 中包含 knit: reprex::reprex_render ,這會導致 RStudio "Knit" 按鈕使用 reprex_render() 。如果您自己提交這些文件,您也應該這樣做。

用法

reprex_document(
  venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"),
  advertise = NULL,
  session_info = opt(FALSE),
  style = opt(FALSE),
  comment = opt("#>"),
  tidyverse_quiet = opt(TRUE),
  std_out_err = opt(FALSE),
  pandoc_args = NULL
)

參數

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" 的別名。

advertise

邏輯性強。是否包含說明 reprex 創建時間和方式的頁腳。如果未指定,則參考選項 reprex.advertise,如果未定義,則默認為 TRUE(對於 "gh""html""so""ds" );且 FALSE 對應 "r""rtf""slack"

session_info

邏輯性強。是否在表示末尾包含 sessioninfo::session_info() (如果可用)或 sessionInfo() 。當venue 為"gh" 時,會話信息包含在可折疊詳細信息標簽中。閱讀有關 opt() 的更多信息。

style

邏輯性強。是否設置knitr chunk選項tidy = "styler",它使用styler package重新設置代碼樣式。閱讀有關 opt() 的更多信息。

comment

特點。用於注釋輸出的前綴,默認為 "#>" 。閱讀有關 opt() 的更多信息。

tidyverse_quiet

邏輯性強。設置選項 tidyverse.quiettidymodels.quiet ,它們抑製( TRUE ,默認值)或包含( FALSE ) tidyverse 和 tidymodels 包的啟動消息。閱讀有關 opt() 的更多信息。

std_out_err

邏輯性強。是否為 reprex 渲染進程發送到 stdout 和 stderr 的輸出附加一個部分。如果 reprex 生成子進程或 system() 調用,這可能是顯示輸出所必需的。請注意,這不能與主 R 進程的輸出正確交錯,也不能保證標準輸出和標準錯誤中的行按正確的時間順序排列。有關更多信息,請參閱callr::r()。閱讀有關 opt() 的更多信息。

pandoc_args

傳遞給 pandoc 的附加命令行選項

要傳遞給 rmarkdown::render() 的 R Markdown 輸出格式。

例子

reprex_document()
#> $knitr
#> $knitr$opts_chunk
#> $knitr$opts_chunk$dev
#> [1] "png"
#> 
#> $knitr$opts_chunk$dpi
#> [1] 96
#> 
#> $knitr$opts_chunk$fig.width
#> [1] 7
#> 
#> $knitr$opts_chunk$fig.height
#> [1] 5
#> 
#> $knitr$opts_chunk$fig.retina
#> NULL
#> 
#> $knitr$opts_chunk$collapse
#> [1] TRUE
#> 
#> $knitr$opts_chunk$error
#> [1] TRUE
#> 
#> $knitr$opts_chunk$comment
#> [1] "#>"
#> 
#> $knitr$opts_chunk$R.options
#> $knitr$opts_chunk$R.options$tidyverse.quiet
#> [1] TRUE
#> 
#> $knitr$opts_chunk$R.options$tidymodels.quiet
#> [1] TRUE
#> 
#> 
#> 
#> $knitr$opts_knit
#> $knitr$opts_knit$upload.fun
#> function (file, key = "9f3460e67f308f6") 
#> {
#>     if (!is.character(key)) 
#>         stop("The Imgur API Key must be a character string!")
#>     resp = httr::POST("https://api.imgur.com/3/image.xml", config = httr::add_headers(Authorization = paste("Client-ID", 
#>         key)), body = list(image = httr::upload_file(file)))
#>     httr::stop_for_status(resp, "upload to imgur")
#>     res = httr::content(resp, as = "raw")
#>     res = if (length(res)) 
#>         xml2::as_list(xml2::read_xml(res))
#>     if (packageVersion("xml2") >= "1.2.0") 
#>         res <- res[[1L]]
#>     if (is.null(res$link[[1]])) 
#>         stop("failed to upload ", file)
#>     structure(res$link[[1]], XML = res)
#> }
#> <bytecode: 0x55f8f8e9e1d8>
#> <environment: namespace:knitr>
#> 
#> 
#> $knitr$knit_hooks
#> NULL
#> 
#> $knitr$opts_hooks
#> NULL
#> 
#> $knitr$opts_template
#> NULL
#> 
#> 
#> $pandoc
#> $pandoc$to
#> [1] "gfm-yaml_metadata_block"
#> 
#> $pandoc$from
#> [1] "markdown+autolink_bare_uris+tex_math_single_backslash-implicit_figures"
#> 
#> $pandoc$args
#> [1] "--wrap=preserve"
#> 
#> $pandoc$keep_tex
#> [1] FALSE
#> 
#> $pandoc$latex_engine
#> [1] "pdflatex"
#> 
#> $pandoc$ext
#> [1] ".md"
#> 
#> 
#> $keep_md
#> [1] FALSE
#> 
#> $clean_supporting
#> [1] FALSE
#> 
#> $df_print
#> [1] "default"
#> 
#> $pre_knit
#> function (input, ...) 
#> {
#>     knit_input <- sub("[.]R$", ".spin.Rmd", input)
#>     input_lines <- read_lines(knit_input)
#>     input_lines <- c(rprofile_alert(venue), "", input_lines)
#>     input_lines <- c(reprex_opts(venue), "", input_lines)
#>     if (isTRUE(advertise)) {
#>         input_lines <- c(input_lines, "", ad(venue))
#>     }
#>     if (isTRUE(std_out_err)) {
#>         input_lines <- c(input_lines, "", std_out_err_stub(input, 
#>             venue))
#>     }
#>     if (isTRUE(session_info)) {
#>         input_lines <- c(input_lines, "", si(venue))
#>     }
#>     write_lines(input_lines, knit_input)
#> }
#> <bytecode: 0x55f8f95291c8>
#> <environment: 0x55f8f952c6a0>
#> 
#> $post_knit
#> NULL
#> 
#> $pre_processor
#> NULL
#> 
#> $intermediates_generator
#> NULL
#> 
#> $post_processor
#> NULL
#> 
#> $on_exit
#> function () 
#> {
#>     if (is.function(base)) 
#>         base()
#>     if (is.function(overlay)) 
#>         overlay()
#> }
#> <bytecode: 0x55f8f8f73350>
#> <environment: 0x55f8f8f72e10>
#> 
#> attr(,"class")
#> [1] "rmarkdown_output_format"

相關用法


注:本文由純淨天空篩選整理自Jennifer Bryan等大神的英文原創作品 reprex output format。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。