简体   繁体   中英

Size of PDF files generated by knitr

I am using the following chunk in an Rnw file.

<<scatter,dev='pdf',include=TRUE>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
@

I knitted the same Rnw file under R3.6.3 and R4.0.2 , respectively. However, the PDF figure generated under R4.0.2 is much larger (~8 times) than the one from R3.6.3 .

I am wondering if there is a way (eg. a chunk option) to reduce the PDF size under R4.0.2 .

The session information of two different versions of R is shown below.

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.29

loaded via a namespace (and not attached):
[1] compiler_4.0.2 magrittr_1.5   tools_4.0.2    stringi_1.4.6  highr_0.8      stringr_1.4.0 
[7] xfun_0.16      evaluate_0.14
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)

Matrix products: default

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.28

loaded via a namespace (and not attached):
[1] compiler_3.6.3 magrittr_1.5   tools_3.6.3    stringi_1.4.6  highr_0.8      stringr_1.4.0 
[7] xfun_0.12      evaluate_0.14 

The only reference to pdf in the release notes lately has been in relation to the dingbats font. Looking at the reference, it says (emphasis mine):

useDingbats logical. Should small circles be rendered via the Dingbats font? Defaults to TRUE, which produces smaller and better output. Setting this to FALSE can work around font display problems in broken PDF viewers: although this font is one of the 14 guaranteed to be available in all PDF viewers, that guarantee is not always honoured.

You can change the default via

pdf.options(useDingbats = TRUE)

in the first code chunk of your Rnw document. This change is global to your document. If you'd rather change it only for a specific code chunk, you may use the chunk option dev.args , eg,

<<scatter, dev='pdf', dev.args=list(useDingbats = TRUE)>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
@

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM