Open DawnEve opened 2 years ago
I'll look at that.
Just to share that I'll edit your post so that guidelines are followed for readability: https://yihui.org/issue/#please-format-your-issue-correctly
I can't really explain it but it seems that the R code is not really valid itself
About the pin
in par()
pin: The current plot dimensions, (width, height), in inches.
When setting fig.width
and fig.height
in a chunk, it will set the width and height of the graphic device. We can emulate code this way and it will throw an error even outside of knitr, even 2 errors
# Temp plot file
plot_file <- tempfile(fileext = ".png")
# Before opening device
par(no.readonly = TRUE)$pin
#> [1] 5.759999 3.159999
# Knitr will open a png device using the size provided
grDevices::png(filename = plot_file,
width = 2.2,
height = 1,
units = "in",
res = 72)
# New pin value following the size
opar <- par(no.readonly = TRUE)
opar$pin
#> [1] 0.9544431 -0.8400014
# It is negative - not sure why
# Changing par
par(no.readonly = TRUE)$mar
#> [1] 5.1 4.1 4.1 2.1
par(mar=c(0,0,0,0))
par(no.readonly = TRUE)$mar
#> [1] 0 0 0 0
# Ploting work
plot(iris$Sepal.Length)
# Trying to restore par - issue with `par`, probably because negative dimention is invalid
par(opar)
#> Error in par(opar): invalid value specified for graphical parameter "pin"
# Trying to plot with reset par - margin has been reset, but it is too large as the figure is too small
# so error
plot(2)
#> Error in plot.new(): figure margins too large
dev.off()
#> png
#> 2
unlink(plot_file)
When you set the dimension in chunk option to be greater, this won't probably happen because the values are ok in par()
But as you found knitr will open a new graphic device for each chunk by default, so if you need to reset some par define in one chunk, it is recommended to open a new graphic device.
So I am not sure it is a bug in rmarkdown or knitr considering that doing the same in R base would lead to the same result.
Not sure why pin
would be negative as it is supposed to be a dimension.
@yihui do you have more insights on this behavior ?
I don't know why pin
could be negative, either. It sounds like a bug of base R. Perhaps try to report it to R core? https://www.r-project.org/bugs.html
Perhaps try to report it to R core?
Ok I'll look into that. Never done it before but there is a first for everything ! 😄
Good idea. As I'm not going that deep in R, it's a good idea for senior developers like you to report and discuss this issue.
I just waiting, and alternative solutions in Rmarkdown is acceptable for this situation now.
I found this bug weeks ago, which I can't restore par() in Rmarkdown sometimes when the fig.width is too small.
If I want this small fig.width size and to restore par() in the same trunk like the code shown below, is it possible?
This bug can be passed when setting
fig.width=2.2, fig.height=1
larger or removingopar=par(no.readonly = T)
, or puting them in two separate trunks. I used the 3rd solution now, so this bug is not urgent.test2
Error in par(opar) : invalid value specified for graphical parameter "pin" Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> par Execution halted