Skip to content

Commit

Permalink
inline expressions do not permit errors any more; closes #651
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Dec 12, 2013
1 parent 883a101 commit 8c6bb38
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

- the default value for the chunk option `tidy` is `FALSE` now, which means the R source code in chunks will no longer be reformatted by `formatR::tidy.source()` by default; this feature must be explicitly turned on by `tidy=TRUE`, and it has brought a lot of confusion in the past, so it is perhaps a good idea not to reformat the source code by default

- inline R expressions will no longer be evaluated in `try()`, which means errors in inline R code will be emitted immediately

- the first argument of the `plot` hook is the filename of the plot now; in previous versions, it was a vector of length 2 (basename and file extension); see `?hook_plot`

- in the previous version, we can set `options(knitr.foo = value)` so that **knitr** can adjust the package options `opts_knit$set(foo = value)` before knitting a document; now the prefix for package options has been changed to `knitr.package.`, i.e. we should set `options(knitr.package.foo)` to achieve `opts_knit$set(foo)`; besides, it is also possible to change the default chunk options using `options(knitr.chunk.foo)` now, but you are warned that this may bring reproducibility issues, so please use with care
Expand Down
14 changes: 4 additions & 10 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ call_inline = function(block) {

inline_exec = function(
block, eval = eval_lang(opts_chunk$get('eval')), envir = knit_global(),
error = eval_lang(opts_chunk$get('error')), hook = knit_hooks$get('inline')

This comment has been minimized.

Copy link
@krlmlr

krlmlr Dec 12, 2013

Contributor

Any chance we keep this and use a different chunk option that defaults to FALSE?

This comment has been minimized.

Copy link
@yihui

yihui Dec 12, 2013

Author Owner

Sorry, but no. I have made the decision not to use try() on my side. I really do not understand how difficult it is to fix errors in the inline code, when you have to fix them sooner or later.

hook = knit_hooks$get('inline')
) {

# run inline code and substitute original texts
Expand All @@ -305,19 +305,13 @@ inline_exec = function(
loc = block$location
for (i in 1:n) {
res = if (eval) {
(if (error) try else identity)(
{
v = withVisible(eval(parse_only(code[i]), envir = envir))
if (v$visible) v$value
}
)
v = withVisible(eval(parse_only(code[i]), envir = envir))
if (v$visible) v$value
} else '??'
d = nchar(input)
# replace with evaluated results
str_sub(input, loc[i, 1], loc[i, 2]) = if (length(res)) {
if (inherits(res, 'try-error')) {
knit_hooks$get('error')(str_c('\n', res, '\n'), opts_chunk$get())
} else hook(res)
paste(hook(res), collapse = '')
} else ''
if (i < n) loc[(i + 1):n, ] = loc[(i + 1):n, ] - (d - nchar(input))
# may need to move back and forth because replacement may be longer or shorter
Expand Down
2 changes: 1 addition & 1 deletion R/template.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ knit_expand = function(file, ..., text = readLines(file, warn = FALSE),
env = list(...)
env = if (length(env)) list2env(env, parent = parent.frame()) else parent.frame()
inline_exec(list(code = mat, input = txt, location = loc),
eval = TRUE, envir = env, error = FALSE, hook = identity)
eval = TRUE, envir = env, hook = identity)
}
3 changes: 1 addition & 2 deletions inst/examples/knitr-spin.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ mean(x)
#' is the mean of x plus 2.
#' The code itself may contain braces, but these are not checked. Thus,
#' perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`
#' can produce different results compared to simply running the script:
{{2 + 3}} - {{4 - 5}}
#' can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.
#'
#' Now we continue writing the report. We can draw plots as well.

Expand Down
3 changes: 1 addition & 2 deletions inst/examples/knitr-spin.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ You can use the special syntax {{code}} to embed inline expressions, e.g.
is the mean of x plus 2.
The code itself may contain braces, but these are not checked. Thus,
perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`
can produce different results compared to simply running the script:
`r 2 + 3}} - {{4 - 5`
can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.

Now we continue writing the report. We can draw plots as well.

Expand Down

0 comments on commit 8c6bb38

Please sign in to comment.