Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Last active September 25, 2025 01:55
Show Gist options
  • Save vjcitn/634e722fe2eea78449bbe3dbd2a39d13 to your computer and use it in GitHub Desktop.
Save vjcitn/634e722fe2eea78449bbe3dbd2a39d13 to your computer and use it in GitHub Desktop.
silly approach to starting napari from R
# based on https://alisterburt.com/napari-workshops/notebooks/viewer_intro.html
library(reticulate)
py_require("napari[all]")
py_require("scikit-image")
nap = import("napari")
v = nap$Viewer()
si = import("skimage")
#id = si$data$cells3d()
if (!file.exists("nuclei.tif")) {
system("wget --no-check-certificate https://raw.githubusercontent.com/alisterburt/napari-workshops/main/napari-workshops/notebooks/data/nuclei.tif")
}
if (!file.exists("cell_membranes.tif")) {
system("wget --no-check-certificate https://raw.githubusercontent.com/alisterburt/napari-workshops/main/napari-workshops/notebooks/data/cell_membranes.tif")
}
nuc = si$io$imread("nuclei.tif")
v$add_image(nuc)
mem = si$io$imread("cell_membranes.tif")
v$add_image(mem)
nap$run()
@vjcitn
Copy link
Author

vjcitn commented Sep 25, 2025

R version 4.5.1 (2025-06-13)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.6.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] C

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reticulate_1.43.0 rmarkdown_2.29   

loaded via a namespace (and not attached):
 [1] digest_0.6.37     fastmap_1.2.0     xfun_0.53         Matrix_1.7-3     
 [5] lattice_0.22-7    knitr_1.50        htmltools_0.5.8.1 png_0.1-8        
 [9] cli_3.6.5         grid_4.5.1        withr_3.0.2       compiler_4.5.1   
[13] rprojroot_2.1.1   here_1.0.1        tools_4.5.1       evaluate_1.0.5   
[17] startup_0.23.0    Rcpp_1.1.0        rlang_1.1.6       jsonlite_2.0.0  

@vjcitn
Copy link
Author

vjcitn commented Sep 25, 2025

can this help avoid blocking?

2. Using coro (async/await) with reticulate:
Python function.
Same as above. R code with coro.
The coro package provides async and await keywords in R, making asynchronous code more sequential-looking.
Code

  library(reticulate)
  library(coro)
  library(future)

  # Ensure Python environment and import module as before
  py_run_string("import sys")
  py_run_string("sys.path.append('.')")
  my_python_module <- import("my_python_module")

  # Define an async R function
  my_async_r_function <- async(function() {
    result <- await(future({
      my_python_module$long_running_task(5)
    }))
    print(paste("Python task result (async/await):", result))
  })

  # Run the async function
  my_async_r_function()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment