Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code/output blocks are split #463

Closed
florisvdh opened this issue May 29, 2024 · 6 comments
Closed

Code/output blocks are split #463

florisvdh opened this issue May 29, 2024 · 6 comments

Comments

@florisvdh
Copy link

Running the following in a bare R console:

reprex::reprex(
  {
    1:4
    2:5
  },
  session_info = TRUE
)

gives:

1:4
#> [1] 1 2 3 4
2:5
#> [1] 2 3 4 5

Created on 2024-05-29 with reprex v2.1.0.9000

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.0 (2024-04-24)
#>  os       Linux Mint 21.3
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language nl_BE:nl
#>  collate  nl_BE.UTF-8
#>  ctype    nl_BE.UTF-8
#>  tz       Europe/Brussels
#>  date     2024-05-29
#>  pandoc   3.1.11 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.2      2023-12-11 [3] RSPM (R 4.3.0)
#>  digest        0.6.35     2024-03-11 [3] RSPM (R 4.3.0)
#>  evaluate      0.23       2023-11-01 [3] RSPM (R 4.3.0)
#>  fastmap       1.2.0      2024-05-15 [3] RSPM (R 4.4.0)
#>  fs            1.6.4      2024-04-25 [3] RSPM (R 4.3.0)
#>  glue          1.7.0      2024-01-09 [3] RSPM (R 4.3.0)
#>  htmltools     0.5.8.1    2024-04-04 [3] RSPM (R 4.3.0)
#>  knitr         1.46       2024-04-06 [3] RSPM (R 4.3.0)
#>  lifecycle     1.0.4      2023-11-07 [3] RSPM (R 4.3.0)
#>  magrittr      2.0.3      2022-03-30 [3] RSPM (R 4.2.0)
#>  purrr         1.0.2      2023-08-10 [3] RSPM (R 4.2.0)
#>  R.cache       0.16.0     2022-07-21 [3] RSPM (R 4.2.0)
#>  R.methodsS3   1.8.2      2022-06-13 [3] RSPM (R 4.2.0)
#>  R.oo          1.26.0     2024-01-24 [3] RSPM (R 4.3.0)
#>  R.utils       2.12.3     2023-11-18 [3] RSPM (R 4.3.0)
#>  reprex        2.1.0.9000 2024-05-29 [1] Github (tidyverse/reprex@e1f65e9)
#>  rlang         1.1.3      2024-01-10 [3] RSPM (R 4.3.0)
#>  rmarkdown     2.27.1     2024-05-29 [1] Github (rstudio/rmarkdown@e1c93a9)
#>  rstudioapi    0.16.0     2024-03-24 [3] RSPM (R 4.3.0)
#>  sessioninfo   1.2.2      2021-12-06 [3] RSPM (R 4.2.0)
#>  styler        1.10.3     2024-04-07 [3] RSPM (R 4.3.0)
#>  vctrs         0.6.5      2023-12-01 [3] RSPM (R 4.3.0)
#>  withr         3.0.0      2024-01-16 [3] RSPM (R 4.3.2)
#>  xfun          0.44       2024-05-15 [3] RSPM (R 4.4.0)
#>  yaml          2.3.8      2023-12-11 [3] RSPM (R 4.3.0)
#> 
#>  [1] /home/floris/lib/R/library
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Before, {reprex} would keep code and output together in one block. More recently though, R version and rmarkdown versions have changed.

Running knitr::opts_chunk$set(collapse = TRUE) didn't solve it.

@cderv
Copy link
Contributor

cderv commented May 30, 2024

This is an issue with knitr, it seems we missed a breakage for the collapse options

With knitr 1.46

> xfun::raw_string(knitr::knit(text = c("```{r, collapse=TRUE}\n1 + 1\n1:1\n```")))
                                                                                                                                                                

``` r
1 + 1
## [1] 2
```

``` r
1:1
## [1] 1
```

With knitr 1.45

> xfun::raw_string(knitr::knit(text = c("```{r, collapse=TRUE}\n1 + 1\n1:1\n```")))
                                                                                                                                                                

```r
1 + 1
## [1] 2
1:1
## [1] 1
```

I'll fix it on knitr side.

@jennybc
Copy link
Member

jennybc commented May 30, 2024

Thanks @cderv for tracking this down. I will close here and just wait for the fix to appear in a released version of knitr.

@jennybc jennybc closed this as completed May 30, 2024
@hadley
Copy link
Member

hadley commented Jun 14, 2024

I'm seeing this problem again in knitr 1.47:

> packageVersion('knitr')
[1] ‘1.47’
> xfun::raw_string(knitr::knit(text = c("```{r, collapse=TRUE}\n1 + 1\n1:1\n```")))
1/1 [unnamed-chunk-1]

``` r
1 + 1
## [1] 2
```

``` r
1:1
## [1] 1
```

@cderv
Copy link
Contributor

cderv commented Jun 14, 2024

It is fixed in current knitr dev version, which will be knitr 1.48. It has not been released to CRAN yet.

@yihui could you do a knitr release so that this is solved for reprex ?

@yihui
Copy link

yihui commented Jun 14, 2024

Can people wait for two more weeks? I've been exhausted recently. If there's a hurry, I can certainly make the release sooner, but CRAN just complained to me about my last release, so I tend not to make the next release too soon.

@hadley
Copy link
Member

hadley commented Jun 14, 2024

Yeah, no rush from my end.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Oct 26, 2024
(w3m -insecure -dump -T text https://github.com/yihui/knitr/releases)
knitr 1.48
BUG FIXES

  * Fix regression from 1.46 with collapse = TRUE option not correctly
    collapsing source code and output into one when code chunk returns multiple
    outputs (thanks, @jennybc, @florisvdh, tidyverse/reprex#463).

  * hook_purl() should not write the path of the R script to the output
    document (thanks, @fenguoerbian, #2348).

knitr 1.47
NEW FEATURES

  * For kable(), you can set the global option knitr.kable.max_rows to limit
    the number of rows to show in the table, e.g., options(knitr.kable.max_rows
    = 30). This is a way to prevent kable() from generating a huge table from a
    large data object by accident.

  * write_bib() now escapes all non-escaped "&" in the bibliography by default.
    Previously, it only escaped the title field of the package citation. You
    can disable the escape with the argument tweak = FALSE (thanks, @HedvigS #
    2335, @atusy #2342).

BUG FIXES

  * Fixed a bug that write_bib() fails to use the first URL of a package when
    multiple URLs are provided in DESCRIPTION and separated by \n (thanks,
    @bastistician, #2343).

MINOR CHANGES

  * The syntax highlighting LaTeX commands for Rnw documents, \hlstr and \
    hlstd, were renamed to \hlsng and \hldef, respectively, to maintain
    consistency with Andrew Simon's highlight package (thanks, @dcser123, #2341
    ).

knitr 1.46
NEW FEATURES

  * Added a new chunk option tab.cap to specify the table caption for kable()
    (thanks, @ulyngs, #1679). Previously, the caption could only be specified
    via the caption argument of kable(). Now you can set it in the chunk header
    if you want. Please note that this chunk option only works with a single
    kable() in each code chunk, and its value must be of length 1.

  * spin() now recognizes # %% as a valid code chunk delimiter (thanks,
    @kylebutts, #2307).

  * spin() also recognizes #| comments as code chunks now (thanks, @kylebutts,
    #2320).

  * Chunk hooks can have the ... argument now. Previously, only arguments
    before, options, envir, and name are accepted. If a chunk hook function has
    the ... argument, all the aforementioned four arguments are passed to the
    hook. This means the hook function no longer has to have the four arguments
    explicitly in its signature, e.g., function(before, ...) is also valid if
    only the before argument is used inside the hook. See https://yihui.org/
    knitr/hooks/#chunk-hooks for more information.

  * For package vignettes, PNG plots will be optimized by optipng and pngquant
    if they are installed and can be found from the system PATH variable. This
    can help reduce the package size if vignettes contain PNG plots (thanks,
    @nanxstats, https://nanx.me/blog/post/rpkgs-pngquant-ragg/).

BUG FIXES

  * spin() stopped working with input that cannot be parsed as R code due to #
    1605. Now it works again (thanks, @Hemken, #1773).

  * write_bib() generated empty entries for packages without URLs (thanks,
    @bastistician, #2304).

  * The family argument was not passed to the pdf device (thanks, @sebkopf,
    rstudio/rmarkdown#2526).

  * Trailing spaces escaped by \ should not be trimmed in kable() (thanks,
    @mjsmith037, #2308).

  * kable() fails when the value of the caption argument is of length > 1
    (thanks, @LeeMendelowitz, #2312).

  * include_graphics() may provide an incorrect plot width to LaTeX when the
    locale setting for LC_NUMERIC is not C because the decimal separator may
    not be a dot (thanks, @tdhock, rstudio/rmarkdown#2525).

  * When TinyTeX and the LaTeX package pdfcrop are installed, knitr::pdf_crop()
    is unable to find pdfcrop (thanks, @dmkaplan2000, rstudio/tinytex#435).

MAJOR CHANGES

  * Unbalanced chunk delimiters (fences) in R Markdown documents are no longer
    allowed, as announced two years ago at https://yihui.org/en/2021/10/
    unbalanced-delimiters/ (#2306). This means the opening delimiter must
    strictly match the closing delimiter, e.g., if a code chunk starts with
    four backticks, it must also end with four; or if a chunk header is
    indented by two spaces, the closing fence must be indented by exactly two
    spaces. For authors who cannot update their R Markdown documents for any
    reason at the moment, setting options(knitr.unbalanced.chunk = TRUE) (e.g.,
    in .Rprofile) can temporarily prevent knitr from throwing an error, but it
    is strongly recommended that you fix the problems as soon as possible,
    because this workaround will be removed in future.

  * Package vignettes are tangled by default during R CMD check, per request
    from CRAN maintainers (d0d1b47). The consequence is that R CMD check will
    check R scripts tangled from vignettes by default, unless you set the
    environment variable _R_CHECK_VIGNETTES_SKIP_RUN_MAYBE_=true. Previously,
    knitr would skip tangling vignettes during R CMD check, because R scripts
    tangled from vignettes are not guaranteed to valid. With the skip undone, R
    CMD check may fail in places other than CRAN (because CRAN has set the
    environment variable).

MINOR CHANGES

  * Fixed broken vignettes, improved CSS for HTML vignettes, and reduced the
    file sizes.

  * SQL code chunks that run ALTER statements are only executed and not tried
    to fecth a result (thanks, @maxschmi, #2330).

  * The function imgur_upload() has been moved to (and enhanced in) the xfun
    package as xfun::upload_imgur() so it is no longer tied to knitr and can be
    reused by other pakages. Now knitr::imgur_upload() is only a wrapper
    function of xfun::upload_imgur(). You are recommended to use the latter (#
    2325).

  * spin() dropped support for #- as the chunk delimiter token. Please use #+
    or # %% or #| instead.

  * Faster processing of cache dependencies in dep_auto() (thanks, @knokknok, #
    2318).

  * Removed some S3 methods that are used internally and changed them to normal
    functions: print.block -> print_block, print.inline -> print_inline,
    process_group.block/process_group.inline -> process_group, and
    process_tangle.block/process_tangle.inline -> process_tangle.

knitr 1.45
NEW FEATURES

  * Improved the error message to contain more specific information when YAML
    chunk options could not be parsed (thanks, @pedropark99, #2294).

BUG FIXES

  * Special characters in the chunk option fig.alt are properly escaped now
    (thanks, @jay-sf, #2290).

  * Negative numbers returned from inline R expressions lost their minus signs
    when formatted in the scientific notation (thanks, @fkohrt, #2288).

  * convert_chunk_header(type = 'yaml') will now use dash option name for known
    knitr options, and numeric option are kept with same significant digits,
    e.g fig.width = 10 is converted to fig-width: 10.

  * Add the necessary \newline to the last subfigure (thanks, @slrellison,
    rstudio/rmarkdown#2518).

  * Percent signs (%) in LaTeX figure captions and short captions are properly
    escaped now (thanks, @s-u, #2302).

MAJOR CHANGES

  * The object opts_current will be restored after each code chunk has finished
    executing. Previously, it would not be restored, which means even for
    inline R expressions, opts_current$get() will inherit chunk options from a
    previous code chunk (thanks, @rundel, #1988). Besides, opts_current$get
    ('label') will return a unique label for inline expressions. One possible
    application is to construct unique figure paths via fig_path() (e.g.,
    ropensci/magick#310).

  * opts_current$set() without opts_current$lock(FALSE) will trigger a warning
    instead of an error for now and it will become an error in future (#2296).

knitr 1.44
NEW FEATURES

  * kable() can generate Emacs org-mode tables now via kable(..., format =
    'org') (thanks, @xvrdm #1372, @maxecharel #2258).

  * Added support for the qmd (Quarto) output format to spin(), e.g., spin
    ('script.R', format = 'qmd') (thanks, @cderv, #2284).

  * write_bib() has a new argument packageURL to control whether to use a URL
    from the DESCRIPTION file or the one generated by utils::citation()
    (thanks, @dmurdoch, #2264).

  * Updated the package vignette vignette('knit_print', 'knitr') to mention
    that package authors no longer have to make knitr a hard dependency if they
    want to define S3 methods for knitr::knit_print with R >= 3.6.0 (thanks,
    @cderv, #1929).

  * Added a new function download_image() to download an image from a URL and
    include it via include_graphics(). This is mainly for including online
    images when the output format is not HTML (e.g., LaTeX), because the URL
    will not work as the image path, and it has to be downloaded beforehand
    (thanks, @bayeslearner, #2274).

BUG FIXES

  * Make the internal function add_html_caption() work with Quarto <= v1.3.353
    (thanks, @giabaio, #2261).

  * Fixed a bug in spin(format = 'Rnw') reported by @Tarious14 at yihui/
    yihui.org#769 (reply in thread)

  * When the chunk option dev = 'svglite', the svglite device should be used to
    record plots (thanks, @Darxor, #2272).

  * Figure captions are no longer escaped for reStructuredText output, and the
    alt text can also be specified via the fig.alt chunk option now (thanks,
    @trevorld, #2023).

  * Use the correct type of progress bar when rendering Quarto documents in
    RStudio, which takes place in RStudio's background jobs pane or build pane
    (thanks, @hadley, #2271).

  * The opts_current object can no longer be modified within code chunks via
    its $set() method (thanks, @AshesITR, #1798).

  * The argument col.names of kable() can be used to specify the column name of
    row names now, e.g., kable(head(mtcars), col.names = c("car", names
    (mtcars))) ("car" will be the column name of row names in the first column)
    (thanks, @iago-pssjd, #1933).

MAJOR CHANGES

  * Dashes (-) in the names of all chunk options are normalized to dots (.)
    now, e.g., fig-height will be converted to fig.height. This is to make
    knitr more compatible with Quarto since Quarto always use dashes in chunk
    option names (#2282).

MINOR CHANGES

  * In-body chunk options (#|) are now preserved when extracting code from a
    document via purl() (thanks, @LuisLauM, #2268).

  * A warning message will be issued when taking PDF screenshots for HTML
    widgets with the webshot2 package, because webshot2 doesn't use the correct
    figure size at the moment (thanks, @icejean, #2276).

  * If the title argument of knit2wp() is omitted and a title field is
    specified in the YAML metadata of the input document, the YAML title will
    be used (thanks, @arencambre, #1924).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants