当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。