Browse Source

beginnings of send tab to twitter as image & gist link

master
boB Rudis 7 years ago
parent
commit
42cd906ee8
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1
      .Rbuildignore
  2. 1
      .gitignore
  3. 7
      DESCRIPTION
  4. 6
      NAMESPACE
  5. 43
      R/bare_rename.r
  6. 4
      R/hrbraddins-package.R
  7. 39
      R/tweet-share.r
  8. 17
      README.Rmd
  9. 10
      inst/rstudio/addins.dcf
  10. 16
      man/bare_rename.Rd
  11. 11
      man/tweet_share.Rd
  12. 1
      tests/testthat/test-hrbraddins.R

1
.Rbuildignore

@ -9,3 +9,4 @@
^README_files$
^doc$
^codecov\.yml$
^\.httr-oauth$

1
.gitignore

@ -6,3 +6,4 @@
src/*.o
src/*.so
src/*.dll
.httr-oauth

7
DESCRIPTION

@ -15,5 +15,10 @@ Depends:
R (>= 3.2.0)
Imports:
rstudioapi (>= 0.4),
utils
utils,
magick,
pdftools,
rmarkdown,
gistr,
knitr
RoxygenNote: 6.0.1

6
NAMESPACE

@ -1,7 +1,11 @@
# Generated by roxygen2: do not edit by hand
export(bare_combine)
export(bare_rename)
export(join_rows)
export(tweet_share)
import(knitr)
import(magick)
import(pdftools)
import(rmarkdown)
import(rstudioapi)
importFrom(utils,read.csv)

43
R/bare_rename.r

@ -1,43 +0,0 @@
#' Turn a selection of comma-separated bare strings into a dplyr::rename statement
#'
#' Turns \cr\cr
#' \code{a,b c,d,e f}\cr\cr
#' or\cr\cr
#' \code{a, b c, d, e f}\cr\cr
#' into\cr\cr
#' \code{dplyr::rename(a, `b c`, d, `e f`)}
#'
#' @export
bare_rename <- function() {
ctx <- rstudioapi::getActiveDocumentContext()
if (!is.null(ctx)) {
if (ctx$selection[[1]]$text != "") {
bits <- utils::read.csv(text = ctx$selection[[1]]$text,
stringsAsFactors = FALSE,
header = FALSE)
bits <- unlist(bits, use.names = FALSE)
op <- options("useFancyQuotes")
options(useFancyQuotes = FALSE)
bits <- sapply(bits, trimws)
bits <- ifelse(grepl(" ", bits), dQuote(bits), bits)
bits <- gsub('^"|"$', '`', bits)
options(op)
bits <- paste0(bits, collapse = ", ")
bits <- sprintf("dplyr::rename(%s)", bits)
rstudioapi::modifyRange(ctx$selection[[1]]$range, bits)
}
}
}

4
R/hrbraddins-package.R

@ -3,6 +3,8 @@
#' @name hrbraddins
#' @docType package
#' @author Bob Rudis (bob@@rud.is)
#' @import rstudioapi
#' @import rstudioapi magick pdftools rmarkdown knitr
#' @importFrom gistr gist_create
#' @importFrom twitteR tweet
#' @importFrom utils read.csv
NULL

39
R/tweet-share.r

@ -0,0 +1,39 @@
#' Share the active RStudio source tab on Twitter
#'
#' TODO tempdir/tempfile; error checking
#'
#' @export
tweet_share <- function() {
ctx <- rstudioapi::getSourceEditorContext()
writeLines(ctx$contents, "/tmp/a.r")
g <- gistr::gist_create("/tmp/a.r", browse=FALSE)
fil <- file("/tmp/a.rmd", open = "w")
writeLines(c(
"---",
"output: pdf_document",
"---",
"\\pagenumbering{gobble}",
"```{r eval=FALSE}",
ctx$contents,
"```",
sprintf("gist: [%s](%s)", g$url, g$url)),
fil)
close(fil)
rmarkdown::render("/tmp/a.rmd", rmarkdown::pdf_document(), "/tmp/a.pdf")
code_raster <- pdftools::pdf_render_page("/tmp/a.pdf", dpi=144)
img <- magick::image_read(code_raster)
img <- magick::image_trim(img)
magick::image_write(img, "/tmp/a.png")
#magick::image_browse(img)
twitteR::tweet(sprintf("R code: %s", g$url), mediaPath = "/tmp/a.png")
}

17
README.Rmd

@ -10,25 +10,8 @@ output: rmarkdown::github_document
The following functions are implemented:
- `bare_combine`: Turn a selection of comma-separated bare strings into a - `base::combine`: statement
- `bare_rename`: Turn a selection of comma-separated bare strings into a `dplyr::rename` statement
- `join_rows`: Join `cr`/`lf`-separated selected rows of text into a single space-separated row
### Bare Rename
The RStudio Addins menu selection "Bare Rename" will take a text selection and make it into `dplyr::rename()` statement For example:
a,b c,d,e f
or
a, b c, d, e f
will be converted to:
dplyr::rename(a, `b c`, d, `e f`)
**NOTE**: not all backtick-edge cases are handled (yet).
### Bare Combine
Similarly, the RStudio Addins menu selection "Bare Combine" will take a text selection and make it into a `c()` statement. For example:

10
inst/rstudio/addins.dcf

@ -1,8 +1,3 @@
Name: Bare Rename
Description: Turns a selected comma-separated list of strings into a dplyr::rename() statement
Binding: bare_rename
Interactive: false
Name: Bare Combine
Description: Turns a selected comma-separated list of strings into a base::c() statement
Binding: bare_combine
@ -12,3 +7,8 @@ Name: Join Rows
Description: Join a selected set of rows into one line
Binding: join_rows
Interactive: false
Name: Tweet Share
Description: Share the active RStudio source tab on Twitter
Binding: tweet_share
Interactive: false

16
man/bare_rename.Rd

@ -1,16 +0,0 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bare_rename.r
\name{bare_rename}
\alias{bare_rename}
\title{Turn a selection of comma-separated bare strings into a dplyr::rename statement}
\usage{
bare_rename()
}
\description{
Turns \cr\cr
\code{a,b c,d,e f}\cr\cr
or\cr\cr
\code{a, b c, d, e f}\cr\cr
into\cr\cr
\code{dplyr::rename(a, `b c`, d, `e f`)}
}

11
man/tweet_share.Rd

@ -0,0 +1,11 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tweet-share.r
\name{tweet_share}
\alias{tweet_share}
\title{Share the active RStudio source tab on Twitter}
\usage{
tweet_share()
}
\description{
Share the active RStudio source tab on Twitter
}

1
tests/testthat/test-hrbraddins.R

@ -2,7 +2,6 @@ context("basic functionality")
test_that("we can do something", {
expect_error(bare_combine())
expect_error(bare_rename())
expect_error(join_rows())
})

Loading…
Cancel
Save