diff --git a/DESCRIPTION b/DESCRIPTION index 7d3ffdf..66e6837 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: httr, magick, R6, + reprex, RSelenium, rtweet, utils, diff --git a/NAMESPACE b/NAMESPACE index 382e979..429e322 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/addins.R b/R/addins.R new file mode 100644 index 0000000..c80b086 --- /dev/null +++ b/R/addins.R @@ -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)) +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf new file mode 100644 index 0000000..5906960 --- /dev/null +++ b/inst/rstudio/addins.dcf @@ -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 diff --git a/man/reprex_to_carbon_browser.Rd b/man/reprex_to_carbon_browser.Rd new file mode 100644 index 0000000..3236782 --- /dev/null +++ b/man/reprex_to_carbon_browser.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addins.R +\name{reprex_to_carbon_browser} +\alias{reprex_to_carbon_browser} +\alias{reprex_to_carbon_download} +\title{Creates a Reprex of selected code (or from input) renders it and opens or downloads the Carbon} +\usage{ +reprex_to_carbon_browser(..., html_preview = FALSE, style = TRUE, + url_only = FALSE) + +reprex_to_carbon_download(..., file = "carbon_reprex.png", + html_preview = FALSE, style = TRUE, url_only = FALSE) +} +\arguments{ +\item{...}{arguments passed to \link[reprex:reprex]{reprex::reprex}} + +\item{html_preview}{show also reprex html result? Default is false} + +\item{style}{should the code be styled? Default is True} + +\item{url_only}{if only the URL should be returned without opening the browser} +} +\value{ +the URL to the Carbon (invisible) or the filename (invisible) +} +\description{ +Creates a Reprex of selected code (or from input) renders it and opens or downloads the Carbon +} +\section{Functions}{ +\itemize{ +\item \code{reprex_to_carbon_download}: downloads the picture +}} + +\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() +} +}