{"id":26947777,"url":"https://github.com/rhenkin/rforceplots","last_synced_at":"2025-06-18T02:37:33.584Z","repository":{"id":217749214,"uuid":"474916910","full_name":"rhenkin/rforceplots","owner":"rhenkin","description":"Wrapper for shapjs node package for easy force plots in R without Python dependencies","archived":false,"fork":false,"pushed_at":"2022-03-28T08:43:52.000Z","size":254,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-03T07:28:57.089Z","etag":null,"topics":["data-science","models","r-package","shap","visualization"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rhenkin.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-03-28T08:42:59.000Z","updated_at":"2023-12-15T08:56:03.000Z","dependencies_parsed_at":"2024-01-18T05:35:56.539Z","dependency_job_id":null,"html_url":"https://github.com/rhenkin/rforceplots","commit_stats":null,"previous_names":["rhenkin/rforceplots"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rhenkin/rforceplots","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhenkin%2Frforceplots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhenkin%2Frforceplots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhenkin%2Frforceplots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhenkin%2Frforceplots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rhenkin","download_url":"https://codeload.github.com/rhenkin/rforceplots/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhenkin%2Frforceplots/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260475936,"owners_count":23014943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","models","r-package","shap","visualization"],"created_at":"2025-04-02T20:27:01.916Z","updated_at":"2025-06-18T02:37:28.571Z","avatar_url":"https://github.com/rhenkin.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# rforceplots\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)\n\u003c!-- badges: end --\u003e\n\n`rforceplots` is as a stand-alone wrapper for the [shapjs](https://www.npmjs.com/package/shapjs) node package to facilitate rendering additive force plots in R. It does not require Python or the reticulate package and is compatible with objects exported by the `fastshap::explain()`, `shapr::explain()` and `shapper::individual_variable_effect()` functions. It includes Shiny bindings and also gives access to the three base visualization functions from the shapjs node package for custom SHAP explanation formats.\n\n## Installation\n\nYou can install the development version of rforceplots with:\n\n``` r\nremotes::install_github(\"rhenkin/rforceplots\")\n```\n\n## Usage\n\nThe main method of using the package is combining it with the three SHAP packages available in R,: [fastshap](https://cran.r-project.org/web/packages/fastshap/index.html), [shapr](https://cran.r-project.org/web/packages/shapr/index.html) and [shapper](https://cran.r-project.org/web/packages/shapper/index.html). With all three, using `rforceplots` comes after using an `explain` function or equivalent.\n\n### fastshap\n\nThe `fastshap` package already includes most of the plots from the original Python SHAP package. However, the force plots from `fastshap` require Python to be installed. `rforceplots` provides an alternative without the Python dependency. The example below is based on the [vignette](https://bgreenwell.github.io/fastshap/articles/forceplot.html) from the `fastshap` package.\n\n```{r fastshap_example, eval = FALSE}\n# Load required packages\nlibrary(fastshap)\nlibrary(xgboost)\nlibrary(rforceplots)\n\n# Load the Boston housing data\n# install.packages(\"pdp)\ndata(boston, package = \"pdp\")\nX \u003c- data.matrix(subset(boston, select = -cmedv))  # matrix of feature values\n\n# Fit a gradient boosted regression tree ensemble; hyperparameters were tuned \n# using `autoxgb::autoxgb()`\nset.seed(859)  # for reproducibility\nbst \u003c- xgboost(X, label = boston$cmedv, nrounds = 338, max_depth = 3, eta = 0.1,\n               verbose = 0)\n\n# Compute exact explanations for all rows\nex \u003c- explain(bst, exact = TRUE, X = X)\n\n# For combined force plot\nForcePlot(ex, X)\n\n# For individual additive force plot\nForcePlot(ex[1,], X[1,])\n```\n\n### shapr\n\nUsing `ForcePlot` with shapjs is also straightforward. If no extra argument is passed, a combined force plot is displayed. With the use of the additional `i` argument, a single additive force plot is showed, where i corresponds to the observation row index. Most of the code below is part of the example for the shapr package and is run when you call `example(\"explain\", package = \"shapr\")`.\n\n```{r shapr_example, eval = FALSE}\nlibrary(shapr)\nlibrary(rforceplots)\n\n# Load example data\ndata(\"Boston\", package = \"MASS\")\n# Split data into test- and training data\nx_train \u003c- head(Boston, -3)\nx_test \u003c- tail(Boston, 3)\n# Fit a linear model\nmodel \u003c- lm(medv ~ lstat + rm + dis + indus, data = x_train)\n explainer \u003c- shapr(x_train, model)\np \u003c- mean(x_train$mpg)\nexplanation \u003c- explain(x_test, explainer, approach = \"empirical\",\nprediction_zero = p, n_samples = 1e2)\n\n# Visualize one explanation\nForcePlot(explanation, 1)\n```\n\n### shapper\n\nAfter running the shapper pipeline, you can call `ForcePlot()` on the resulting `individual_variable_effect` object. Calling the function on the IVE object will show a force plot for the whole set of new observations to be explained, whereas using the additional `id` argument will show a single additive force plot. The `id` here corresponds to the row of the object in the original training data.\n\n```{r shapper_example, eval = FALSE}\nlibrary(shapper)\nlibrary(rforceplots)\nlibrary(DALEX)\nlibrary(randomForest)\n\nY_train \u003c- HR$status\nx_train \u003c- HR[ , -6]\n\nset.seed(123)\nmodel_rf \u003c- randomForest(x = x_train, y = Y_train)\n\np_function \u003c- function(model, data) predict(model, newdata = data, type = \"prob\")\n\nive_rf \u003c-\n  individual_variable_effect(\n    model_rf,\n    data = x_train,\n    predict_function = p_function,\n    new_observation = x_train[1:2, ],\n    nsamples = 50\n  )\n\n# Show force plot for two observations (see new_observation argument in previous function)\nForcePlot(ive_rf)\n\n# Show force plot for single observation\nForcePlot(ive_rf, id = 1, outName = \"fired\")\n```\n\n## Saving the plots\n\nThe plots included in this package are based on the `htmlwidgets` package. To save a plot as HTML (including the interactions), you can use:\n\n```{r savewidget, eval = FALSE}\n# [... previous code ...]\nforceplot_to_save \u003c- ForcePlot(...)\nhtmlwidgets::saveWidget(forceplot_to_save, \"saved_plot.html\")\n```\n\n\n## Development\n\nIf you you want to work on the `rforceplots` package, you will need [node](https://nodejs.org) and [yarn](https://yarnpkg.com) installed to set up the JavaScript dependencies, e.g. new node packages or updated versions. Once they are installed and the package.json is updated, you can run the following and proceed to change the R code as usual.\n\n```{bash yarn_commands, eval = FALSE}\nyarn install\nyarn run webpack\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhenkin%2Frforceplots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frhenkin%2Frforceplots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhenkin%2Frforceplots/lists"}