Size of PDF files generated by knitr - r

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, e.g.,
<<scatter, dev='pdf', dev.args=list(useDingbats = TRUE)>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
#

Related

RStudio makes my graphs turn grey when I expand them

I'm using a script in RStudio which produces several graphs. This is a (very simplified) example of how it looks
mpg = mtcars$mpg
cyl = mtcars$disp
hp = mtcars $hp
plot(mpg, cyl)
dev.new()
plot(mpg,hp)
dev.new()
plot(hp, cyl)
When I run this in RStudio (using the "Source" key), the first graph stays in the "Plots" viewer in the lower right of the Rstudio window (I would actually like it to be a separate window as well if anyone knows how to do that), and the second and third graphs are produced as individual windows separate from the RStudio screen.
When I try to change the size of these windows (by pressing the full screen button in the top right corner, or by dragging the side), the most recently generated window is fine and resizes, but the previous one turns grey and the graph stops showing.
I thought it might have something to do with the fact that the graphics devices are inactive, but the most recently generated one, which rescales fine, also says it's inactive.
Any help would be appreciated! Thanks.
Edit: here is the output of sessionInfo():
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DescTools_0.99.36 dplR_1.7.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 mvtnorm_1.1-1 lattice_0.20-41 matrixStats_0.56.0 XML_3.99-0.3 png_0.1-7
[7] digest_0.6.25 MASS_7.3-51.5 R.methodsS3_1.8.0 grid_4.0.0 plyr_1.8.6 signal_0.7-6
[13] magrittr_1.5 stringi_1.4.6 rstudioapi_0.11 R.oo_1.23.0 R.utils_2.9.2 Matrix_1.2-18
[19] boot_1.3-24 tools_4.0.0 stringr_1.4.0 compiler_4.0.0 expm_0.999-4
It's not a perfect answer, but here's what I found to work around it:
When you generate a number of graphics devices the most recently generated one remains "active" while the rest are inactive. If you don't close the active window and resize the others, it works fine. But if you close the active window and then try to resize the others, they just go grey. Somehow, closing the active window shuts down something and stops you from being able to resize the graphs at all. Weird.

Office word throw warning info when open officer generated docx with tiff in it

When I'm opening a docx file generated by R package officer which is inserted with a tiff picture file, the Office 2016 throw me an error massage.
example:
library(magrittr)
library(ggplot2)
library(officer)
img <- "test.tiff"
pic <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
img %>% ggsave(pic, width = 6, height = 3)
read_docx() %>%
body_add_img(img, width = 6, height = 3) %>%
print(target = "test.docx")
Then when opening test.docx, I got an error massage
We're sorry. We can't open test.docx because we found a problem with its contents.
Details
The file is corrupt and cannot be opened.
After I press 'OK' button, Word ask me if I want to recover the contents of the document. Choose 'yes' then I got a Document1 with correct picture in it.
I tried .jpg and .png format, and they both worked correctly, so this can be a work-around for now. Is this a bug or just me making mistakes? Either way it is truly welcomed to enlighten me, thanks.
sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] officer_0.2.0 ggplot2_2.2.1 magrittr_1.5
loaded via a namespace (and not attached):
[1] Rcpp_0.12.14 digest_0.6.13 R.methodsS3_1.7.1 R6_2.2.2 grid_3.4.3
[6] plyr_1.8.4 gtable_0.2.0 scales_0.5.0 zip_1.0.0 rlang_0.1.6
[11] uuid_0.1-2 lazyeval_0.2.1 R.oo_1.21.0 R.utils_2.6.0 xml2_1.1.1
[16] labeling_0.3 tools_3.4.3 munsell_0.4.3 yaml_2.1.16 compiler_3.4.3
[21] base64enc_0.1-3 colorspace_1.3-2 htmltools_0.3.6 tibble_1.3.4
As written here, only png, jpg and emf have been tested. However, support for tiff is quite easy to integrate, I will push un an update to handle that later today on Github.

knitr, RGL and legend3d: legend3d didn't show up in the html generated by knitr

I am able to create a dynamic 3D scatter plot using rgl and knitr, but the legend created by the legend3d is missing. I guess it may be related to the implementation of hook_webgl. Any idea? Thank you.
Here are the code I used to create the 3D scatter plot:
```{r fig1, webgl=TRUE, fig.width=10 , fig.height = 10}
library(knitr)
library(rgl)
knit_hooks$set(webgl = hook_webgl)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rep(1:2,each=500))
legend3d("topright",legend=c("Type 1","Type 2"), pch=16, col=1:2)
```
My session info:
R version 3.1.3 (2015-03-09)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows Server 2008 R2 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] knitr_1.11 rgl_0.95.1441 dplyr_0.4.3
loaded via a namespace (and not attached):
[1] assertthat_0.1 DBI_0.3.1 digest_0.6.8 evaluate_0.8
[5] formatR_1.2.1 highr_0.5.1 htmltools_0.3 lazyeval_0.1.10
[9] magrittr_1.5 parallel_3.1.3 R6_2.1.2 Rcpp_0.12.3
[13] rmarkdown_0.9.5 stringi_0.5-5 stringr_1.0.0 tools_3.1.3
[17] yaml_2.1.13
Looks like a bug in the rglwidget code; I'll look into it.
I recommend a few changes to your programming style. These won't help with this bug, but may save you problems later:
Use the setupKnitr() function rather than setting the knitr hook yourself. The link to knitr is likely to change, but setupKnitr() will protect you from problems.
Rather than saying webgl=TRUE in the chunk options, make a call to the rglwidget::rglwidget() function when you want to draw the plot. I'm hoping to make this call unnecessary in the future (more like what happens with regular plots), but for now it's the better tested way to do things.
Update your R. Current version is 3.2.3, with 3.2.4 due quite soon.
Use rgl and rglwidget from R-forge. They are newer than the CRAN versions, and have several bug fixes. Hopefully soon they'll have the legend bug fix too.
Added later:
This wasn't so much a bug as something that wasn't implemented. Now it is, if you use rgl version 0.95.1456 and rglwidget version 0.1.1456, both available from R-forge. Not much testing yet, so there may be more changes over the next while.

switch() returns NULL in shiny on unicode characters coming from variable input only

When I use switch inside my shiny-server function on unicode characters, I don't get the expected behavior :
library(shiny)
app<-shinyApp(
ui=shinyUI(fluidPage(
selectInput("variable",label = NULL,choices = c( "a","b","é"), selected = "é"),
mainPanel(plotOutput("text"))
)),
server=function(input, output) {
output$text<-renderText({
print(switch(input$variable,
"a"="A",
"b"="B",
"é"="E"))
})
})
runApp(app)
Will print "A" in the console for an input equals to "a" but NULL in the console for input equals to "é".
On the other hand :
x<-"é"
switch(x,
"a"="A",
"b"="B",
"é"="E")
returns "E", just as I expected it.
I tried to replace the first argument of the switch call input$variable by "é" only, it does work like that. I guess input$variable is not exactly what I think it is, but coercing to a character string with as.character(input$variable) does not work either.
If I use print(input$variable) it comes up with [1] "é" which is all fine. This is beyond my current understanding...
I really need to display unicode characters and translate them in non-unicode. Any help would be greatly appreciated :-)
By the way sessionInfo() returns :
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_0.12.0
loaded via a namespace (and not attached):
[1] digest_0.6.8 htmltools_0.2.6 httpuv_1.3.2 jsonlite_0.9.16 mime_0.3
[6] R6_2.0.1 Rcpp_0.11.6 rsconnect_0.3.79 rstudioapi_0.3.1 tools_3.1.3
[11] xtable_1.7-4

Quantstrat faber_rebal.R demo error

I have been going through the demos in quantstrat. I have a problem running faber_rebal.r. It fails with the following error:
> out<-applyStrategy.rebalancing(strategy='faber' , portfolios='faber')
Error in `colnames<-`(`*tmp*`, value = c("MaxPos", "LongLevels", "MinPos", :
length of 'dimnames' [2] not equal to array extent
Here is the output of sessionInfo():
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_South Africa.1252 LC_CTYPE=English_South Africa.1252
[3] LC_MONETARY=English_South Africa.1252 LC_NUMERIC=C
[5] LC_TIME=English_South Africa.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] quantstrat_0.7.8 foreach_1.4.1 blotter_0.8.15
[4] PerformanceAnalytics_1.1.0 FinancialInstrument_1.1 quantmod_0.4-0
[7] Defaults_1.1-1 TTR_0.22-0 xts_0.9-5
[10] zoo_1.7-10 lattice_0.20-23
loaded via a namespace (and not attached):
[1] codetools_0.2-8 grid_3.0.1 iterators_1.0.6 tools_3.0.1
The problem occurs within the function applyStrategy.rebalancing when it calls the private function ruleProc.
I also get the same error on my Ubuntu 12.04 Machine with R 3.0.1.
Any help to get it working would be appreciated.
Thanks
Charles
I had some problems getting the faber_rebal.R demo to work as well.
First, you have to set the timezone:
ttz<-Sys.getenv('TZ')
Sys.setenv(TZ='UTC')
Second, I could get the following line in the add.rule rebalance to work:
refprice=quote(last(getPrice(mktdata)[paste('::',timestamp,sep='')][,1])),
so I changed it to:
refprice=quote(last(getPrice(mktdata)[paste('::','20140119',sep='')][,1])),
I hope that helps.
Best,
Peter
It looks like demo was fixed in the latest update to the package. Try updating to Rev.: 1595 or later

Resources