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

18/convert to package #30

Merged
merged 17 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
^.*\.Rproj$
^.dockerignore$
^.github/
^.lintr$
^Dockerfile$
^LICENSE\.md$
^\.Rproj\.user$
^docker-compose.yml$
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
**/*.Rproj
**/*.md
**/.git
**/.github
**/.gitignore
**/Dockerfile
analysis_output_dir
analysis_output_dir/**/*
benchmarks_dir
benchmarks_dir/**/*
dashboard_data_dir
dashboard_data_dir/**/*
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
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.env
.Rproj.user
inputs/
outputs/
data/
.Rhistory
analysis_output_dir/**/*
benchmarks_dir/**/*
dashboard_data_dir/**/*
12 changes: 12 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
linters: linters_with_defaults(
cyclocomp_linter = NULL,
line_length_linter = NULL,
indentation_linter = NULL,
object_usage_linter = NULL,
trailing_whitespace_linter = NULL,
object_length_linter = NULL,
assignment_linter = NULL,
commas_linter = NULL,
trailing_blank_lines_linter = NULL,
commented_code_linter = NULL
)
18 changes: 13 additions & 5 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 @@ -22,10 +27,13 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
dplyr,
glue,
jsonlite,
pacta.portfolio.report,
magrittr,
pacta.portfolio.utils,
readr
readr,
rlang,
stringr,
tidyr
Remotes:
RMI-PACTA/pacta.portfolio.report,
RMI-PACTA/pacta.portfolio.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.
39 changes: 39 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by roxygen2: do not edit by hand

importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,case_when)
importFrom(dplyr,desc)
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,first)
importFrom(dplyr,group_by)
importFrom(dplyr,if_else)
importFrom(dplyr,inner_join)
importFrom(dplyr,join_by)
importFrom(dplyr,lag)
importFrom(dplyr,last)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_at)
importFrom(dplyr,n)
importFrom(dplyr,pull)
importFrom(dplyr,reframe)
importFrom(dplyr,rename)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
importFrom(dplyr,slice)
importFrom(dplyr,summarise)
importFrom(dplyr,tibble)
importFrom(dplyr,transmute)
importFrom(dplyr,ungroup)
importFrom(magrittr,"%>%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,.env)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,unite)
12 changes: 12 additions & 0 deletions R/choose_dictionary_language.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
choose_dictionary_language <-
function(data, language) {
language <- tolower(language)

Check warning on line 3 in R/choose_dictionary_language.R

View check run for this annotation

Codecov / codecov/patch

R/choose_dictionary_language.R#L3

Added line #L3 was not covered by tests

data %>%
transmute(
.data$id_data,
.data$id_column,
translate_key = .data$key,
translate_value = .data[[language]]
)

Check warning on line 11 in R/choose_dictionary_language.R

View check run for this annotation

Codecov / codecov/patch

R/choose_dictionary_language.R#L5-L11

Added lines #L5 - L11 were not covered by tests
}
12 changes: 12 additions & 0 deletions R/filter_scenarios_per_sector.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
filter_scenarios_per_sector <-
function(data, select_scenario_other, select_scenario) {
special_sectors <- c("Aviation")
rest_of_sectors <- setdiff(unique(data$ald_sector), special_sectors)

Check warning on line 4 in R/filter_scenarios_per_sector.R

View check run for this annotation

Codecov / codecov/patch

R/filter_scenarios_per_sector.R#L3-L4

Added lines #L3 - L4 were not covered by tests

data %>%
filter(
scenarios_found_in_sectors(.data, select_scenario_other, c("Aviation")) |
scenarios_found_in_sectors(.data, select_scenario, rest_of_sectors)
)

Check warning on line 10 in R/filter_scenarios_per_sector.R

View check run for this annotation

Codecov / codecov/patch

R/filter_scenarios_per_sector.R#L6-L10

Added lines #L6 - L10 were not covered by tests
}

4 changes: 4 additions & 0 deletions R/get_scenario.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
get_scenario <- function(scenario_parameter) {
scenario <- unlist(stringr::str_split(scenario_parameter,"_", n = 2))[2]
scenario

Check warning on line 3 in R/get_scenario.R

View check run for this annotation

Codecov / codecov/patch

R/get_scenario.R#L2-L3

Added lines #L2 - L3 were not covered by tests
}
4 changes: 4 additions & 0 deletions R/get_scenario_source.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
get_scenario_source <- function(scenario_parameter) {
source <- unlist(stringr::str_split(scenario_parameter,"_", n = 2))[1]
source

Check warning on line 3 in R/get_scenario_source.R

View check run for this annotation

Codecov / codecov/patch

R/get_scenario_source.R#L2-L3

Added lines #L2 - L3 were not covered by tests
}
127 changes: 127 additions & 0 deletions R/prep_audit_table.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
prep_audit_table <-
function(audit_file, investor_name, portfolio_name, currency_exchange_value) {
sort_order <- c(
"Bonds",
"Equity",
"Other",
"Unclassified"
)

Check warning on line 8 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L3-L8

Added lines #L3 - L8 were not covered by tests

audit_table_init <-
audit_file %>%
filter(
.data$investor_name == .env$investor_name,
.data$portfolio_name == .env$portfolio_name
) %>%
mutate(asset_type = if_else(
.data$valid_input, .data$asset_type, "Unknown"
)) %>%
mutate(is_included = if_else(
.data$asset_type %in% c("Others", "Funds"), FALSE, .data$valid_input
)) %>%
mutate(included = if_else(.data$is_included, "Yes", "No")) %>%
mutate(asset_type_analysis = case_when(
.data$asset_type %in% c("Bonds", "Equity") ~ .data$asset_type,
.data$asset_type == "Others" ~ "Other",
(.data$asset_type == "Funds") & (.data$direct_holding) ~ "Other",
TRUE ~ "Unclassified"
)) %>%
mutate(
asset_type_analysis =
factor(.data$asset_type_analysis, levels = .env$sort_order)
) %>%
mutate(value_usd = if_else(.data$value_usd < 0, 0, .data$value_usd)) %>%
mutate(value_usd = .data$value_usd / .env$currency_exchange_value)

Check warning on line 34 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L10-L34

Added lines #L10 - L34 were not covered by tests

included_table_totals <-
audit_table_init %>%
group_by(.data$asset_type_analysis, .data$included) %>%
summarise(
total_value_invested = sum(.data$value_usd, na.rm = TRUE),
.groups = "drop"
) %>%
mutate(
percentage_value_invested =
.data$total_value_invested / sum(.data$total_value_invested)
)

Check warning on line 46 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L36-L46

Added lines #L36 - L46 were not covered by tests

included_table_value_breakdown <-
audit_table_init %>%
mutate(investment_means = case_when(
(.data$asset_type == "Funds") & (.data$direct_holding) ~ "Unidentified Funds",
.data$direct_holding ~ "Direct",
!.data$direct_holding ~ "Via a Fund"
)) %>%
group_by(.data$asset_type_analysis, .data$investment_means) %>%
summarise(
value_invested = sum(.data$value_usd, na.rm = TRUE),
.groups = "drop"
)

Check warning on line 59 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L48-L59

Added lines #L48 - L59 were not covered by tests

fields_totals <-
c(
"asset_type_analysis",
"included",
"total_value_invested",
"percentage_value_invested"
)

Check warning on line 67 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L61-L67

Added lines #L61 - L67 were not covered by tests

included_table_per_asset <-
included_table_totals %>%
left_join(included_table_value_breakdown, by = "asset_type_analysis") %>%
remove_duplicate_entries_totals(fields_totals) %>%
select(
"asset_type_analysis",
"total_value_invested",
"percentage_value_invested",
"included",
"value_invested",
"investment_means"
)

Check warning on line 80 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L69-L80

Added lines #L69 - L80 were not covered by tests

sum_table <-
included_table_per_asset %>%
summarise(
asset_type_analysis = "Total",
total_value_invested = sum(.data$total_value_invested, na.rm = TRUE),
percentage_value_invested = sum(.data$percentage_value_invested, na.rm = TRUE),
included = NA,
value_invested = sum(.data$value_invested, na.rm = TRUE),
investment_means = NA
)

Check warning on line 91 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L82-L91

Added lines #L82 - L91 were not covered by tests

bind_rows(included_table_per_asset, sum_table)

Check warning on line 93 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L93

Added line #L93 was not covered by tests
}


equal_adjacent_fields_totals <-
function(table, fields_totals, idx) {
are_equal <- TRUE
for (field in fields_totals) {
are_equal <- are_equal &&
(pull(slice(table, idx - 1), field) == pull(slice(table, idx), field))

Check warning on line 102 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L99-L102

Added lines #L99 - L102 were not covered by tests
}
are_equal

Check warning on line 104 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L104

Added line #L104 was not covered by tests
}


remove_duplicate_entries_totals <-
function(table, fields_totals) {
for (asset in unique(table$asset_type_analysis)) {
idx_asset <-
table %>%
mutate(is_chosen_asset = .data$asset_type_analysis == .env$asset) %>%
pull(.data$is_chosen_asset) %>%
which()

Check warning on line 115 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L110-L115

Added lines #L110 - L115 were not covered by tests

if (length(idx_asset) >= 2) {
for (i in length(idx_asset):2) {
idx <- idx_asset[i]
if (equal_adjacent_fields_totals(table, fields_totals, idx)) {
table[idx, fields_totals] <- NA

Check warning on line 121 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L117-L121

Added lines #L117 - L121 were not covered by tests
}
}
}
}
table

Check warning on line 126 in R/prep_audit_table.R

View check run for this annotation

Codecov / codecov/patch

R/prep_audit_table.R#L126

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