knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
ormr
ormr
allows to work with Python installed to a custom location,
using only a single point of contact.
library(ormr)
We use only one point of contact, ormr_folder_name
,
which is the folder where ormr
installs the Python environment and packages
needed:
if (is_conda_installed()) { ormr_folder_name <- get_default_ormr_folder_name() } else { ormr_folder_name <- "python3" }
Note we use the default ormr_folder_name
, as this package
needs to only use one Python version, in both this
vignette and its tests.
The goal of this vignette is to show how to use ormr
to run an example Python script,
where Python is installed to a custom location.
Here is the example Python script we want to run:
python_script_path <- system.file("extdata", "scipy_example.py", package = "ormr") readLines(python_script_path)
As a spoiler, it is demonstrated here that the script maybe cannot be run yet:
try( run_python_script( ormr_folder_name = ormr_folder_name, python_script_path = python_script_path ), silent = FALSE )
When using the default ormr
Conda environment, scipy
may actually be installed.
These are the steps to achieve the goal:
Note that there is no need to create a Conda environment:
this is what ormr
does for you. ormr
uses eager loading,
which is the opposite of reticulate
's lazy loading.
Currently, these packages are installed:
installed_python_packages <- ormr::get_installed_python_packages( ormr_folder_name = ormr_folder_name ) knitr::kable(installed_python_packages)
We are going to install scipy
, which is a small package:
package_name <- "scipy"
We can verify that it may not be installed yet:
is_python_package_installed( ormr_folder_name = ormr_folder_name, package_name = package_name )
The package may be installed when using the default
ormr
Conda environment.
Also, running an Python script that requires scipy
may fail now:
try( run_python_script( ormr_folder_name = ormr_folder_name, python_script_path = python_script_path ), silent = FALSE )
The script may pass when using the default
ormr
Conda environment and the needed Python packages
are already installed.
Here, we install the package:
ormr::install_python_packages( ormr_folder_name = ormr_folder_name, package_names = package_name )
Now the Python package is installed:
testthat::expect_true( ormr::is_python_package_installed( ormr_folder_name = ormr_folder_name, package_name = package_name ) )
Great, with packages in place, we run a Python script with those packages:
testthat::expect_silent( ormr::run_python_script( ormr_folder_name = ormr_folder_name, python_script_path = python_script_path ) )
No error, so we can conclude it works!
The clean up, we throw away the ormr
folder in which the magic happened.
if (ormr_folder_name != get_default_ormr_folder_name()) { unlink(ormr_folder_name, recursive = TRUE) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.