atusy / ftExtra

Helper functions for the flextable package
https://ftextra.atusy.net/
Other
65 stars 5 forks source link

colformat_md not working with save_as_image #109

Open Mayamikarmakar opened 4 months ago

Mayamikarmakar commented 4 months ago
ft_image <- data.frame(
  R = sprintf("![](%s)", file.path(R.home("doc"), "html", "logo.jpg")),
  stringsAsFactors = FALSE
) %>%
  flextable() %>%
  colformat_md() %>%
  flextable::autofit()

  save_as_image(ft_image,"image.png")

doesn't give the .png file.

save_as_image(ft_image,"image.png") Error in check_numeric_scalar(height, "height") : height must be a numeric scalar In addition: Warning messages: 1: In max(.SD$part_width, na.rm = TRUE) : no non-missing arguments to max; returning -Inf 2: In max(.SD$part_ascent, na.rm = TRUE) : no non-missing arguments to max; returning -Inf 3: In max(.SD$part_ascent, na.rm = TRUE) : no non-missing arguments to max; returning -Inf 4: In max(height, params$min_height, na.rm = TRUE) : no non-missing arguments to max; returning -Inf

 ft_emoji <- data.frame(emoji = c(":+1:"), stringsAsFactors = FALSE) %>%
  flextable() %>%
  colformat_md(md_extensions = "+emoji")

  save_as_image(ft_emjoi,"emoji.png")

are not supporting the emoji's used. image

atusy commented 4 months ago

Thanks. I will take a look at the first issue which is reproducible.

The second issue sounds like the matter of your system environment. At least it worked on my environment. The document of save_as_image says it uses R graphic system. This indicates you need some fonts...? unsure...

' This function uses R graphic system to create an image from the flextable,

' allowing for high-quality image output. See [gen_grob()] for more options.

I guess you can reproduce the second issue without ftExtra.

 ft_emoji <- data.frame(emoji = c(":+1:"), stringsAsFactors = FALSE) %>%
  flextable() %>%
  colformat_md(md_extensions = "+emoji")

  save_as_image(ft_emjoi,"emoji.png")

and maybe even without flextable

plot(1, 1, xlab = "👍")
atusy commented 4 months ago

The first issue comes from image sizes being NA from below. Maybe I should consider using flextable::as_image() which has the guess_size option or guess the size on ftExtra side.

https://github.com/atusy/ftExtra/blob/ceda37eb30d025c081ace47439d30dcadeec16a7/R/as-paragraph-md.R?plain=1#L49-L50

atusy commented 4 months ago

reproducible example without ftExtra

library(flextable)
flextable(data.frame(x = 1)) |> 
  compose(
    1, 1,
    as_paragraph(chunk_dataframe(
      txt = "", img_data = file.path(R.home("doc"), "html", "logo.jpg")
    ))
  ) |>
  save_as_image('image.png')