Reputation: 121
I am working on slides where I want to show the output generated by debugger()
and exit. Kniting, gets stuck on the chunk as it is waiting for user input which, in my case, would be 0
. Any way to force RStudio to knit in the interactive mode?
Upvotes: 0
Views: 502
Reputation: 7790
Try using the R package
subprocess to run some code
in a seperate R session. The following example has been built using code
snippets provided in the 'intro' vignette from the subprocess package and the
example provided in the help file for debugger
.
A buy running the debugger
code in a child process you can script the interaction needed within the R code chunks of your primary document.
Example .Rmd file:
---
title: 'Debugger in a knited doc'
output: html_document
---
```{r label='internal-setup', include=FALSE}
knitr::opts_chunk$set(collapse = TRUE)
```
Try using the R package
[subprocess](https://cran.r-project.org/package=subprocess) to run some code
in a seperate R session. The following example has been built using code
snippets provided in the 'intro' vignette from the subprocess package and the
example provided in the help file for `debugger`.
The first thing we will do is build a function for calling the R binary.
```{r label="setup_subprocess"}
library(subprocess)
R_binary <- function () {
R_exe <- ifelse (tolower(.Platform$OS.type) == "windows", "R.exe", "R")
return(file.path(R.home("bin"), R_exe))
}
```
Spawning a child R process is done as follows:
```{r label="spawning"}
child_r <- subprocess::spawn_process(R_binary(), c("--vanilla"))
Sys.sleep(2) # allow sufficient time for the child R process to start
subprocess::process_read(child_r)$stdout
```
We will write some code as a character string and send it to the child
process.
```{r }
unlink("testdump.rda")
code <-
'
options(error = quote(dump.frames("testdump", TRUE)))
f <- function() {
g <- function() stop("test dump.frames")
g()
}
f() # will generate a dump on file "testdump.rda"
options(error = NULL)
'
invisible(subprocess::process_write(child_r, code))
subprocess::process_read(child_r)$stdout
```
Now, you can load the dump into the child process and start the debugger.
```{r }
code <-
'
load("testdump.rda")
debugger(testdump)
'
invisible(subprocess::process_write(child_r, code))
subprocess::process_read(child_r)$stdout
```
Say you want to start walking through the debugger for `f(1)`
```{r }
invisible(subprocess::process_write(child_r, "1\n"))
subprocess::process_read(child_r)$stdout
```
You can continue to walk through the debugging as needed via the child
process.
Don't forget to terminate the process
```{r }
subprocess::process_terminate(child_r)
```
This document resulted in an .html page that looked like this:
Try using the R package subprocess to run some code in a seperate R session. The following example has been built using code snippets provided in the ‘intro’ vignette from the subprocess package and the example provided in the help file for debugger.
The first thing we will do is build a function for calling the R binary.
library(subprocess)
R_binary <- function () {
R_exe <- ifelse (tolower(.Platform$OS.type) == "windows", "R.exe", "R")
return(file.path(R.home("bin"), R_exe))
}
Spawning a child R process is done as follows:
child_r <- subprocess::spawn_process(R_binary(), c("--vanilla"))
Sys.sleep(2) # allow sufficient time for the child R process to start
subprocess::process_read(child_r)$stdout
## [1] ""
## [2] "R version 3.5.0 (2018-04-23) -- \"Joy in Playing\""
## [3] "Copyright (C) 2018 The R Foundation for Statistical Computing"
## [4] "Platform: x86_64-apple-darwin17.5.0 (64-bit)"
## [5] ""
## [6] "R is free software and comes with ABSOLUTELY NO WARRANTY."
## [7] "You are welcome to redistribute it under certain conditions."
## [8] "Type 'license()' or 'licence()' for distribution details."
## [9] ""
## [10] " Natural language support but running in an English locale"
## [11] ""
## [12] "R is a collaborative project with many contributors."
## [13] "Type 'contributors()' for more information and"
## [14] "'citation()' on how to cite R or R packages in publications."
## [15] ""
## [16] "Type 'demo()' for some demos, 'help()' for on-line help, or"
## [17] "'help.start()' for an HTML browser interface to help."
## [18] "Type 'q()' to quit R."
## [19] ""
## [20] "> "
We will write some code as a character string and send it to the child process.
unlink("testdump.rda")
code <-
'
options(error = quote(dump.frames("testdump", TRUE)))
f <- function() {
g <- function() stop("test dump.frames")
g()
}
f() # will generate a dump on file "testdump.rda"
options(error = NULL)
'
invisible(subprocess::process_write(child_r, code))
subprocess::process_read(child_r)$stdout
## [1] ""
## [2] "> options(error = quote(dump.frames(\"testdump\", TRUE)))"
## [3] "> "
## [4] "> f <- function() {"
## [5] "+ g <- function() stop(\"test dump.frames\")"
## [6] "+ g()"
## [7] "+ }"
## [8] "> f() # will generate a dump on file \"testdump.rda\""
Now, you can load the dump into the child process and start the debugger.
code <-
'
load("testdump.rda")
debugger(testdump)
'
invisible(subprocess::process_write(child_r, code))
subprocess::process_read(child_r)$stdout
## [1] "> options(error = NULL)"
## [2] "> "
## [3] "> load(\"testdump.rda\")"
## [4] "> debugger(testdump)"
## [5] "Message: Error in g() : test dump.frames"
## [6] "Calls: f -> g"
## [7] "Available environments had calls:"
## [8] "1: f()"
## [9] "2: g()"
## [10] "3: stop(\"test dump.frames\")"
## [11] ""
## [12] "Enter an environment number, or 0 to exit Selection: "
Say you want to start walking through the debugger for f(1)
invisible(subprocess::process_write(child_r, "1\n"))
subprocess::process_read(child_r)$stdout
## [1] "1"
## [2] "Browsing in the environment with call:"
## [3] " f()"
## [4] "Called from: debugger.look(ind)"
## [5] "Browse[1]> "
You can continue to walk through the debugging as needed via the child process.
Don’t forget to terminate the process
subprocess::process_terminate(child_r)
## [1] TRUE
You can get copies of the actual files from my github page.
Upvotes: 1