Tutorial#
One of the main things that conda apart from other python packaging tool solutions is its ability to work with other languages and toolchains. Conda is happy to mix Python with packages from R, Java, C, etc. This tutorial explores using conda-project to create a Shiny app using both Jupyter notebook on the Python side, and shiny on the R side. After completing this tutorial, you will be able to:
Understand the relationship between conda-project commands and their corresponding actions.
Add a custom command to a project.
Use the
runaction to launch external processes.
If conda-project is not yet installed and started Project, follow the Installation Instructions.
Create a new project#
This section of the tutorial explores the init and activate subcommands of conda-project.
Open a Command Prompt or terminal window.
Initialize the project in a new directory:
conda-project init -n learn-cp --directory cp-tutorial --platform osx-arm64,linux-64 -c defaults -c conda-forge \ python=3.10 notebook r-essentials r-plotly r-shiny
At this point, conda-project will create a new directory, and write environment, lock, and project files to the new directory. This example passes the
osx-arm64andlinux-64platform architectures. This has the effect of speeding up the package resolution and lock file generation. If the--platformargument is omitted, package resolution will be calculated across conda-project’s default list of platform architectures.Note
This tutorial issues the
initaction for a new project. As noted in the User Guide, it is also possible to create a project from an existing environment file.If using windows, change the
--platformargument to point to the correct architecture (win-64).Navigate the newly created project directory:
cd cp-tutorial
Investigate the contents of the newly created environment file:
$ cat environment.yml channels: - defaults - conda-forge dependencies: - python=3.10 - notebook - r-essentials - r-plotly - r-shiny platforms: - osx-arm64 - linux-64
Note
Refer to conda-project create for a detailed list of options.
Investigate the contents of the newly created conda project file:
$ cat conda-project.yml name: learn-cp environments: default: - environment.yml variables: {} commands: {}
Note that the
namekey is populated with the value supplied in theinitaction.An upcoming section of the tutorial will involve adding a custom command to the
commandskey. It’s also possible to define variables using thevariableskey. These key varlue pairs are loaded as overrides to the inherited execution environment when arunaction is issued.Initiate the
installaction on the project:$ conda-project install
Note
As noted in a preceding section, the
installaction will initiate conda install for the active environment.Note
Running the install step is not strictly required. Calling the
runcommand such as the one included in the next section, will result ininstallbeing called automatically.
Create an example notebook-based shiny app#
In this section of the tutorial, Jupyter notebook will be used to develop and refine the shiny app.
Use
conda-projectto launch Jupyer notebook.$ conda-project run jupyter notebook
Start a
Rkernel by clicking New and select the option labeled R.Paste the following content into the first cell of the notebook and then run the cell.
library(shiny) library(data.table) library(ggplot2) ui <- fluidPage( titlePanel('Auto MPG'), sidebarPanel( radioButtons("origin", h3("Origin"), choices = list("Asia" = "Asia"), selected = "Asia") ), mainPanel(plotOutput(outputId = "distPlot")) ) server <- function(input, output) { df = fread('http://bit.ly/autompg-csv') output$distPlot <- renderPlot({ ggplot(data = df[origin==input$origin], mapping = aes(mpg)) + geom_density() }) } shinyApp(ui = ui, server = server)
A link will appear at the bottom of the cell once the shiny app is being served. Click the link and verify the output.
With the first cell selected, click the Stop button to stop the server.
Modify the content in the first cell by adding the US and Europe to the choices list in the UI:
choices = list("Asia" = "Asia", "Europe" = "Europe", "US" = "US"),
Run the cell again. When the server link appears, navigate to the shiny App and verify the code update is reflected in the UI.
Finally, save the verified R code to a file called
app.Rin the project directory.
Note
To exit the running Jupyter Notebook program press CTRL+C in the terminal or command line application.
Add and run a project command#
Register a new command to launch the Shiny app:
Using a preferred editor, modify conda-project.yml by replacing the line
comands: {}with the following:commands: shiny: Rscript -e "shiny::runApp('.', port=8086, host='0.0.0.0')"
Use
conda-projectto run the newly added command using therunaction:$ conda-project run shiny
The application should now be running and available at http://localhost:8086/. To close the running program, press CTRL+C in your terminal or command line.