File: htmlTable_helpers_isNotebook.R

package info (click to toggle)
r-cran-htmltable 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,600 kB
  • sloc: javascript: 6,797; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,019 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#' Detects if the call is made from within an RStudio Rmd file or a file
#' with the html_notebook output set.
#' @importFrom rstudioapi isAvailable getActiveDocumentContext
#' @keywords internal
prIsNotebook <- function() {
  if (!isAvailable()) {
    return(FALSE)
  }

  ctxt <- getActiveDocumentContext()
  if (grepl("\\.Rmd$", ctxt$path)) {
    return(prCheck4output2console(ctxt))
  }

  # Look for html_notebook within the header if the file hasn't been saved
  contents <- ctxt$contents
  header <- grep("^---$", contents)
  if (length(header) == 2) {
    return(any(grepl(
      "html_notebook$",
      contents[min(header):max(header)]
    )))
  }

  return(FALSE)
}

prCheck4output2console <- function(ctxt) {
  contents <- ctxt$contents
  header_boundary <- grep("^---$", contents)
  if (length(header_boundary) <= 1) {
    # Play it safe if the header is invalid
    return(TRUE)
  }

  header <- contents[header_boundary[1]:header_boundary[2]]
  return(!any(grepl("chunk_output_type: console", header)))
}