Created
April 24, 2020 21:32
-
-
Save karawoo/93437f51b32c621a7011fac703f218ca to your computer and use it in GitHub Desktop.
reprex demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### | |
#### Reprex demo #### | |
####################### | |
## 2020-04-24 | |
# A simple reprex -------------------------------------------------------------- | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
mean(dat$a) | |
## Copy the above and run reprex::reprex() -- markdown is now on my clipboard | |
# Can also provide code as an expression --------------------------------------- | |
reprex({ | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
mean(dat$a) | |
}) | |
# Errors, warnings are included in output -------------------------------------- | |
reprex({ | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
dat[, b] | |
}) | |
# Everything you need should be included in the reprex ------------------------- | |
library("ggplot2") | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
reprex({ | |
p <- ggplot(dat, aes(x = a, y = b)) + geom_point() | |
}) | |
# This will fail because ggplot2 is not loaded in the R session reprex creates | |
# Put library("ggplot2") and dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
# within the reprex | |
# Plots Just Work(TM) ---------------------------------------------------------- | |
reprex({ | |
library("ggplot2") | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
p <- ggplot(dat, aes(x = a, y = b)) + geom_point() | |
p | |
}) | |
# "But I need external files!!!" ----------------------------------------------- | |
## Do you really? | |
my_file <- "a,b\n1,'a'\n2,'b'" | |
dat <- read.table(header = TRUE, text = my_file, sep = ",") | |
mean(dat$a) | |
## If you really, really need files on disk, use temp files | |
## https://github.com/r-lib/roxygen2md/issues/4 | |
# Output options --------------------------------------------------------------- | |
# Default is GitHub-flavored markdown | |
# HTML | |
reprex({ | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
mean(dat$a) | |
}, venue = "html") | |
# R | |
reprex({ | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
mean(dat$a) | |
}, venue = "r") | |
# RTF - nice for presentations, copy/paste into Keynote | |
reprex({ | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
mean(dat$a) | |
}, venue = "rtf") | |
# Session info ----------------------------------------------------------------- | |
reprex({ | |
dat <- data.frame(a = 1:2, b = LETTERS[1:2]) | |
mean(dat$a) | |
}, session_info = TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment