Skip to content

Commit 62fc48e

Browse files
authored
Merge pull request #189 from mrkaye97/restyle-package
Restyle the package
2 parents bab195f + 03ef79d commit 62fc48e

19 files changed

+293
-258
lines changed

R/call_slack_api.R

+13-10
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ with_retry <- function(fun) {
8080
#' @return The API response (a named list)
8181
#' @export
8282
#'
83-
call_slack_api <- function(path, ..., body = NULL, .method = c("GET", "POST"),
84-
token,
85-
.verbose = Sys.getenv("SLACKR_VERBOSE", "FALSE"),
86-
.next_cursor = "") {
83+
call_slack_api <- function(
84+
path,
85+
...,
86+
body = NULL,
87+
.method = c("GET", "POST"),
88+
token,
89+
.verbose = Sys.getenv("SLACKR_VERBOSE", "FALSE"),
90+
.next_cursor = ""
91+
) {
92+
8793
if (missing(token) || is.null(token)) {
8894
token <- Sys.getenv("SLACK_TOKEN", "")
8995
}
@@ -98,9 +104,7 @@ call_slack_api <- function(path, ..., body = NULL, .method = c("GET", "POST"),
98104
if (.verbose == "TRUE") {
99105
old_config <- set_config(verbose())
100106
on.exit(set_config(old_config), add = TRUE)
101-
} # else {
102-
# set_config(httr::verbose(data_out = FALSE, data_in = FALSE, info = FALSE, ssl = FALSE))
103-
# }
107+
}
104108

105109
# Set up the API call
106110
call_api <- function() {
@@ -112,7 +116,6 @@ call_slack_api <- function(path, ..., body = NULL, .method = c("GET", "POST"),
112116
.headers = c(Authorization = paste("Bearer", token))
113117
),
114118
query = add_cursor_get(..., .next_cursor = .next_cursor)
115-
# ...
116119
)
117120
} else if (.method == "POST") {
118121
POST(
@@ -135,7 +138,6 @@ call_slack_api <- function(path, ..., body = NULL, .method = c("GET", "POST"),
135138
add_cursor_get <- function(..., .next_cursor = "") {
136139
z <- list(...)
137140
if (!is.null(.next_cursor) && .next_cursor != "") {
138-
# inform("Appending cursor to query")
139141
z <- append(z, list(cursor = .next_cursor))
140142
}
141143
z
@@ -207,7 +209,8 @@ with_pagination <- function(fun, extract) {
207209
result <- convert_response_to_tibble(r, extract)
208210
} else {
209211
result <- bind_rows(
210-
result, convert_response_to_tibble(r, extract)
212+
result,
213+
convert_response_to_tibble(r, extract)
211214
)
212215
}
213216
}

R/call_slack_internals.R

+15-11
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ list_users <- function(token = Sys.getenv("SLACK_TOKEN"), ...) {
6161
#' @param token A Slack API token.
6262
#'
6363
#' @references https://api.slack.com/methods/chat.postMessage
64-
post_message <- function(txt,
65-
channel,
66-
emoji = "",
67-
username = Sys.getenv("SLACK_USERNAME"),
68-
token = Sys.getenv("SLACK_TOKEN"),
69-
...) {
64+
post_message <- function(
65+
txt,
66+
channel,
67+
emoji = "",
68+
username = Sys.getenv("SLACK_USERNAME"),
69+
token = Sys.getenv("SLACK_TOKEN"),
70+
...
71+
) {
7072
r <-
7173
call_slack_api(
7274
"/api/chat.postMessage",
@@ -104,11 +106,13 @@ post_message <- function(txt,
104106
#'
105107
#'
106108
#' @references https://api.slack.com/methods/files.upload
107-
files_upload <- function(file,
108-
channels,
109-
initial_comment = NULL,
110-
token = Sys.getenv("SLACK_TOKEN"),
111-
...) {
109+
files_upload <- function(
110+
file,
111+
channels,
112+
initial_comment = NULL,
113+
token = Sys.getenv("SLACK_TOKEN"),
114+
...
115+
) {
112116
r <- call_slack_api(
113117
"/api/files.upload",
114118
.method = POST,

R/gg_slackr.R

+17-16
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@
2929
#' ggslackr(qplot(mpg, wt, data = mtcars))
3030
#' }
3131
#' @export
32-
ggslackr <- function(plot = last_plot(),
33-
channels = Sys.getenv("SLACK_CHANNEL"),
34-
scale = 1,
35-
width = par("din")[1],
36-
height = par("din")[2],
37-
units = NULL,
38-
dpi = 300,
39-
limitsize = TRUE,
40-
token = Sys.getenv("SLACK_TOKEN"),
41-
file = "ggplot",
42-
initial_comment = NULL,
43-
thread_ts = NULL,
44-
title = NULL,
45-
device = c("png", "eps", "ps", "pdf", "jpeg", "tiff", "bmp", "svg"),
46-
...) {
47-
32+
ggslackr <- function(
33+
plot = last_plot(),
34+
channels = Sys.getenv("SLACK_CHANNEL"),
35+
scale = 1,
36+
width = par("din")[1],
37+
height = par("din")[2],
38+
units = NULL,
39+
dpi = 300,
40+
limitsize = TRUE,
41+
token = Sys.getenv("SLACK_TOKEN"),
42+
file = "ggplot",
43+
initial_comment = NULL,
44+
thread_ts = NULL,
45+
title = NULL,
46+
device = c("png", "eps", "ps", "pdf", "jpeg", "tiff", "bmp", "svg"),
47+
...
48+
) {
4849
ext <- paste0(".", match.arg(device))
4950
ftmp <- tempfile(file, fileext = ext)
5051

R/register_onexit.R

+16-11
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@
2727
#' summary(lm.D9)
2828
#'
2929
#' # pass a message to Slack channel 'general' with a header message to begin output
30-
#' register_onexit(lm, "bazinga!",
30+
#' register_onexit(
31+
#' lm,
32+
#' "bazinga!",
3133
#' channel = "#general",
3234
#' header_msg = "This is a message to begin"
3335
#' )
3436
#'
3537
#' lm.D9 <- slack_lm(weight ~ group)
3638
#'
3739
#' # onexit with an expression that calls lm.plot
38-
#' register_onexit(lm,
40+
#' register_onexit(
41+
#' lm,
3942
#' {
4043
#' par(mfrow = c(2, 2), oma = c(0, 0, 2, 0))
4144
#' plot(z)
@@ -55,15 +58,17 @@
5558
#' [slackr_msg()]
5659
#' @author Jonathan Sidi (aut)
5760
#' @export
58-
register_onexit <- function(f,
59-
...,
60-
header_msg = NULL,
61-
use_device = FALSE,
62-
env = parent.frame(2),
63-
channel = Sys.getenv("SLACK_CHANNEL"),
64-
username = Sys.getenv("SLACK_USERNAME"),
65-
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
66-
token = Sys.getenv("SLACK_TOKEN")) {
61+
register_onexit <- function(
62+
f,
63+
...,
64+
header_msg = NULL,
65+
use_device = FALSE,
66+
env = parent.frame(2),
67+
channel = Sys.getenv("SLACK_CHANNEL"),
68+
username = Sys.getenv("SLACK_USERNAME"),
69+
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
70+
token = Sys.getenv("SLACK_TOKEN")
71+
) {
6772
warn_for_args(
6873
token,
6974
username = username,

R/slackr.R

+43-42
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
#' slackr("iris info", head(iris), str(iris))
2828
#' }
2929
#' @export
30-
slackr <- function(...,
31-
channel = Sys.getenv("SLACK_CHANNEL"),
32-
username = Sys.getenv("SLACK_USERNAME"),
33-
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
34-
token = Sys.getenv("SLACK_TOKEN"),
35-
thread_ts = NULL,
36-
reply_broadcast = FALSE) {
37-
30+
slackr <- function(
31+
...,
32+
channel = Sys.getenv("SLACK_CHANNEL"),
33+
username = Sys.getenv("SLACK_USERNAME"),
34+
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
35+
token = Sys.getenv("SLACK_TOKEN"),
36+
thread_ts = NULL,
37+
reply_broadcast = FALSE
38+
) {
3839
local_options(list(cli.num_colors = 1))
3940

4041
warn_for_args(
@@ -44,7 +45,6 @@ slackr <- function(...,
4445
)
4546

4647
if (!missing(...)) {
47-
4848
# get the arglist
4949
args <- substitute(list(...))[-1L]
5050

@@ -69,31 +69,31 @@ slackr <- function(...,
6969
expr <- args[[i]]
7070

7171
# do something, note all the newlines...Slack ``` needs them
72-
tmp <- switch(mode(expr),
73-
# if it's actually an expresison, iterate over it
74-
expression = {
75-
cat(sprintf("> %s\n", deparse(expr)))
76-
lapply(expr, evalVis)
77-
},
78-
# if it's a call or a name, eval, printing run output as if in console
79-
call = ,
80-
name = {
81-
cat(sprintf("> %s\n", deparse(expr)))
82-
list(evalVis(expr))
83-
},
84-
# if pretty much anything else (i.e. a bare value) just output it
85-
integer = ,
86-
double = ,
87-
complex = ,
88-
raw = ,
89-
logical = ,
90-
numeric = cat(sprintf("%s\n\n", as.character(expr))),
91-
character = cat(sprintf("%s\n\n", expr)),
92-
abort("mode of argument not handled at present by slackr")
72+
tmp <- switch(
73+
mode(expr),
74+
# if it's actually an expresison, iterate over it
75+
expression = {
76+
cat(sprintf("> %s\n", deparse(expr)))
77+
lapply(expr, evalVis)
78+
},
79+
# if it's a call or a name, eval, printing run output as if in console
80+
call = ,
81+
name = {
82+
cat(sprintf("> %s\n", deparse(expr)))
83+
list(evalVis(expr))
84+
},
85+
# if pretty much anything else (i.e. a bare value) just output it
86+
integer = ,
87+
double = ,
88+
complex = ,
89+
raw = ,
90+
logical = ,
91+
numeric = cat(sprintf("%s\n\n", as.character(expr))),
92+
character = cat(sprintf("%s\n\n", expr)),
93+
abort("mode of argument not handled at present by slackr")
9394
)
9495

9596
for (item in tmp) {
96-
9797
if (item$visible) {
9898
print(item$value)
9999
cat("\n")
@@ -120,7 +120,6 @@ slackr <- function(...,
120120
thread_ts = thread_ts,
121121
reply_broadcast = reply_broadcast
122122
)
123-
124123
}
125124

126125
invisible(resp)
@@ -151,14 +150,16 @@ slackr <- function(...,
151150
#' slackr_msg("Hi")
152151
#' }
153152
#' @export
154-
slackr_msg <- function(txt = "",
155-
channel = Sys.getenv("SLACK_CHANNEL"),
156-
username = Sys.getenv("SLACK_USERNAME"),
157-
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
158-
token = Sys.getenv("SLACK_TOKEN"),
159-
thread_ts = NULL,
160-
reply_broadcast = FALSE,
161-
...) {
153+
slackr_msg <- function(
154+
txt = "",
155+
channel = Sys.getenv("SLACK_CHANNEL"),
156+
username = Sys.getenv("SLACK_USERNAME"),
157+
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
158+
token = Sys.getenv("SLACK_TOKEN"),
159+
thread_ts = NULL,
160+
reply_broadcast = FALSE,
161+
...
162+
) {
162163
warn_for_args(
163164
token,
164165
username = username,
@@ -169,9 +170,9 @@ slackr_msg <- function(txt = "",
169170

170171
z <-
171172
post_message(
172-
txt = output,
173+
txt = output,
173174
emoji = icon_emoji,
174-
channel = channel,
175+
channel = channel,
175176
token = token,
176177
username = username,
177178
link_names = 1,

R/slackr_bot.r

+24-24
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
#' slackr_bot("iris info", head(iris), str(iris))
3535
#'
3636
#' # or directly
37-
#' slackr_bot("Test message",
37+
#' slackr_bot(
38+
#' "Test message",
3839
#' incoming_webhook_url = "https://hooks.slack.com/services/XXXXX/XXXXX/XXXXX"
3940
#' )
4041
#' }
@@ -47,7 +48,6 @@ slackr_bot <- function(..., incoming_webhook_url = Sys.getenv("SLACK_INCOMING_WE
4748
}
4849

4950
if (!missing(...)) {
50-
5151
# get the arglist
5252
args <- substitute(list(...))[-1L]
5353

@@ -72,27 +72,28 @@ slackr_bot <- function(..., incoming_webhook_url = Sys.getenv("SLACK_INCOMING_WE
7272
expr <- args[[i]]
7373

7474
# do something, note all the newlines...Slack ``` needs them
75-
tmp <- switch(mode(expr),
76-
# if it's actually an expression, iterate over it
77-
expression = {
78-
cat(sprintf("> %s\n", deparse(expr)))
79-
lapply(expr, evalVis)
80-
},
81-
# if it's a call or a name, eval, printing run output as if in console
82-
call = ,
83-
name = {
84-
cat(sprintf("> %s\n", deparse(expr)))
85-
list(evalVis(expr))
86-
},
87-
# if pretty much anything else (i.e. a bare value) just output it
88-
integer = ,
89-
double = ,
90-
complex = ,
91-
raw = ,
92-
logical = ,
93-
numeric = cat(sprintf("%s\n\n", as.character(expr))),
94-
character = cat(sprintf("%s\n\n", expr)),
95-
abort("mode of argument not handled at present by slackr")
75+
tmp <- switch(
76+
mode(expr),
77+
# if it's actually an expression, iterate over it
78+
expression = {
79+
cat(sprintf("> %s\n", deparse(expr)))
80+
lapply(expr, evalVis)
81+
},
82+
# if it's a call or a name, eval, printing run output as if in console
83+
call = ,
84+
name = {
85+
cat(sprintf("> %s\n", deparse(expr)))
86+
list(evalVis(expr))
87+
},
88+
# if pretty much anything else (i.e. a bare value) just output it
89+
integer = ,
90+
double = ,
91+
complex = ,
92+
raw = ,
93+
logical = ,
94+
numeric = cat(sprintf("%s\n\n", as.character(expr))),
95+
character = cat(sprintf("%s\n\n", expr)),
96+
abort("mode of argument not handled at present by slackr")
9697
)
9798

9899
for (item in tmp) {
@@ -126,7 +127,6 @@ slackr_bot <- function(..., incoming_webhook_url = Sys.getenv("SLACK_INCOMING_WE
126127
)
127128

128129
stop_for_status(resp)
129-
130130
}
131131

132132
return(invisible(resp))

0 commit comments

Comments
 (0)