Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: capture parsing errors #65

Closed
carloscinelli opened this issue Mar 7, 2016 · 0 comments
Closed

Feature: capture parsing errors #65

carloscinelli opened this issue Mar 7, 2016 · 0 comments

Comments

@carloscinelli
Copy link

Related question on SO: http://stackoverflow.com/questions/35842682/knitr-knitting-chunks-with-parsing-errors

Maybe it would be a good idea to capture parsing errors. For instance, the code below:

f <- function(x){
  z <- sum(x
           #
           y <- x + 1
           return(x + y + z)
}

Should give 3 errors:

> Sys.setenv(LANG = "en")
> f <- function(x){
+   z <- sum(x
+            #
+            y <- x + 1
Error: unexpected symbol in:
"           #
           y"
>            return(x + y + z)
Error: object 'y' not found
> }
Error: unexpected '}' in "}"

The current version of the evaluate() function stops at the first error:

text <- "f <- function(x){
  z <- sum(x
#
y <- x + 1
return(x + y + z)
}"
evaluate(text)
Error in parse(text = x, srcfile = src) : <text>:4:1: unexpected symbol
3: #
4: y
   ^

The main use case I am thinking here is for educational material (which is what I am working on now), but maybe there will be other use cases too.

@carloscinelli carloscinelli changed the title capturing parsing errors Feature: capture parsing errors Mar 7, 2016
@yihui yihui closed this as completed in cc0c584 Mar 8, 2016
jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Jul 28, 2016
Upstream changes:

CHANGES IN knitr VERSION 1.13

NEW FEATURES

    code chunks that generate metadata may be cached now; it requires htmlwidgets >= v0.6 and htmltools >= 0.3.3 if you cache code chunks that contain HTML widgets or Shiny inputs/outputs (#1158)

    when the output format is not HTML, HTML widgets used to fail to render; now knitr will try to generate static screenshots for HTML widgets automatically using the webshot package; you can specify alternative screenshots via the chunk option screenshot.alt (which takes a character vector of image paths), and pass more options to webshot::webshot() via the chunk option screenshot.opts, e.g. list(delay = 3, cliprect = 'viewport')

    added two functions include_url() and include_app() to embed URLs in the output (the latter is for Shiny app URLs); when the output format is HTML, iframe will be used; otherwise screenshots of the URLs will be used

    screenshotting for HTML widgets and URLs can be forced for all output formats via the chunk option screenshot.force = TRUE; if you set the chunk option screenshot.force = FALSE, knitr will just render these content normally and not take screenshots

    added a new chunk option fig.link, which can be used to attach hyperlinks on figures, e.g. you can add a link to a screenshot of a Shiny app so that readers can check out the actual live app after clicking on the static screenshot (this chunk option currently only works for Markdown and LaTeX output)

    syntactical errors in code chunks will be allowed when the chunk option error = TRUE and the package version of evaluate is at least 0.8.4; previously knitr would just stop on parsing errors (r-lib/evaluate#65)

    PNG/JPEG images included via include_graphics() also respects the chunk option dpi now; if it is numeric and the chunk option out.width is not set, the output width (in inches) of an image will be automatically calculated from the actual width (in pixels) divided by dpi; note this feature requires the packages png and/or jpeg to be installed, and you can disable the feature using dpi = NA (thanks, @hadley, rstudio/bookdown#38)

    added a new hook function named evaluate in knit_hooks so that users can redefine a evaluator to evaluate the code chunk; the default is evaluate::evaluate(), and your custom evaluator must be compatible with evaluate::evaluate() in terms of the argument names and the data structure of the returned value (a list of values with special classes)

    added a new function combine_words() to combine multiple words / phrases into a single string, which may be useful in inline R expressions, e.g. combine_words(c('a', 'b', 'c')) returns a, b, and c

    render_markdown() gained a new argument fence_char to customize the character to be used as the code blocks fence, e.g. it can be a backtick, or a tilde, depending on the Markdown rendering engine (thanks, @tinyheero, #1161)

    the pandoc() function no longer assumes Markdown input (thanks, @scls19fr, #1170)

    added a new function knit_meta_add() so that users can manually inject metadata into the current knitr session

    for the tikz engine, if fig.ext = 'svg', dvisvgm will be called to convert the DVI output of TikZ to SVG; you need to install dvisvgm, and Windows users have to install GhostScript as well (thanks, @dkilfoyle, #1177)

    new js and css engines which surround their content with <script> and <style> tags respecitvely, and print no output when not in an HTML document

    for LaTeX tables, kable() supports short captions now via the caption.short argument, e.g. kable(..., caption = 'A long caption', caption.short = 'A short caption') (thanks, @ismayc, #1199)

    added three global R options knitr.sanitize.errors, knitr.sanitize.warnings, and knitr.sanitize.messages to mask or change the messages, e.g. if options(knitr.sanitize.errors = TRUE) and the chunk option error = TRUE, the actual error message will be replaced by a character string like "An error occurred"; these options can also accept character values so you can customize the messages to be displayed, e.g. options(knitr.sanitize.warnings = 'You had a warning from the code'); see rstudio/shiny#1123 for the motivation of these options

BUG FIXES

    when the chunk option cache.rebuild = TRUE, the cache database should be rewritten (thanks, Oleg Mayba)

    include_graphics() did not work in inline R expressions (thanks, @WastlM, #1166)

    the cex parameter was not correctly restored in the case of opts_knit$set(global.par = TRUE) (http://stackoverflow.com/q/35606445/559676)

    for Rnw documents, when there are two instances of \documentclass{}, knitr might mistakenly treats the second instance as the the actual command to declare the document class (thanks, #1180, @ekstroem)

    corrected the environment for evaluating R scripts in stitch_rhtml() and stitch_rmd() (thanks, @Hughan, #1207)

MAJOR CHANGES

    the default value of the package option eval.after is changed from NULL to fig.cap, i.e. the figure caption will always be evaluated after a code chunk is evaluated (thanks, @JoshOBrien, #1165)

    the function eclipse_theme() has been removed since the website eclipsecolorthemes.org has been down for a long time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant