having , rmd file below content:
```{r} data.frame(a=1) str(.last.value) ``` it renders data.frame rendering str(.last.value) produces ## null.
there knitr option or trick can use make works expected?
expected output second
## 'data.frame': 1 obs. of 1 variable: ## $ a: num 1
actually, there way make .last.value work.
```{r setup, include=false} knitr::opts_chunk$set(render = function(x, ...){ unlockbinding(".last.value", .basenamespaceenv) assign(".last.value", x, .basenamespaceenv) lockbinding(".last.value", .basenamespaceenv) knitr::knit_print(x, ...) }) ``` this piece of code defines custom render function binds output .last.value.
though, above fix not prefect. behaviour little bit different console. example, in r console
> x <- 2 > .last.value [1] 2 the .last.value returns last value if previous command nonvisible. on other hand, above hack knitr requires result printed.
```{r} x <- 2 x .last.value ``` update
to make .last.value works in r console, instead, consider following high-risk hack overwrites knitr internal function.
```{r setup, include=false} unlockbinding("knit_handlers", getnamespace("knitr")) evalq( assign( "knit_handlers", function(fun, options) { if (!is.function(fun)) fun = knit_print if (length(formals(fun)) < 2) stop("the chunk option 'render' must function of form ", "function(x, options) or function(x, ...)") merge_list(default_handlers, list(value = function(x, visible) { unlockbinding(".last.value", .basenamespaceenv) assign(".last.value", x, .basenamespaceenv) lockbinding(".last.value", .basenamespaceenv) if (visible) fun(x, options = options) })) } ), getnamespace("knitr") ) lockbinding("knit_handlers", getnamespace("knitr")) ``` this overwrites knitr internal function knit_handlers, subjects break @ anytime.
Comments
Post a Comment