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

Equivalent of oldbookdown::screenshot() #38

Closed
hadley opened this issue Mar 9, 2016 · 8 comments
Closed

Equivalent of oldbookdown::screenshot() #38

hadley opened this issue Mar 9, 2016 · 8 comments
Milestone

Comments

@hadley
Copy link
Member

hadley commented Mar 9, 2016

I don't think we have anything that's quite equivalent yet. There are two important differences from knitr::include_graphics():

  • you can use it inside r for inline graphics
  • it preserves the original size of the image (using optional dpi argument to scale) (i.e. it ignores fig.width and fig.height)

This is most useful for diagrams (so the screenshot() name isn't great), where you have carefully sized the image.

@yihui
Copy link
Member

yihui commented Mar 9, 2016

knitr::include_graphics() works for r although I didn't think very carefully about it.

For the image size, include_graphics() basically uses out.width/out.height, and if they are not set, there should not be any restrictions on the size, i.e. it should show the original size, unless the size is has been restricted by CSS (like max-width: 100%) or LaTeX.

@hadley
Copy link
Member Author

hadley commented Apr 21, 2016

knitr::include_graphics() still doesn't work quite the way I want. Let me provide more details.

Take http://r4ds.had.co.nz/introduction.html#rstudio. That section contains two screens, one of the R console and one of the options pane. They are currently displayed as the same width - but "rstudio-layout.png" is 2466 pixels wide and "rstudio-workspace.png" is 1350 pixels wide. Rather than inspecting out.width at all, I'd rather have those images sized "naturally", i.e. 2466 / 220 = 11.2". and 1350 / 220 = 6.13" (the dpi is 220 because I made them on my retina macbook)

(You might wonder if 11" is too wide - and indeed I think it is. I probably need to redo the screenshot at a more reasonable size, but I didn't realise until now because include_graphics() resizes it for me.)

Another use case is http://r4ds.had.co.nz/relation-data.html#understanding-joins. Here I have hand drawn diagrams that have different widths, but with a consistent font size. Unless I manually adjust the out.width (which I've done here) the diagrams are displayed with the same width, distorting the font size across the diagrams. (If you look through the whole chapter you'll notice the font size in the diagrams isn't consistent because I didn't adjust the out.width everywhere)

Does that make sense? I'm fairly confident that this is different enough from the current behaviour of include_graphics() that it requires it's own function.

(I could've sworn that include_graphics() didn't work for me inline, but it does now 😄 )

@hadley hadley changed the title Equivalent of bookdown::screenshot() Equivalent of oldbookdown::screenshot() Apr 21, 2016
@yihui
Copy link
Member

yihui commented Apr 22, 2016

On closer inspection, I realized this was a bug of knitr::include_graphics(). It should not have added the width constraint on images. I just fixed it in knitr, and now it should just show images of their original sizes.

@hadley
Copy link
Member Author

hadley commented Apr 22, 2016

Ok, that's much better 👍 But setting the dpi chunk option doesn't seem to affect the output size?

@yihui
Copy link
Member

yihui commented Apr 22, 2016

Right. dpi currently only works for R plots.

@hadley
Copy link
Member Author

hadley commented Apr 25, 2016

@yihui could you make it work for this too? I think it is moderately important.

yihui added a commit to yihui/knitr that referenced this issue Apr 25, 2016
@yihui
Copy link
Member

yihui commented Apr 25, 2016

Done in the dev version of knitr.

@yihui yihui modified the milestone: v0.1 Jul 12, 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
@github-actions
Copy link

github-actions bot commented Nov 6, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants