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

mrc-2432 Implement optimiser to strategize across regions #57

Merged
merged 2 commits into from Jun 8, 2021

Conversation

mwoodbri
Copy link
Contributor

@mwoodbri mwoodbri commented Jun 4, 2021

This refactors Dirk's code as a function to be called by the optimise endpoint (which will be implemented in mrc-2433)

Any feedback whatsoever on my novice R code very welcome. Biggest challenge was getting dplyr to pass make check - ideas for tidying this up without making it unreadable gratefully received. We inherited dplyr and could probably do without it but would take me a while to translate into pure R...

@codecov
Copy link

codecov bot commented Jun 4, 2021

Codecov Report

Merging #57 (ec516b4) into master (9369876) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #57   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         6    +1     
  Lines          375       409   +34     
=========================================
+ Hits           375       409   +34     
Impacted Files Coverage Δ
R/optimise.R 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9369876...ec516b4. Read the comment docs.

DESCRIPTION Outdated
thor
thor,
dplyr,
tibble,
Copy link
Contributor Author

@mwoodbri mwoodbri Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a dependency of dplyr but seemingly needs to be specified explicitly here for make check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually put those things in Suggests (and typically order these lists alphabetically as they're easier to maintain that way)

R/optimise.R Show resolved Hide resolved
R/optimise.R Outdated
@@ -0,0 +1,44 @@
library(dplyr, warn.conflicts = FALSE)

utils::globalVariables(c("zone", "intervention", "total_costs", "total_cases_averted", "value", "i", "j"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to avoid this using .data$ but it got very verbose, and I still had trouble eliminating all the warnings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other thing you can do is to scope these variables within the function so do:

function(...) {
  zone <- intervention <- NULL # used by dplyr
  df %>% select(zone) # etc
}

There's no great way of doing this though. The .data$ trick is the canonical way but it's not nice to read

@@ -0,0 +1,43 @@
library(ROI.plugin.glpk)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of the correct way to do this - it's only required for its side-effects (i.e. loading the plugin), and apparently needs to be done in the context of where the code is invoked, not defined (i.e. it isn't sufficient to put this line in optimise.R.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done by adding

##' @import ROI.plugin.glpk

somewhere near in R/optimise.R and rerun devtools::document() so that NAMESPACE contains an import call. That will mean that mintr can find the plugin

@mwoodbri mwoodbri requested a review from richfitz June 7, 2021 08:08
DESCRIPTION Outdated
thor
thor,
dplyr,
tibble,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually put those things in Suggests (and typically order these lists alphabetically as they're easier to maintain that way)

R/optimise.R Outdated
@@ -0,0 +1,44 @@
library(dplyr, warn.conflicts = FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no calls to library() within package code. I presume this is working around something with dplyr's DSL? The import that you have in the NAMESPACE should sort this?

R/optimise.R Outdated
@@ -0,0 +1,44 @@
library(dplyr, warn.conflicts = FALSE)

utils::globalVariables(c("zone", "intervention", "total_costs", "total_cases_averted", "value", "i", "j"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other thing you can do is to scope these variables within the function so do:

function(...) {
  zone <- intervention <- NULL # used by dplyr
  df %>% select(zone) # etc
}

There's no great way of doing this though. The .data$ trick is the canonical way but it's not nice to read

R/optimise.R Show resolved Hide resolved
@@ -0,0 +1,43 @@
library(ROI.plugin.glpk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done by adding

##' @import ROI.plugin.glpk

somewhere near in R/optimise.R and rerun devtools::document() so that NAMESPACE contains an import call. That will mean that mintr can find the plugin

tests/testthat/test-optimise.R Show resolved Hide resolved
@mwoodbri mwoodbri requested a review from richfitz June 7, 2021 11:42
@richfitz richfitz merged commit 874d3f6 into master Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants