Skip to content

Repository files navigation

NPM Calculator Tool

DOI

Second release of the NPM Calculator tool. This version is designed to assess user-entered single product information against the UK NPM (2004/5) and scope for HFSS legislation around product placement.

NPM Table Calculator

This package also includes an experimental Table Calculator feature. This uses the CDRC R package nutrientprofiler which is explicitly installed in the Dockerfile. All components of the Table Calculator except server functions are defined within new component files that are present in the R folder. All server functions for the Table Calculator are defined within server.R to enable proper propagation of input and output variables.

Updating nutrientprofiler package in NPM Calculator

Occasionally, there may be updates to the nutrientprofiler package that need to be updated within this Shiny app. To pull through more up to date versions of the package you should change the version specified within the RUN line that installs the package within the Dockerfile.

# change @v2.0.0 to another version
RUN R -e 'remotes::install_github("Leeds-CDRC/nutrientprofiler@v2.0.0")'

If the change is not included in a version but rather is the most recent commit on the main branch, you can change @v2.0.0 to @main, or remove the @v2.0.0 section completely.

You can also specify a commit hash on the repository or on a fork of the repository:

RUN R -e 'remotes::install_github("Leeds-CDRC/nutrientprofiler", ref="8c5c887")'

RUN R -e 'remotes::install_github("murphyqm/edits-nutrientprofiler", ref="ab1144a")'

where ab1144a is the commit hash.

Building the app locally

Running an app locally is a useful way of testing changes and updates without affecting the live version of the webapp. In order to deploy this app locally in your web browser, you will need Docker (or a Linux macine) and a version of git installed (you can use plain git, GitHub CLI, or GitHub Desktop).

Deployment using a Docker container

After cloning the repository with git and navigating into the folder, from your command line/terminal as a Docker-enabled user (with Docker running):

# Build a container using the file "local.dockerfile"
# -t provides a tag to reference the container later
docker build -f local.dockerfile . -t npm-calculator

It will take a little while to build the docker image locally.

You can check that this has built locally with the command docker images, which should have output similar to this:

docker images

REPOSITORY       TAG       IMAGE ID       CREATED              SIZE
npm-calculator   latest    ID-code-here   About a minute ago   2.2GB

You can then run the docker image and forward the specified port from the container to the host machine you are working on, so that you can view it in a web browser:

docker run -p 3838:3838 npm-calculator

Now, visit http://localhost:3838/ to view the local application.

This local deployment has been tested on Windows, WSL2, and Linux (Ubuntu 22.04).

Deployment using a devcontainer

Alternatively, you can launch a devcontainer from VSCode by opening the folder in VSCode, launching the command pallette (F1) and selecting "Dev Container: Open folder in a container...".

It will take a few minutes for the container to build; once it has, you will be able to open a new terminal in VSCode within this contained environment. You can view your app by running the following from the terminal (within the devcontainer):

R -e "shiny::runApp(host='0.0.0.0', port=3838)"

Note that unlike the Docker container for deployment, the app files are not copied across to /srv/shiny-server/ and the shiny app is launched from the main repository directory.

Alternatively, you can launch an interactive R session, and then run:

shiny::runApp(host='0.0.0.0', port=3838)

Deployment using a devcontainer on GitHub codespaces

You can launch the devcontainer using the cloud platform GitHub codespaces. Launching the project is possible from the repository main page on GitHub, from the "Code" tab (where you can copy the link to clone the repository). A sub-tab in the pop-up dialogue will prompt you to create a new codespace on main.

Deployment on Linux in an environment

You can also deploy locally from a Linux machine (or WSL2 in Windows) where R and git are installed.

You can create an R env using the renv package, or you can build a virtual environment using conda or an alternative package manager.

If using conda/mamba, create your environment using the conda-env.yml file:

conda env create --file conda-env.yml

Then, you can activate this env (conda activate npm-env) and use the remotes package to install the nutrientprofiler package:

R -e 'remotes::install_github("Leeds-CDRC/nutrientprofiler@v2.0.0")'

# Or again, a specific commit:
R -e 'remotes::install_github("Leeds-CDRC/nutrientprofiler", ref="8c5c887")'

Note this only needs to be done the first time the environment is activated.

And then run the app from the project directory:

R -e "shiny::runApp(host='0.0.0.0', port=3838)"

New feature: collecting use info

In order to store the output of the user feedback, Azure needs the SAS token for the storage account. This can be regenerated/rotated for security.

N.B. if rotated/regenerated, you will also need to update the token used for the app feedback monitor where it is saved as a Repository Secret (APP_FEEDBACK_URL) (n.b. this is the SAS token added to the end of the table storage url).

When running the app locally, the keys will not be available and an error message will show.

To regenerate the Azure tokens:

  1. In the appuserfeedback storage account on Azure, go to Security + networking > Access keys > Rotate key
  2. Then, Security + networking > Shared access signature > Create a new access signature:
    • Deselect all services except Table
    • Deselect all resources except Object
    • Deselect all permissions except Add
    • Set start and expiry date/time
    • Select a key under Signing key
    • Click generate and copy the SAS token
  3. Go to the NPM Calculator test app service
  4. Go to Settings > Environment variables > AZURE_TABLE_SAS_TOKEN and update, pasting in the new SAS token.
  5. Give the app a few minutes to restart, and test. If successful, repeat with the production version.

Note that the tokens should not be stored locally, and do not need to be added to the secrets of the GitHub repository.

License

The NPM calculator and underlying nutrientprofiler R package provide functions to help assess product information against the UK Nutrient Profiling Model (2004/5) and scope for HFSS legislation around product placement. It is designed to provide low level functions that implement UK Nutrient Profiling Model scoring that can be applied across product datasets.

Copyright (C) 2024 University of Leeds

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

A copy of the GNU Affero General Public License is supplied along with this program in the LICENSE file in the repository. You can also find the full text at https://www.gnu.org/licenses.

You can contact us by raising an issue on our GitHub repository (https://github.com/Leeds-CDRC/NPM-Calculator/issues/new - login required) or by emailing us at info@cdrc.ac.uk.

Releases

Used by

Contributors

Languages