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

Figures drawn with plot() break code chunks into pieces #17

Closed
liao961120 opened this issue Jul 25, 2024 · 6 comments
Closed

Figures drawn with plot() break code chunks into pieces #17

liao961120 opened this issue Jul 25, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@liao961120
Copy link

I find this counter-intuitive. Not sure if it's intended?

Here's an example:

Code in main.Rmd:

```{r}
#### Data ####
set.seed(2023)
subj_idx = 1:30
gender = rep(1:2, each=15)
heights = c(160, 175)[gender] + rnorm(30, sd=15)

#### Plot ####
ylim = c( min(heights)-5, max(heights)+5 )
plot( 1, type="n", xlim=c(.5,30.5), ylim=ylim,
      xlab="Subject Index", ylab="Height (cm)" )
points(  1:15, heights[ 1:15], col=2, pch=19 )
points( 16:30, heights[16:30], col=4, pch=19 )
abline( v=15.5, col="grey", lty="dashed" )
mtext( c("Girls","Boys"), at=c(7,23), col=c(2,4), padj=-.5 )
```

Output in Markdown

litedown::fuse("main.Rmd", output="output.md")
``` {.r}
#### Data ####
set.seed(2023)
subj_idx = 1:30
gender = rep(1:2, each=15)
heights = c(160, 175)[gender] + rnorm(30, sd=15)

#### Plot ####
ylim = c( min(heights)-5, max(heights)+5 )
plot( 1, type="n", xlim=c(.5,30.5), ylim=ylim,
      xlab="Subject Index", ylab="Height (cm)" )
```
![](<index__files/chunk-2-1.svg>)

``` {.r}
points(  1:15, heights[ 1:15], col=2, pch=19 )
points( 16:30, heights[16:30], col=4, pch=19 )
abline( v=15.5, col="grey", lty="dashed" )
mtext( c("Girls","Boys"), at=c(7,23), col=c(2,4), padj=-.5 )
```

Output in HTML

litedown::fuse("main.Rmd", output="output.html")

image


Huge thanks for this awesome work!

@yihui
Copy link
Owner

yihui commented Jul 25, 2024

It's not intended and sounds like a bug. I'll investigate. Thanks for the report!

@yihui
Copy link
Owner

yihui commented Jul 25, 2024

I can't reproduce the problem on macOS or Windows. Are you using Linux?

@yihui
Copy link
Owner

yihui commented Jul 25, 2024

I just tested it on Ubuntu and couldn't reproduce the problem, either. Please provide xfun::session_info('litedown').

@liao961120
Copy link
Author

I may have found the cause of the problem.

It seems to happen only when dev = svglite::svglite is set:

litedown::reactor(dev = svglite::svglite)

Rmd = '
```{r}
#### Data ####
set.seed(2023)
subj_idx = 1:30
gender = rep(1:2, each=15)
heights = c(160, 175)[gender] + rnorm(30, sd=15)

#### Plot ####
ylim = c( min(heights)-5, max(heights)+5 )
plot( 1, type="n", xlim=c(.5,30.5), ylim=ylim,
      xlab="Subject Index", ylab="Height (cm)" )
points(  1:15, heights[ 1:15], col=2, pch=19 )
points( 16:30, heights[16:30], col=4, pch=19 )
abline( v=15.5, col="grey", lty="dashed" )
mtext( c("Girls","Boys"), at=c(7,23), col=c(2,4), padj=-.5 )
```
'
litedown::fuse(Rmd, output="index.md")

Output:

``` {.r}
#### Data ####
set.seed(2023)
subj_idx = 1:30
gender = rep(1:2, each=15)
heights = c(160, 175)[gender] + rnorm(30, sd=15)

#### Plot ####
ylim = c( min(heights)-5, max(heights)+5 )
plot( 1, type="n", xlim=c(.5,30.5), ylim=ylim,
      xlab="Subject Index", ylab="Height (cm)" )
```
![](<index__files/chunk-1-1.svg>)

``` {.r}
points(  1:15, heights[ 1:15], col=2, pch=19 )
points( 16:30, heights[16:30], col=4, pch=19 )
abline( v=15.5, col="grey", lty="dashed" )
mtext( c("Girls","Boys"), at=c(7,23), col=c(2,4), padj=-.5 )
```

Changing dev = svglite::svglite to dev = svg or other options fixes the problem:

litedown::reactor(dev = svg) 

Rmd = '
```{r}
#### Data ####
set.seed(2023)
subj_idx = 1:30
gender = rep(1:2, each=15)
heights = c(160, 175)[gender] + rnorm(30, sd=15)

#### Plot ####
ylim = c( min(heights)-5, max(heights)+5 )
plot( 1, type="n", xlim=c(.5,30.5), ylim=ylim,
      xlab="Subject Index", ylab="Height (cm)" )
points(  1:15, heights[ 1:15], col=2, pch=19 )
points( 16:30, heights[16:30], col=4, pch=19 )
abline( v=15.5, col="grey", lty="dashed" )
mtext( c("Girls","Boys"), at=c(7,23), col=c(2,4), padj=-.5 )
```
'
litedown::fuse(Rmd, output="index.md")

Output:

``` {.r}
#### Data ####
set.seed(2023)
subj_idx = 1:30
gender = rep(1:2, each=15)
heights = c(160, 175)[gender] + rnorm(30, sd=15)

#### Plot ####
ylim = c( min(heights)-5, max(heights)+5 )
plot( 1, type="n", xlim=c(.5,30.5), ylim=ylim,
      xlab="Subject Index", ylab="Height (cm)" )
points(  1:15, heights[ 1:15], col=2, pch=19 )
points( 16:30, heights[16:30], col=4, pch=19 )
abline( v=15.5, col="grey", lty="dashed" )
mtext( c("Girls","Boys"), at=c(7,23), col=c(2,4), padj=-.5 )
```
![](<index__files/chunk-1-1.svg>)

xfun::session_info('litedown')
#> R version 4.2.1 (2022-06-23)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS 14.3.1, RStudio c(2023, 6, 2, 561)
#> 
#> Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
#> 
#> Package version:
#>   commonmark_1.9.1 graphics_4.2.1   grDevices_4.2.1  litedown_0.0.30  stats_4.2.1     
#>   tools_4.2.1      utils_4.2.1      xfun_0.46  

@yihui
Copy link
Owner

yihui commented Jul 26, 2024

It's a bug of xfun::record(). Minimal reprex:

record('plot(1)\npoints(1)')  # default png; okay
record('plot(1)\npoints(1)', dev = svglite::svglite)  # plot in wrong place

I understand what's going on now and will fix it.

@yihui yihui added the bug Something isn't working label Jul 26, 2024
@yihui
Copy link
Owner

yihui commented Jul 26, 2024

Fixed in xfun: https://github.com/yihui/xfun

litedown::fuse('```{r, dev=svglite::svglite}
plot(1)
points(1)
```', output = 'markdown')
``` {.r}
plot(1)
```
![](<litedown__files/chunk-1-1.svg>)

``` {.r}
points(1)
```

> xfun::session_info('litedown')
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.5, RStudio 2024.4.2.764

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  commonmark_1.9.1 graphics_4.4.0   grDevices_4.4.0  litedown_0.0.30  stats_4.4.0      tools_4.4.0      utils_4.4.0     
  xfun_0.46.3 

clrpackages pushed a commit to clearlinux-pkgs/R-xfun that referenced this issue Aug 20, 2024
Yihui Xie (25):
      add functions lazy_save() and lazy_load()
      provide the command directly in the message
      amend 02973d25c413f27dda43534dde0f279342ac3d23: coerce the version into character, otherwise c() will concatenate version numbers as a list
      fix yihui/litedown#17: svglite will create a plot file upon the call to a high-level plot function, and modify this file later if low-level plot functions are called later
      correct issue number
      no need to use on.exit(after = FALSE)
      support lower versions of R
      typo
      add endsWidth()
      add trimws()
      add file.size()
      typo
      move to zzz.R
      add anyNA()
      remove functions that are available in R 3.2.0
      it seems 9000 is still too big
      make the batch size an argument of cloud_check()
      separate status and report
      rename get_fun to call_fun and call the object directly if it's a function
      `...` is not actually used
      get rid of the assign() hack and use the job name directly
      suppress all messages by default, and only report status and report
      show submission progress
      no need to show the call
      CRAAN release v0.47
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Oct 26, 2024
# CHANGES IN xfun VERSION 0.48

- Added utilities for HTML tags: `html_tag()`, `html_escape()`,
  `html_escape()`, and `html_view()`. Removed the soft dependency on
  the **htmltools** package accordingly.

- `base_pkgs()` is faster now: it calls
  `tools::standard_package_names()` if the function exists (R >=
  4.4.0), otherwise it just returns a constant vector of base package
  names (thanks, @arnaudgallou, #91).

- Added a function `mime_type()` to obtain the MIME types of files via
  `mime::guess_type()` if **mime** is installed, otherwise it will
  call `tools:::mime_type()`, and fall back to using a system command
  (e.g., `file --mime-type`) to obtain the types.

- Added a function `file_rename()` to deal with `file.rename()`
  failures by calling `file.copy()` (thanks, @Giqles @katrinabrock,
  rstudio/bookdown#804).

- `new_app()` will use `utils::browseURL()` to open the app if
  `options('viewer')` is not configured (thanks, @AlbertLei,
  yihui/litedown#29).

- Added a method `record_print.record_asis()` to return the object as is.

# CHANGES IN xfun VERSION 0.47

- Added functions `lazy_save()` and `lazy_load()` to save objects to
  files and lazy-load them.

- Fixed a bug in `record(dev = svglite::svglite)` that misplaced plots
  when low-level plot functions are used (thanks, @liao961120,
  yihui/litedown#17).

- Specified the lowest R version required (v3.2.0) for this package.

# CHANGES IN xfun VERSION 0.46

- `md_table()` should add a vertical ellipsis to row names when rows
  are truncated by the `limit` argument.

- `session_info()` recognizes Positron now (thanks, @chuxinyuan, #89).

# CHANGES IN xfun VERSION 0.45

- For `record()` with `verbose = 1` or `2`, invisible `NULL` is no
  longer printed.

- `Rscript_call()` will show the actual error message (if an error
  occurred) during calling the function in a new R session.

# CHANGES IN xfun VERSION 0.44

- Added a function `cache_exec()` to cache the execution of an
  expression either in memory or on disk. It is much more general and
  flexible than `cache_rds()`. For example, it supports custom
  reading/writing methods for cache files, and can load locally
  created variables in the expression while loading cache.

- Added an argument `cache` to `record()` to make it possible to enable caching.

- Added arguments `message` and `warning` to `record()` to decide
  whether messages and warnings should be recorded.

- Changed the default value of the argument `error` of `record()` from
  `FALSE` to `NA`. Now `FALSE` means to suppress error messages, and
  `NA` means to throw errors normally. This is for consistency with
  the `message` and `warning` arguments.

- Added an S3 generic function `record_print()`, which is similar to
  `knitr::knit_print()` but for the purpose of printing visible values
  in `record()`.

- The `record()` function gained new arguments `print` and
  `print.args` to support custom printing functions and arguments.

- Added a function `md_table()`, which is a minimal Markdown table
  generator.

- Exported the internal function `md5()` to calculate the MD5
  checksums of R objects. The function is essentially a workaround for
  `tools::md5sum()` (see HenrikBengtsson/Wishlist-for-R#21).

- For `fenced_block()`, a space is added between the backticks and the
  language name, e.g., ```` ```r ```` has become ```` ``` r ````
  now. This will affect snapshot tests based on Markdown ([an
  example](yihui/knitr-examples@931e0a2)).

- Added a shorthand `fenced_div()` for `fenced_block(char = ':')`.

- `write_utf8()` returns the `con` argument (typically a file path)
  now. Previously, it returns `NULL`.

- Added an experimental function `new_app()` to create a local web application.

- The returned value of `yaml_body()` contains a new element `lines`
  in the list indicating the line numbers of YAML metadata if exists.

- Removed the `skip` argument from `split_source()`.

- For `split_source(line_number = TRUE)`, the attribute name for line
  numbers in the returned value was changed from `line_start` (a
  single starting line number) to `lines` (both the starting and
  ending numbers).

- Fixed an edge case in `prose_index()`, in which inline code was
  incorrectly recognized as a code block fence.

# CHANGES IN xfun VERSION 0.43

- Added a function `upload_imgur()`, which was adapted from
  `knitr::imgur_upload()`. The latter will call the former in the
  future. `xfun::upload_imgur()` allows users to choose whether to use
  the system command `curl` or the R package **curl** to upload the
  image. It also has a new argument `include_xml` to specify whether
  the XML response needs to be included in the returned value.

- Added a function `fenced_block()` to create a fenced block in
  Markdown (thanks, @cderv, yihui/knitr#2331). The block can be either
  a code block or a fenced Div.

- Fixed a bug in `xfun::record()` when the argument `verbose = 1` or `2`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants