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

first version of reprex and addins #28

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
httr,
magick,
R6,
reprex,
RSelenium,
rtweet,
utils,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(carbon)
export(reprex_to_carbon_browser)
export(reprex_to_carbon_download)
export(tinyurl)
importFrom(R6,R6Class)
importFrom(RSelenium,makeFirefoxProfile)
Expand Down
60 changes: 60 additions & 0 deletions R/addins.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' Creates a Reprex of selected code (or from input) renders it and opens or downloads the Carbon
#'
#' @param ... arguments passed to [reprex::reprex]
#' @param html_preview show also reprex html result? Default is false
#' @param style should the code be styled? Default is True
#' @param url_only if only the URL should be returned without opening the browser
#'
#' @return the URL to the Carbon (invisible) or the filename (invisible)
#' @export
#'
#' @examples
#' \dontrun{
#' reprex_to_carbon_browser(input = c(" x=1: 10", " x"))
#' # copy the following code using STRG + C or similar:
#' a <- 1:10
#' b <- rnorm(10)
#' cbind(a, b)
#' plot(a, b)
#'
#' # Then run this
#' reprex_to_carbon_browser()
#' }
reprex_to_carbon_browser <- function(..., html_preview = FALSE, style = TRUE,
url_only = FALSE) {
res <- reprex::reprex(..., venue = "r", html_preview = html_preview,
style = style)
res <- paste(res[!grepl("^#' !\\[", res)], collapse = "\n")

carb <- carbon$new(trimws(res))
if (url_only) return(carb$uri)

carb$browse()
return(invisible(carb$uri))
}

#' @describeIn reprex_to_carbon_browser downloads the picture
#' @export
reprex_to_carbon_download <- function(..., file = "carbon_reprex.png",
html_preview = FALSE, style = TRUE,
url_only = FALSE) {
res <- reprex::reprex(..., venue = "r", html_preview = html_preview,
style = style)
res <- paste(res[!grepl("^#' !\\[", res)], collapse = "\n")

carb <- carbon$new(trimws(res))
if (url_only) return(carb$uri)

# check if the file exists, if so, increase version of the file
while (file.exists(file)) {
if (!grepl("-\\d+\\.(png|jpg)$", file)) {
file <- gsub("\\.(png|jpg)$", "-01.\\1", file)
} else {
nr <- as.integer(gsub("^.*-(\\d+)\\.(png|jpg)$", "\\1", file)) + 1
file <- gsub("^(.*)-\\d+\\.(png|jpg)$", sprintf("\\1-%02i.\\2", nr), file)
}
}

carb$carbonate(file = file)
return(invisible(file))
}
9 changes: 9 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Name: Browse carbonated Reprex
Description: Opens the browser for the current reprex in Carbon
Binding: reprex_to_carbon_browser
Interactive: true

Name: Download carbonated Reprex
Description: Downloads the current reprex in Carbon
Binding: reprex_to_carbon_download
Interactive: true
46 changes: 46 additions & 0 deletions man/reprex_to_carbon_browser.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.