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

Address locked binding issue #309

Merged
merged 4 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 17 additions & 12 deletions aif360/aif360-r/R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
#'
load_aif360_lib <- function() {
e <- globalenv()
e$datasets <- import("aif360.datasets")
e$metrics <- import("aif360.metrics")
e$pre_algo <- import("aif360.algorithms.preprocessing")
e$in_algo <- import("aif360.algorithms.inprocessing")
e$post_algo <- import("aif360.algorithms.postprocessing")
e$tf <- import("tensorflow")
lockBinding("datasets", e)
lockBinding("metrics", e)
lockBinding("pre_algo", e)
lockBinding("in_algo", e)
lockBinding("post_algo", e)
lockBinding("tf", e)
bindings <- c("datasets", "metrics", "pre_algo", "in_algo", "post_algo", "tf")
if (!all(bindings %in% ls(e))){
e$datasets <- import("aif360.datasets")
e$metrics <- import("aif360.metrics")
e$pre_algo <- import("aif360.algorithms.preprocessing")
e$in_algo <- import("aif360.algorithms.inprocessing")
e$post_algo <- import("aif360.algorithms.postprocessing")
e$tf <- import("tensorflow")
lockBinding("datasets", e)
lockBinding("metrics", e)
lockBinding("pre_algo", e)
lockBinding("in_algo", e)
lockBinding("post_algo", e)
lockBinding("tf", e)
} else {
message(paste0(c("The aif360 functions have already been loaded:", bindings), sep = " "))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Two suggestions.

  • Let's not display all the functions as it is not required for the users
  • Expand the message to something like The aif360 functions have already been loaded. You can begin using the package

}
}
2 changes: 2 additions & 0 deletions aif360/aif360-r/README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ load_aif360_lib()
load_aif360_lib()
```

If you get an error: `cannot change value of locked binding`, please restart the R session and run `load_aif360_lib()` exactly once.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add function to load the library.


``` r
# load a toy dataset
data <- data.frame("feature1" = c(0,0,1,1,1,1,0,1,1,0),
Expand Down
35 changes: 15 additions & 20 deletions aif360/aif360-r/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<!-- badges: start -->

[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/aif360)](https://cran.r-project.org/package=aif360)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/aif360)](https://cran.r-project.org/package=aif360)
<!-- badges: end -->

## Overview
Expand All @@ -31,7 +31,7 @@ Or install the development version from GitHub:
devtools::install_github("Trusted-AI/AIF360/aif360/aif360-r")
```

Then, use the install\_aif360() function to install AIF360:
Then, use the install_aif360() function to install AIF360:

``` r
library(aif360)
Expand All @@ -42,7 +42,7 @@ install_aif360()

AIF360 is distributed as a Python package and so needs to be installed
within a Python environment on your system. By default, the
install\_aif360() function attempts to install AIF360 within an isolated
install_aif360() function attempts to install AIF360 within an isolated
Python environment (“r-reticulate”).

You can check using `reticulate::conda_python()` and
Expand All @@ -53,42 +53,36 @@ You can check using `reticulate::conda_python()` and
1) Install reticulate and check if you have miniconda installed. If you
do, go to step 2.

<!-- end list -->

``` r
install.packages("reticulate")
reticulate::conda_list()
```

If you get an error: `Error: Unable to find conda binary. Is Anaconda
installed?`, please install miniconda
If you get an error:
`Error: Unable to find conda binary. Is Anaconda installed?`, please
install miniconda

``` r
reticulate::install_miniconda()
```

If everything worked, you should get the message:

`* Miniconda has been successfully installed at
'/home/rstudio/.local/share/r-miniconda'.`
`* Miniconda has been successfully installed at '/home/rstudio/.local/share/r-miniconda'.`

You can double check:

reticulate::conda_list()

You will get something like this:

```
name python
1 r-miniconda /home/rstudio/.local/share/r-miniconda/bin/python
2 r-reticulate /home/rstudio/.local/share/r-miniconda/envs/r-reticulate/bin/python
```
name python
1 r-miniconda /home/rstudio/.local/share/r-miniconda/bin/python
2 r-reticulate /home/rstudio/.local/share/r-miniconda/envs/r-reticulate/bin/python

2) You can create a new conda env and then configure which version of
Python to use:

<!-- end list -->

``` r
reticulate::conda_create(envname = "r-test")
reticulate::use_miniconda(condaenv = "r-test", required = TRUE)
Expand All @@ -99,8 +93,6 @@ Check that everything is working `reticulate::py_config()`.
3) If you haven’t yet, please install the aif360 package
`install.packages("aif360")` and then install aif360 dependencies

<!-- end list -->

``` r
aif360::install_aif360(envname = "r-test")
```
Expand All @@ -110,7 +102,7 @@ restart.

4) Finally, load the aif360 functions

<!-- end list -->
<!-- -->

library(aif360)
reticulate::use_miniconda(condaenv = "r-test", required = TRUE)
Expand All @@ -122,11 +114,14 @@ restart.
load_aif360_lib()
```

If you get an error: `cannot change value of locked binding`, please
restart the R session and run `load_aif360_lib()` exactly once.

``` r
# load a toy dataset
data <- data.frame("feature1" = c(0,0,1,1,1,1,0,1,1,0),
"feature2" = c(0,1,0,1,1,0,0,0,0,1),
"label" = c(1,0,0,1,0,0,1,0,1,1))
"label" = c(1,0,0,1,0,0,1,0,1,1))

# format the dataset
formatted_dataset <- aif360::aif_dataset(data_path = data,
Expand Down