munichrocker / DatawRappr

R-Package to connect to the Datawrapper-API
https://munichrocker.github.io/DatawRappr/
MIT License
86 stars 13 forks source link

scale parameter has no impact in dw_export_chart() #71

Open dhmontgomery opened 2 weeks ago

dhmontgomery commented 2 weeks ago

When I attempt to export a chart via dw_export_chart(), the scale parameter does not appear to do anything. The intended output, as far as I can tell, is that giving this a value between 1 and 4 should increase the size of the image by multiplicative factors.

But when I put in different values, I get the exact same output. For example, all these return identical images:

dw_export_chart("23LZl", scale = 1)
dw_export_chart("23LZl", scale = 2)
dw_export_chart("23LZl", scale = 3)
dw_export_chart("23LZl", scale = 4)

Put another way:

> c(1:4) %>% 
     map_dfr(~dw_export_chart("23LZl", scale = .) %>%
                 image_info())
# A tibble: 4 × 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
1 PNG     1092    928 sRGB       TRUE     93468 72x72  
2 PNG     1092    928 sRGB       TRUE     93468 72x72  
3 PNG     1092    928 sRGB       TRUE     93468 72x72  
4 PNG     1092    928 sRGB       TRUE     93468 72x72  

This replicates on different images. I can still use the GUI to export these images at different scales, it just doesn't do anything with dw_export_chart().

dhmontgomery commented 2 weeks ago

I believe I have found the cause of the problem! In the API docs, it lists two different relevant parameters:

I extracted dw_call_api() from the function and did some experimenting, and if you change the scale = scale[1] parameter to zoom = scale[1] you get PNG output of different sizes, as expected.

Possible solutions:

  1. Just duplicate the API call so setting scale sets both the scale and zoom parameters: ... scale = scale[1], zoom = scale[1], ... (As near as I can tell this works without causing an error)
  2. Use a conditional based on the type parameter to determine whether the value of scale should be sent to the scale or zoom API parameters.
  3. Provide separate parameter options for scale and zoom and require the user to pick the correct one (this seems like a bad idea)

I will work on a pull request, unless someone has strong thoughts on the various options before I finish that.