Skip to content

Commit

Permalink
Scan deps: support IPython notebooks (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi authored Jan 7, 2025
1 parent e5bee35 commit 99fe52c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
28 changes: 28 additions & 0 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ scan_path_deps_do <- function(code, path) {
".rmarkdown" = ,
".rmd" = scan_path_deps_do_rmd(code, path),
".rnw" = scan_path_deps_do_rnw(code, path),
".ipynb" = scan_path_deps_do_ipynb(code, path),
"DESCRIPTION" = scan_path_deps_do_dsc(code, path),
"NAMESPACE" = scan_path_deps_do_namespace(code, path),
"_bookdown.yml" = scan_path_deps_do_bookdown(code, path),
Expand Down Expand Up @@ -1231,3 +1232,30 @@ scan_path_deps_do_rnw_ranges <- function(code) {
c(bmx, emx)
})
}

# -------------------------------------------------------------------------

scan_path_deps_do_ipynb <- function(code, path) {
ipynb <- jsonlite::fromJSON(code, simplifyVector = FALSE)
if (!identical(ipynb$metadata$kernelspec$language, "R")) {
return(NULL)
}
ir <- if (identical(ipynb$metadata$kernelspec$name, "ir")) {
scan_deps_df(
path = path,
package = "IRkernel",
code = NA_character_
)
}

deps <- lapply(ipynb$cells, function(cell) {
if (identical(cell$cell_type, "code")) {
c1 <- paste(unlist(cell$source), collapse = "")
scan_path_deps_do_r(c1, path)
}
})

adeps <- drop_nulls(c(list(ir), deps))

do.call("rbind", adeps)
}
13 changes: 13 additions & 0 deletions tests/testthat/_snaps/scan-deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,16 @@
5 ignore-test.Rnw good good * prod library(good) 1 1 1
6 ignore-test.Rnw good good * prod library(good) 1 1 1

# IPython notebook

Code
scan_path_deps_do(readLines(path), basename(path))
Output
# A data frame: 4 x 9
path ref package version type code start_row start_column start_byte
<chr> <chr> <chr> <chr> <chr> <chr> <int> <int> <int>
1 notebook.ipynb IRkernel IRkernel * prod <NA> 1 1 1
2 notebook.ipynb MASS MASS * prod library(MASS) 1 1 1
3 notebook.ipynb stats stats * prod stats::setNames 1 1 1
4 notebook.ipynb cli cli * prod cli::cli_text 2 1 19

63 changes: 63 additions & 0 deletions tests/testthat/fixtures/scan/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some markdown text"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"library(MASS)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"stats::setNames()\n",
"cli::cli_text()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.0.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
8 changes: 8 additions & 0 deletions tests/testthat/test-scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,11 @@ test_that("Ignored chunks in .Rnw file", {
)
})
})

test_that("IPython notebook", {
local_reproducible_output(width = 500)
path <- test_path("fixtures/scan/notebook.ipynb")
expect_snapshot({
scan_path_deps_do(readLines(path), basename(path))
})
})

0 comments on commit 99fe52c

Please sign in to comment.