Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/18 packagize #21

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6397fda
update docker dirs and gitignore
AlexAxthelm Nov 14, 2024
b8bc248
Move existing functions to own files
AlexAxthelm Nov 22, 2024
a518ce2
Wrap main script as a function
AlexAxthelm Nov 22, 2024
11bfca8
Move file to `R/`
AlexAxthelm Nov 22, 2024
a0dc1c9
Use script in inst directory, and install package
AlexAxthelm Nov 22, 2024
a5189e1
update main calling script
AlexAxthelm Nov 22, 2024
bbf3830
Merge branch 'main' into feat/18-packagize
AlexAxthelm Nov 22, 2024
0f8ad76
Move new functions to own files
AlexAxthelm Nov 22, 2024
f3cadd5
Add R package check workflow
AlexAxthelm Nov 22, 2024
de661f5
Add directories as args to main function
AlexAxthelm Nov 22, 2024
659f87a
Add `.lintr` with all errors suppressed
AlexAxthelm Nov 22, 2024
239228f
Linting: indentation
AlexAxthelm Nov 22, 2024
c3b2a06
Style: use explicit pivoting columns
AlexAxthelm Nov 25, 2024
efb6201
linting (trailing whitespace)
AlexAxthelm Nov 25, 2024
81e9ddb
linting: line length
AlexAxthelm Nov 25, 2024
73c6fe1
Linting: assignment (`=`/`<-`) and commas
AlexAxthelm Nov 25, 2024
dfa2d99
Prepare for object_usage_linter
AlexAxthelm Nov 25, 2024
39fc9a7
Linting: First pass object_usage
AlexAxthelm Nov 25, 2024
cb888f3
import functions from `logger`
AlexAxthelm Nov 25, 2024
70ce560
namespace calls to `filter`
AlexAxthelm Nov 25, 2024
11cf740
namespace calls to `mutate`
AlexAxthelm Nov 25, 2024
e14b97b
namespace calls to group_by/ungroup
AlexAxthelm Nov 25, 2024
3b295b0
namespace functions form tidyr
AlexAxthelm Nov 25, 2024
2a96bf0
remove most of the `library` calls
AlexAxthelm Nov 25, 2024
58641ed
Convert from `%>%` pipe to `|>`
AlexAxthelm Nov 25, 2024
2ee4840
fix join syntax and namespace dplyr::if_else
AlexAxthelm Nov 25, 2024
aeacffd
ignore output dir
AlexAxthelm Nov 25, 2024
bdacd04
namespace remaining `dplyr` functions
AlexAxthelm Nov 25, 2024
c04b398
Increment version number to 0.0.0.9002
AlexAxthelm Nov 25, 2024
a5b9049
remaining dplyr::namespacing
AlexAxthelm Nov 25, 2024
66b3f28
MIT license
AlexAxthelm Nov 25, 2024
97cec90
Update Rbuildignore
AlexAxthelm Nov 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
^.*\.Rproj$
^.github/
^.lintr$
^Dockerfile$
^LICENSE\.md$
^\.Rproj\.user$
^docker-compose.yml$
23 changes: 23 additions & 0 deletions .github/workflows/R.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# This example file will enable R language checks on push or PR to the main
# branch.
# It will also run the checks every weeknight at midnight UTC
#
# Note the @main in `uses:` on the last line. This will call the latest version
# of the workflow from the `main` brnach in the RMI-PACTA/actions repo. You can
# also specify a tag from that repo, or a commit SHA to pin action versions.
on:
pull_request:
push:
branches: [main]
schedule:
- cron: '0 0 * * 1,2,3,4,5'
workflow_dispatch:

name: R

jobs:
R-package:
name: R Package Checks
uses: RMI-PACTA/actions/.github/workflows/R.yml@main
secrets: inherit
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ inputs/
outputs/
data/
.Rhistory
analysis_output_dir/**/*
benchmarks_dir/**/*
dashboard_output_dir/**/*
dashboard_data_dir/**/*
4 changes: 4 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters: linters_with_defaults(
object_length_linter = NULL,
commented_code_linter = NULL
)
18 changes: 14 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
Package: workflow.pacta.dashboard
Title: Run PACTA dashboard JSON generation
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R:
c(person(given = "CJ",
c(person(given = "Alex",
family = "Axthelm",
role = c("aut", "cre", "ctr"),
email = "[email protected]",
comment = c(ORCID = "0000-0001-8579-8565")),
person(given = "CJ",
family = "Yetman",
role = c("aut", "ctr"),
email = "[email protected]",
Expand All @@ -23,9 +28,14 @@ RoxygenNote: 7.3.2
Imports:
dplyr,
jsonlite,
logger,
pacta.portfolio.report,
pacta.portfolio.utils,
readr
pacta.workflow.utils,
readr,
rlang,
tidyr
Remotes:
RMI-PACTA/pacta.portfolio.report,
RMI-PACTA/pacta.portfolio.utils
RMI-PACTA/pacta.portfolio.utils,
RMI-PACTA/pacta.workflow.utils
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/rocker/r-ver:4.3.1
FROM docker.io/rocker/r-ver:4.3.1 as base

RUN CRAN_LIKE_URL="https://packagemanager.posit.co/cran/__linux__/jammy/2024-04-05"; \
echo "options(repos = c(CRAN = '$CRAN_LIKE_URL'))" \
Expand All @@ -19,8 +19,11 @@ COPY DESCRIPTION /workflow.pacta.dashboard/DESCRIPTION
# install pak, find dependencises from DESCRIPTION, and install them.
RUN Rscript -e "pak::local_install_deps('/workflow.pacta.dashboard')"

COPY main.R /workflow.pacta.dashboard/main.R
FROM base AS install-pacta

WORKDIR /workflow.pacta.dashboard
COPY . /workflow.pacta.dashboard/

CMD ["Rscript", "--vanilla", "/workflow.pacta.dashboard/main.R"]
RUN Rscript -e "pak::local_install(root = '/workflow.pacta.dashboard')"

# set default run behavior
ENTRYPOINT ["Rscript", "--vanilla", "/workflow.pacta.dashboard/inst/extdata/scripts/prepare_dashboard_data.R"]
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2024
COPYRIGHT HOLDER: workflow.pacta.dashboard authors
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2024 workflow.pacta.dashboard authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated by roxygen2: do not edit by hand

importFrom(logger,log_debug)
importFrom(logger,log_error)
importFrom(logger,log_info)
importFrom(logger,log_trace)
importFrom(logger,log_warn)
importFrom(rlang,.data)
importFrom(rlang,.env)
139 changes: 139 additions & 0 deletions R/prep_company_bubble.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# prep_company_bubble ----------------------------------------------------------
# based on pacta.portfolio.report:::prep_company_bubble, but does not filter to
# allocation == "portfolio_weight" nor by scenario and scenario source
prep_company_bubble <- function(
equity_results_company,
bonds_results_company,
portfolio_name,
start_year,
green_techs
) {

equity_data <-
equity_results_company |>
dplyr::filter(.data$portfolio_name == .env$portfolio_name) |>
dplyr::filter(
.data$ald_sector %in% c(
"Power",
"Automotive"
)
) |>
dplyr::filter(.data$equity_market == "GlobalMarket") |>
dplyr::filter(.data$scenario_geography == "Global") |>
dplyr::filter(
.data$year %in% c(
.env$start_year,
.env$start_year + 5
)
) |>
dplyr::mutate(
plan_buildout = (
dplyr::last(.data$plan_tech_prod, order_by = .data$year) -
dplyr::first(.data$plan_tech_prod, order_by = .data$year)
),
scen_buildout = (
dplyr::last(.data$scen_tech_prod, order_by = .data$year) -
dplyr::first(.data$scen_tech_prod, order_by = .data$year)
),
.by = c(
"company_name",
"technology",
"scenario_source",
"scenario",
"allocation"
)
) |>
dplyr::filter(.data$year == .env$start_year) |>
dplyr::mutate(green = .data$technology %in% .env$green_techs) |>
dplyr::reframe(
plan_tech_share = sum(.data$plan_tech_share, na.rm = TRUE),
plan_buildout = sum(.data$plan_buildout, na.rm = TRUE),
scen_buildout = sum(.data$scen_buildout, na.rm = TRUE),
plan_carsten = sum(.data$plan_carsten, na.rm = TRUE),
port_weight = unique(.data$port_weight),
.by = c(
"company_name",
"allocation",
"scenario_source",
"scenario",
"ald_sector",
"green",
"year"
)
) |>
dplyr::mutate(y = .data$plan_buildout / .data$scen_buildout) |>
dplyr::filter(.data$green) |>
dplyr::select(
-"plan_buildout",
-"scen_buildout",
-"green"
) |>
dplyr::filter(!is.na(.data$plan_tech_share)) |>
dplyr::mutate(y = pmax(.data$y, 0, na.rm = TRUE)) |>
dplyr::mutate(asset_class = "Listed Equity")

Check warning on line 73 in R/prep_company_bubble.R

View check run for this annotation

Codecov / codecov/patch

R/prep_company_bubble.R#L12-L73

Added lines #L12 - L73 were not covered by tests

bonds_data <-
bonds_results_company |>
dplyr::filter(.data$portfolio_name == .env$portfolio_name) |>
dplyr::filter(
.data$ald_sector %in% c(
"Power",
"Automotive"
)
) |>
dplyr::filter(.data$equity_market == "GlobalMarket") |>
dplyr::filter(.data$scenario_geography == "Global") |>
dplyr::filter(
.data$year %in% c(
.env$start_year,
.env$start_year + 5
)
) |>
dplyr::mutate(
plan_buildout = (
dplyr::last(.data$plan_tech_prod, order_by = .data$year) -
dplyr::first(.data$plan_tech_prod, order_by = .data$year)
),
scen_buildout = (
dplyr::last(.data$scen_tech_prod, order_by = .data$year) -
dplyr::first(.data$scen_tech_prod, order_by = .data$year)
),
.by = c(
"company_name",
"technology",
"scenario_source",
"scenario",
"allocation"
)
) |>
dplyr::filter(.data$year == .env$start_year) |>
dplyr::mutate(green = .data$technology %in% .env$green_techs) |>
dplyr::reframe(
plan_tech_share = sum(.data$plan_tech_share, na.rm = TRUE),
plan_buildout = sum(.data$plan_buildout, na.rm = TRUE),
scen_buildout = sum(.data$scen_buildout, na.rm = TRUE),
plan_carsten = sum(.data$plan_carsten, na.rm = TRUE),
port_weight = unique(.data$port_weight),
.by = c(
"company_name",
"allocation",
"scenario_source",
"scenario",
"ald_sector",
"green",
"year"
)
) |>
dplyr::mutate(y = .data$plan_buildout / .data$scen_buildout) |>
dplyr::filter(.data$green) |>
dplyr::select(
-"plan_buildout",
-"scen_buildout",
-"green"
) |>
dplyr::filter(!is.na(.data$plan_tech_share)) |>
dplyr::mutate(y = pmax(.data$y, 0, na.rm = TRUE)) |>
dplyr::mutate(asset_class = "Corporate Bonds")

Check warning on line 136 in R/prep_company_bubble.R

View check run for this annotation

Codecov / codecov/patch

R/prep_company_bubble.R#L75-L136

Added lines #L75 - L136 were not covered by tests

dplyr::bind_rows(equity_data, bonds_data)

Check warning on line 138 in R/prep_company_bubble.R

View check run for this annotation

Codecov / codecov/patch

R/prep_company_bubble.R#L138

Added line #L138 was not covered by tests
}
112 changes: 112 additions & 0 deletions R/prep_exposure_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
prep_exposure_stats <- function(
audit_file,
investor_name,
portfolio_name,
pacta_sectors,
currency_exchange_value
) {
pacta_asset_classes <- c(
"Bonds",
"Equity"
)

Check warning on line 11 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L8-L11

Added lines #L8 - L11 were not covered by tests

audit_table <- pacta.portfolio.report:::prep_audit_table(
audit_file,
investor_name = investor_name,
portfolio_name = portfolio_name,
currency_exchange_value = currency_exchange_value
)

Check warning on line 18 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L13-L18

Added lines #L13 - L18 were not covered by tests

exposure_stats <- audit_file |>
dplyr::filter(
.data$investor_name == .env$investor_name &
.data$portfolio_name == .env$portfolio_name
) |>
dplyr::filter(.data$asset_type %in% pacta_asset_classes) |>
dplyr::filter(.data$valid_input == TRUE) |>
dplyr::mutate(
dplyr::across(
c(
"bics_sector",
"financial_sector"
),
as.character
)
) |>
dplyr::mutate(
sector =
dplyr::if_else(!.data$financial_sector %in% .env$pacta_sectors,
"Other",
.data$financial_sector
)
) |>
dplyr::summarise(
value = sum(.data$value_usd, na.rm = TRUE) / .env$currency_exchange_value,
.by = c("asset_type", "sector")
) |>
dplyr::mutate(
perc_asset_val_sector = .data$value / sum(.data$value, na.rm = TRUE),
.by = c("asset_type")
) |>
dplyr::inner_join(
audit_table,
by = dplyr::join_by("asset_type" == "asset_type_analysis")
) |>
dplyr::select(
"asset_type",
"percentage_value_invested",
"sector",
"perc_asset_val_sector"
)

Check warning on line 60 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L20-L60

Added lines #L20 - L60 were not covered by tests

asset_classes_in_portfolio <- intersect(
pacta_asset_classes,
unique(exposure_stats$asset_type)
)

Check warning on line 65 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L62-L65

Added lines #L62 - L65 were not covered by tests

all_stats_with_zero_sector_exposure <- expand.grid(
asset_type = asset_classes_in_portfolio,
sector = pacta_sectors,
val_sector = 0
) |>
dplyr::inner_join(
dplyr::distinct(
dplyr::select(
exposure_stats,
c("asset_type", "percentage_value_invested")
)
),
by = dplyr::join_by("asset_type" == "asset_type")
)

Check warning on line 80 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L67-L80

Added lines #L67 - L80 were not covered by tests

exposure_stats_all <- all_stats_with_zero_sector_exposure |>
dplyr::left_join(
exposure_stats,
by = dplyr::join_by(
"asset_type" == "asset_type",
"sector" == "sector",
"percentage_value_invested" == "percentage_value_invested"
)
) |>
dplyr::mutate(
perc_asset_val_sector = dplyr::if_else(
is.na(.data$perc_asset_val_sector),
.data$val_sector,
.data$perc_asset_val_sector
)
) |>
dplyr::mutate(
asset_type = dplyr::case_when(
.data$asset_type == "Bonds" ~ "Corporate Bonds",
.data$asset_type == "Equity" ~ "Listed Equity"
)
) |>
dplyr::select(
"asset_type",
"percentage_value_invested",
"sector",
"perc_asset_val_sector"
)

Check warning on line 109 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L82-L109

Added lines #L82 - L109 were not covered by tests

exposure_stats_all

Check warning on line 111 in R/prep_exposure_stats.R

View check run for this annotation

Codecov / codecov/patch

R/prep_exposure_stats.R#L111

Added line #L111 was not covered by tests
}
Loading
Loading