davidgohel / officer

:cop: officer: office documents from R
https://ardata-fr.github.io/officeverse/
Other
592 stars 106 forks source link

can not set image size with external_img #307

Closed cat-zeppelin closed 4 years ago

cat-zeppelin commented 4 years ago

Hello. I'm trying to add an image to a Power Point presentation using external_img but regardless "height" and "width" parameters I pass to external_img the size of the image is always the same.

library(tidyverse)
library(officer)

plot <- ggplot(data = iris) +
    geom_point(
        mapping = aes(Sepal.Length, Petal.Length),
        size = 3
    )

ggsave("plot.png", height = 10, width = 10, units = "cm")

doc <- read_pptx()

doc <- doc %>% 
    add_slide() %>% 
    on_slide(1) %>% 
    ph_with(
        external_img(
            "plot.png",
            height = 10 / 2.54,
            width = 10 / 2.54
        ),
        location = ph_location(
            left = 1 / 2.54,
            top  = 1 / 2.54
        )
    ) %>% 
    ph_with(
        external_img(
            "plot.png",
            height = 5 / 2.54,
            width = 5 / 2.54
        ),
        location = ph_location(
            left = 12 / 2.54,
            top  = 1 / 2.54
        )
    )

print(doc, "out.pptx")

Session Info:

R version 3.6.3 (2020-02-29)
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   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] officer_0.3.12.001 forcats_0.5.0      stringr_1.4.0      dplyr_0.8.4        purrr_0.3.3       
 [6] readr_1.3.1        tidyr_1.0.2        tibble_2.1.3       ggplot2_3.2.1      tidyverse_1.3.0   

loaded via a namespace (and not attached):
 [1] zip_2.0.4        Rcpp_1.0.4.6     cellranger_1.1.0 pillar_1.4.3     compiler_3.6.3   dbplyr_1.4.2    
 [7] tools_3.6.3      uuid_0.1-4       lubridate_1.7.4  jsonlite_1.6.1   lifecycle_0.1.0  nlme_3.1-144    
[13] gtable_0.3.0     lattice_0.20-38  pkgconfig_2.0.3  rlang_0.4.6      reprex_0.3.0     cli_2.0.2       
[19] DBI_1.1.0        rstudioapi_0.11  haven_2.2.0      withr_2.1.2      xml2_1.3.2       httr_1.4.1      
[25] fs_1.3.1         generics_0.0.2   vctrs_0.2.3      hms_0.5.3        grid_3.6.3       tidyselect_1.0.0
[31] glue_1.4.1       R6_2.4.1         fansi_0.4.1      readxl_1.3.1     farver_2.0.3     modelr_0.1.6    
[37] magrittr_1.5     backports_1.1.5  scales_1.1.0     rvest_0.3.5      assertthat_0.2.1 colorspace_1.4-1
[43] labeling_0.3     stringi_1.4.6    lazyeval_0.2.2   munsell_0.5.0    broom_0.5.5      crayon_1.3.4   
davidgohel commented 4 years ago

Hi,

You can have a look at the documentation here:

and set use_loc_size to FALSE

cat-zeppelin commented 4 years ago

@davidgohel thanks a lot. awesome package.

To be frank, it was no so clear that secifying width/height in external_img also require a special argument to be set outside. IMHO, use_loc_size should be "FALSE" (or even removed) by default, since usually people use explicit dimensions. Another one argument is that this argument (accordinglly to the documentation) is only used for external_img content type, thus shoud be inside external_img.

In any case, thank you.

davidgohel commented 4 years ago

it was no so clear that secifying width/height in external_img also require a special argument to be set outside

I don't know what else to do. It's in the manual! If you read the manual, you find it. If you don't read the manual and open an issue on GH and I provide the answer, you complain it's not clear.

IMHO, use_loc_size should be "FALSE" (or even removed) by default,

use_loc_size is there because some users sometimes need the image to fit the placeholder, other need the image to respect external_img width and height.

All ph_with methods are by default filling the space defined by the placeholder. I can change the default value to TRUE (and introduce an exception...) but tomorrow I will have to explain to someone else that some users wanted that parameter to TRUE and now they have to define the value explicitly to FALSE.

alapo commented 4 years ago

Can you use external_img for word documents as well? Sorry if its unrelated, but I am researching a fix for my own GitHub posted question to do this with officedown

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.