R/read_notebook.R

Defines functions read_notebook

Documented in read_notebook

#' Read a Jupyter Notebook as a `tibble`.
#'
#' @param file_path A length 1 character vector pointing to the notebook file.
#'
#' @return A \code{\link{tibble}} with one observation per cell in the notebook
#'   and the following variables:
#'   - **cell_type**: The type of cell in the notebook ("code" or "markdown").
#'   - **metadata**: The cell's metadata as a named list.
#'   - **source**: The source text for the cell as a character string. The
#'     source is processed with \code{\link{clean_source}}.
#'   - **execution_count**: For code cells, how times the cell was executed.
#'
#' The `tibble` also has the following attributes:
#'   - **metadata**: The notebook level metadata.
#'   - **nbformat**, **nbformat_minor**: The major and minor versions of the
#'     notebook format, e.g. "4.2" where "4" is major and "2" is minor.
#' @export
read_notebook <- function(file_path) {
  notebook_list <- jsonlite::read_json(file_path)
  notebook_metadata <- notebook_list$metadata
  notebook_metadata$nbformat <- notebook_list$nbformat
  notebook_metadata$nbformat_minor <- notebook_list$nbformat_minor
  notebook <- dplyr::tibble(cell = notebook_list$cells) %>%
    tidyr::unnest_wider('cell') %>%
    {attributes(.)$metadata <- notebook_metadata; .} %>%
    {attributes(.)$file_path <- fs::path_file(file_path); .} %>%
    {attributes(.)$last_edit <- as.character(fs::file_info(file_path)$modification_time[[1]]); .}

  notebook$source <- clean_source(notebook$source, notebook$cell_type)

  notebook
}
adamblake/nbsimplegrader_companion documentation built on April 19, 2020, 6:05 p.m.