Skip to content

Commit

Permalink
Merge pull request #190 from mrkaye97/slackr-33
Browse files Browse the repository at this point in the history
Slackr 3.3.0
  • Loading branch information
mrkaye97 authored Feb 20, 2023
2 parents 62fc48e + 452d560 commit a4246ed
Show file tree
Hide file tree
Showing 30 changed files with 487 additions and 344 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ docs
.config
.slackr
test_config
.Rprofile
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: slackr
Title: Send Messages, Images, R Objects and Files to 'Slack'
Channels/Users
Version: 3.2.2
Version: 3.3.0
Author: Bob Rudis [aut, cre], Jay Jacobs [ctb], David Severski [ctb],
Quinn Weber [ctb], Konrad Karczewski [ctb], Shinya Uryu [ctb], Gregory
Jefferis [ctb], Ed Niles [ctb], Rick Saporta [ctb], Jonathan Sidi
Expand All @@ -27,7 +27,6 @@ Depends:
Imports:
cachem (>= 1.0.4),
dplyr,
ggplot2,
graphics,
grDevices,
httr (>= 1.4.2),
Expand All @@ -40,6 +39,7 @@ Imports:
withr
Suggests:
covr,
ggplot2,
knitr,
rmarkdown,
svglite,
Expand Down
8 changes: 2 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ importFrom(dplyr,distinct)
importFrom(dplyr,left_join)
importFrom(dplyr,rename)
importFrom(dplyr,setdiff)
importFrom(ggplot2,aes)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggsave)
importFrom(ggplot2,last_plot)
importFrom(grDevices,dev.copy)
importFrom(grDevices,dev.off)
importFrom(grDevices,png)
importFrom(graphics,par)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
Expand All @@ -56,10 +50,12 @@ importFrom(magrittr,"%>%")
importFrom(memoise,memoise)
importFrom(rlang,abort)
importFrom(rlang,call2)
importFrom(rlang,check_installed)
importFrom(rlang,inform)
importFrom(rlang,warn)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tools,file_ext)
importFrom(utils,URLencode)
importFrom(utils,write.csv)
importFrom(withr,local_options)
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# slackr 3.3.0
User-facing changes:

* `ggslackr` now relies on dots (`...`) to pass arguments through to `ggsave`
* `ggplot2` is now in `suggests`, so the user doesn't need it installed to use `slackr`. `ggslackr` will prompt to install if it's not already.
* Fixes a "bug" with pagination in `slackr_history` causing an almost infinite loop
* Fixes a bug in `slackr_save` where `initial_comment` would do nothing
* Improves documentation of `slackr_history` to be more helpful in pointing the user to the Slack API docs

Backend changes:

* Gets rid of default args in some un-exported functions to make tracking down bugs easier
* Adds significant test coverage

# slackr 3.2.2

* Fixes a bug where specifying specifying `duration` had no effect in `slackr_history()` if posted_from_time was not specified also. [linked issue](https://github.com/mrkaye97/slackr/issues/181)
Expand Down
2 changes: 1 addition & 1 deletion R/call_slack_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ POST <- "POST"
#' @noRd
#'
stop_for_status <- function(r) {
# note that httr::stop_for_status should be called explicitly

# note that httr::stop_for_status should be called explicitly
httr::stop_for_status(r)
cr <- content(r, encoding = "UTF-8")

Expand Down
30 changes: 15 additions & 15 deletions R/call_slack_internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @keywords internal
#' @noRd
#' @references https://api.slack.com/methods/conversations.list
list_channels <- function(token = Sys.getenv("SLACK_TOKEN"), types = "public_channel", exclude_archived = TRUE, ...) {
list_channels <- function(token, types, exclude_archived, ...) {
with_pagination(
function(cursor) {
call_slack_api(
Expand All @@ -33,7 +33,7 @@ list_channels <- function(token = Sys.getenv("SLACK_TOKEN"), types = "public_cha
#' @keywords internal
#' @noRd
#' @references https://api.slack.com/methods/users.list
list_users <- function(token = Sys.getenv("SLACK_TOKEN"), ...) {
list_users <- function(token, ...) {
with_pagination(
function(cursor) {
call_slack_api(
Expand Down Expand Up @@ -64,20 +64,19 @@ list_users <- function(token = Sys.getenv("SLACK_TOKEN"), ...) {
post_message <- function(
txt,
channel,
emoji = "",
username = Sys.getenv("SLACK_USERNAME"),
token = Sys.getenv("SLACK_TOKEN"),
emoji,
username,
token,
...
) {
r <-
call_slack_api(
r <- call_slack_api(
"/api/chat.postMessage",
.method = POST,
token = token,
body = list(
text = txt,
channel = channel,
username = username,
text = txt,
channel = channel,
username = username,
link_names = 1,
icon_emoji = emoji,
...
Expand Down Expand Up @@ -109,26 +108,27 @@ post_message <- function(
files_upload <- function(
file,
channels,
initial_comment = NULL,
token = Sys.getenv("SLACK_TOKEN"),
initial_comment,
token,
...
) {
r <- call_slack_api(
"/api/files.upload",
.method = POST,
token = token,
body = list(
file = upload_file(file),
file = upload_file(file),
initial_comment = initial_comment,
channels = paste(channels, collapse = ","),
channels = paste(channels, collapse = ","),
...
)
)

invisible(content(r))
}


list_scopes <- function(token = Sys.getenv("SLACK_TOKEN")) {
list_scopes <- function(token) {
r <- call_slack_api(
"/api/apps.permissions.scopes.list",
.method = GET,
Expand Down
45 changes: 14 additions & 31 deletions R/gg_slackr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,49 @@
#' Unlike the [slackr_dev()] function, this one takes a `ggplot` object,
#' eliminating the need to have a graphics device (think use in scripts).
#'
#' @importFrom rlang check_installed
#' @importFrom tools file_ext
#'
#' @param plot ggplot object to save, defaults to last plot displayed.
#' @param channels Comma-separated list of channel names or IDs where the file will be shared.
#' @param scale scaling factor.
#' @param width width (defaults to the width of current plotting window).
#' @param height height (defaults to the height of current plotting window).
#' @param units units for width and height when either one is explicitly specified
#' (in, cm, or mm).
#' @param dpi dpi to use for raster graphics.
#' @param limitsize when TRUE (the default), ggsave will not save images larger
#' than 50x50 inches, to prevent the common error of specifying dimensions in pixels.
#' @param token Authentication token bearing required scopes.
#' @param file Prefix for filenames (defaults to `ggplot`).
#' @param initial_comment The message text introducing the file in specified channels.
#' @param thread_ts Provide another message's ts value to upload this file as a reply. Never use a reply's ts value; use its parent instead.
#' @param title Title of file.
#' @param device the file extension to use. Options: "png", "eps", "ps", "pdf", "jpeg", "tiff", "bmp", "svg". Default: "png".
#' @param ... other arguments passed to graphics device.
#' @importFrom ggplot2 ggsave last_plot ggplot aes geom_point
#' @importFrom graphics par
#' @param \dots other arguments passed to \link[ggplot2]{ggsave}
#'
#' @return `httr` response object (invisibly)
#'
#' @examples
#' \dontrun{
#' slackr_setup()
#' ggslackr(qplot(mpg, wt, data = mtcars))
#' }
#' @export
ggslackr <- function(
plot = last_plot(),
plot = ggplot2::last_plot(),
channels = Sys.getenv("SLACK_CHANNEL"),
scale = 1,
width = par("din")[1],
height = par("din")[2],
units = NULL,
dpi = 300,
limitsize = TRUE,
token = Sys.getenv("SLACK_TOKEN"),
file = "ggplot",
file = "ggplot.png",
initial_comment = NULL,
thread_ts = NULL,
title = NULL,
device = c("png", "eps", "ps", "pdf", "jpeg", "tiff", "bmp", "svg"),
...
) {
ext <- paste0(".", match.arg(device))

check_installed("ggplot2")

ext <- paste0(".", file_ext(file))
ftmp <- tempfile(file, fileext = ext)

ggsave(
ggplot2::ggsave(
filename = ftmp,
plot = plot,
scale = scale,
width = width,
height = height,
units = units,
dpi = dpi,
limitsize = limitsize,
... = ...
)

res <-
files_upload(
res <- files_upload(
file = ftmp,
channels = channels,
token = token,
Expand Down
2 changes: 1 addition & 1 deletion R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param ... Additiional arguments passed to the function called.
#' @return No return value. Called for side effects
warn_for_args <- function(token, ...) {
if (missing(token) | is.na(token) | is.null(token)) {
if (missing(token) || is.na(token) || is.null(token)) {
abort("You must supply a token.")
}

Expand Down
44 changes: 21 additions & 23 deletions R/slackr.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,16 @@ slackr <- function(
# combined all of them (rval is a character vector)
output <- paste0(rval, collapse = "\n")

resp <-
post_message(
token = token,
channel = channel,
username = username,
emoji = icon_emoji,
txt = sprintf("```%s```", output),
link_names = 1,
thread_ts = thread_ts,
reply_broadcast = reply_broadcast
)
resp <- post_message(
token = token,
channel = channel,
username = username,
emoji = icon_emoji,
txt = sprintf("```%s```", output),
link_names = 1,
thread_ts = thread_ts,
reply_broadcast = reply_broadcast
)
}

invisible(resp)
Expand Down Expand Up @@ -168,18 +167,17 @@ slackr_msg <- function(

output <- paste0(txt, collapse = "\n\n")

z <-
post_message(
txt = output,
emoji = icon_emoji,
channel = channel,
token = token,
username = username,
link_names = 1,
thread_ts = thread_ts,
reply_broadcast = reply_broadcast,
...
)
z <- post_message(
txt = output,
emoji = icon_emoji,
channel = channel,
token = token,
username = username,
link_names = 1,
thread_ts = thread_ts,
reply_broadcast = reply_broadcast,
...
)

invisible(z)
}
Loading

0 comments on commit a4246ed

Please sign in to comment.