2DegreesInvesting / tiltPlot

Plots for the TILT project
https://2degreesinvesting.github.io/tiltPlot/
GNU General Public License v3.0
0 stars 0 forks source link

TASK LIST for enhancements (no action for now) #136

Open maurolepore opened 1 month ago

maurolepore commented 1 month ago

As I start to use tiltPlot I identify some issues that I see. I record them here so I don't forget. Later I'll polish and follow up.

map

# Fails
without_financial |> 
  transform(postcode = as.character(postcode)) |> 
  map_region_risk(country_code = "DE", benchmark = "unit")
#> Error in map_region_risk(transform(without_financial, postcode = as.character(postcode)), : could not find function "map_region_risk"

# This seems like an easy solution
without_financial |> 
  transform(postcode = as.integer(as.character(postcode))) |> 
  map_region_risk(country_code = "DE", benchmark = "unit")
#> Error in map_region_risk(transform(without_financial, postcode = as.integer(as.character(postcode))), : could not find function "map_region_risk"

# Note some postcodes may include letters, and this "solution" turns then to NA
as.integer(c("1234", "5678netherlands"))
#> Warning: NAs introduced by coercion
#> [1] 1234   NA
library(tiltWebTool)
library(tiltPlot)

# All of this fails in a similar way
emissions |> unnest_company() |> tiltPlot::map_region_risk()
#> Error in `mutate()`:
#> ℹ In argument: `risk_category_var = as_risk_category(data[[risk_var]])`.
#> Caused by error in `data[[risk_var]]`:
#> ! Can't extract column with `risk_var`.
#> ✖ Subscript `risk_var` must be size 1, not 2.

sector |> unnest_company() |> tiltPlot::map_region_risk()
#> Error in `mutate()`:
#> ℹ In argument: `risk_category_var = as_risk_category(data[[risk_var]])`.
#> Caused by error in `data[[risk_var]]`:
#> ! Can't extract column with `risk_var`.
#> ✖ Subscript `risk_var` must be size 1, not 0.

sector |> unnest_product() |> tiltPlot::map_region_risk()
#> Error in `mutate()`:
#> ℹ In argument: `risk_category_var = as_risk_category(data[[risk_var]])`.
#> Caused by error in `data[[risk_var]]`:
#> ! Can't extract column with `risk_var`.
#> ✖ Subscript `risk_var` must be size 1, not 0.

Start with the toy data, then with the real data.

This ensures to request what we need only once and avoid saturating the kind service of the API. Also make us fully independent from failures of the server, and we can drop any dependencies related to making those requests. This all means easier maintainance, easier and faster deployments, and a snappier experience for the user.

Notice it takes close to 10 seconds with only 100 companies

library(tiltPlot)

nrow(without_financial)
#> [1] 114

system.time(map_region_risk(without_financial, country_code = "DE", benchmark = "unit"))
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues
#>    user  system elapsed 
#>   0.672   0.059   7.761

This is likely because of the time it takes to make requests. Here we see a message that suggests we're making a request:

library(tiltPlot)

without_financial |> 
  map_region_risk(country_code = "DE", benchmark = "unit")
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

In addition to having a local copy of the data, we can also avoid recomputations by memoising the function.

library(tiltPlot)

without_financial |> 
  head(50) |> 
  map_region_risk(country_code = "DE", benchmark = "unit")
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues
#> Error in `stopifnot()`:
#> ℹ In argument: `color = pmap(list(.data$high, .data$medium, .data$low),
#>   custom_gradient_color)`.
#> Caused by error in `.data$high`:
#> ! Column `high` not found in `.data`.

Created on 2024-06-01 with reprex v2.1.0

barplot

library(tiltPlot)
bar_plot_emission_profile(without_financial)
#> Error in is_character(x): promise already under evaluation: recursive default argument reference or earlier problems?

Created on 2024-06-01 with reprex v2.1.0

I think last time I figured that installing tiltPlot with pak::pak() solved the problem because it discovered the required system dependencies.

Deployments to shinyapps.io failed with what I show below. I think we faced similar problems with system level libraries. TODO revisit those conversations to find the solution.

- Installing units ...                          FAILED
================================================================================
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/home/runner/work/tiltWebTool/tiltWebTool/renv/staging/1/units/libs/units.so':
  libudunits2.so.0: cannot open shared object file: No such file or directory
Calls: loadNamespace -> library.dynam -> dyn.load
Execution halted
Error: Error: error testing if 'units' can be loaded [error code 1]
ss

SystemRequirements: libudunits2-dev