Determine if a script works in an RStudio notebook

In my htmlTable package , I used base::interactive to determine if the output is displayed as a string or in a browser window (here code ). Unfortunately, interactive() returns TRUE when working in RStudio notebook , which is equivalent to the method of determining the runtime inside the laptop?

I tried ls() and search() , but they look the same regardless of environment. I also have a knit_print.htmlTable S3 function that is called by default for knitr , but it doesn't seem to detect it properly in a laptop environment,

+5
source share
1 answer

Thus, the best solution I have found so far is to implement a context analyzer using the RStudio API:

 prIsNotebook <- function() { if (!rstudioapi::isAvailable()) { return(FALSE) } ctxt <- rstudioapi::getActiveDocumentContext() if (grepl("\\.Rmd$", ctxt$path)) { return(TRUE) } # 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) } 

I will not check this as an answer, since it is more of a hack than a true solution.

0
source

Source: https://habr.com/ru/post/1261428/


All Articles