Reputation: 660
I'm working on a project where lots of graphs are needed, and I have to put a source on every one of them. Is there a way to only write it once and refer to it later?
I have this:
plot(db)
grid.text("Fuente: Elaboración propia con datos del INEGI", 0.8, 0.03,
gp = gpar(fontfamily = "Arial", fontface = "italic", cex = 0.7))
plot(db_2)
grid.text("Fuente: Elaboración propia con datos del INEGI", 0.8, 0.03,
gp = gpar(fontfamily = "Arial", fontface = "italic", cex = 0.7))
And need something like this:
fuente <- grid.text("Fuente: Elaboración propia con datos del INEGI", 0.8, 0.03,
gp = gpar(fontfamily = "Arial", fontface = "italic", cex = 0.7))
plot(db)
fuente
plot(db_2)
fuente
Upvotes: 0
Views: 45
Reputation: 886938
An option is to also wrap with quote
and then use eval
fuente <- quote(grid.text("Fuente: Elaboración propia con datos del INEGI", 0.8, 0.03,
gp = gpar(fontfamily = "Arial", fontface = "italic", cex = 0.7)))
plot(db)
eval(fuente)
Upvotes: 0
Reputation: 226057
Do you mean
fuente <- function() {
grid.text("Fuente: Elaboración propia con datos del INEGI", 0.8, 0.03,
gp = gpar(fontfamily = "Arial", fontface = "italic", cex = 0.7))
}
? Then fuente()
will call your function/execute your code.
If you want to be really clever, you can
makeActiveBinding("fuente2", fuente, .GlobalEnv)
and then calling fuente2
(without parentheses) should work (but I wouldn't advise this: it's probably too clever/not a standard idiom)
A more standard way to do this would be to make a wrapper function for your plot call:
myplot <- function(x) {
plot(x)
grid.text(...)
}
myplot(db)
myplot(db2)
(this is not literal, fill in the ...
with the body of your grid.text()
call)
Upvotes: 2