# NOT RUN {
# put some code like this on the clipboard
# (y <- 1:4)
# mean(y)
reprex()
# provide code as an expression
reprex(rbinom(3, size = 10, prob = 0.5))
reprex({y <- 1:4; mean(y)})
# note that you can include newlines in those brackets
# in fact, that is probably a good idea
reprex({
x <- 1:4
y <- 2:5
x + y
})
## provide code via character vector
reprex(input = c("x <- 1:4", "y <- 2:5", "x + y"))
## if just one line, terminate with '\n'
reprex(input = "rnorm(3)\n")
## customize the output comment prefix
reprex(rbinom(3, size = 10, prob = 0.5), comment = "#;-)")
# override a default chunk option, in general
reprex({(y <- 1:4); median(y)}, opts_chunk = list(collapse = FALSE))
# the above is simply shorthand for this and produces same result
reprex({
#+ setup, include = FALSE
knitr::opts_chunk$set(collapse = FALSE)
#+ actual-reprex-code
(y <- 1:4)
median(y)
})
# add prose, use general markdown formatting
reprex({
#' # A Big Heading
#'
#' Look at my cute example. I love the
#' [reprex](https://github.com/jennybc/reprex#readme) package!
y <- 1:4
mean(y)
})
# read reprex from file
writeLines(c("x <- 1:4", "mean(x)"), "foofy.R")
reprex(input = "foofy.R")
# read from file and write to similarly-named outfiles
reprex(input = "foofy.R", outfile = NA)
list.files(pattern = "foofy")
file.remove(list.files(pattern = "foofy"))
# write rendered reprex to file
reprex({
x <- 1:4
y <- 2:5
x + y
}, outfile = "foofy")
list.files(pattern = "foofy")
file.remove(list.files(pattern = "foofy"))
# write reprex to file AND keep figure local too, i.e. don't post to imgur
reprex({
#' Some prose
## regular comment
(x <- 1:4)
median(x)
plot(x)
}, outfile = "blarg", opts_knit = list(upload.fun = identity))
list.files(pattern = "blarg")
unlink(list.files(pattern = "blarg"), recursive = TRUE)
## target venue = StackOverflow
## http://stackoverflow.com/editing-help
ret <- reprex({
x <- 1:4
y <- 2:5
x + y
}, venue = "so")
ret
## target venue = R, also good for email or Slack snippets
ret <- reprex({
x <- 1:4
y <- 2:5
x + y
}, venue = "R")
ret
## include prompt and don't comment the output
## use this when you want to make your code hard to execute :)
reprex({
x <- 1:4
y <- 2:5
x + y
}, opts_chunk = list(comment = NA, prompt = TRUE))
## leading prompts are stripped from source
reprex(input = c("> x <- 1:3", "> median(x)"))
# }
# NOT RUN {
# }
Run the code above in your browser using DataCamp Workspace