Skip to content

Instantly share code, notes, and snippets.

@barryrowlingson
Created May 19, 2014 16:32
Show Gist options
  • Save barryrowlingson/40b19d70e86185af9ce9 to your computer and use it in GitHub Desktop.
Save barryrowlingson/40b19d70e86185af9ce9 to your computer and use it in GitHub Desktop.
Hiding mechanics from R plotting for dexy
# You want to show users the code to make a plot.
# BUT you dont want to show them the png() function call you used to make the plot
# for your documentation.
#
# dexy doesn't. You have to open/close the graphics device in your code:
### @export "graph"
png("plot.png")
plot(c(1,2,3), pch=19, col="purple")
dev.off()
# never mind, if you want to hide it you can put the png and close in sections that you don't show in your source:
### @export "hide1"
png("plot.png")
### @export "graph"
plot(c(1,2,3), pch=19, col="purple")
### @export "hide2"
dev.off()
# (you need the first section hidden if there's a listed section before it).
#
# okay, bit messy. But what if you want ggplot2 graphics? And who doesn't? Well, again you don't want to have
# to show the explicit `print` in your script.
#
# So you get this:
#
### @export "hide1"
png("plot.png")
print(
### @export "graph"
ggplot(d,aes(x=x,y=y)) + geom_point()
### @export "hide2"
)
dev.off()
# errr yuck much? As an added bonus the prompt in the graph section changes to a + because its a continuation
# line from the print function.
#
# knitr creates graphics files with no "mechanical" code in the source - including for ggplot2-type graphics.
# it also knows how to reference the graphics files in the output via its output hooks.
#
# dexy filters which did this would be rather useful... I imagine a method on the `d['name']` object that would
# reference any created images... or other blocks... Then you'd do:
#
# <img src="{{ d['code001.R'].image(1) }}">
#
# to show the first generated image. Syntax applicable to any other code chunks that generate image files...
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment