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

slackr-not-working-from-within-function #18

Closed
hrbrmstr opened this issue Jul 29, 2015 · 4 comments
Closed

slackr-not-working-from-within-function #18

hrbrmstr opened this issue Jul 29, 2015 · 4 comments
Assignees

Comments

@hrbrmstr
Copy link
Collaborator

http://stackoverflow.com/questions/31703569/slackr-not-working-from-within-function?noredirect=1

I am using the slackr package to post messages to Slack. I have the following code that works when outside a function (i.e. posts two messages, twice, to Slack), but when used within a function, uses the variable values from outside the function, not as set within the function.

An example will help illustrate.

> n = 2
> 
> # Run a loop, send to slack.  Display content.
> aMsg <- NULL
> for ( i in 1:n ) {
+   msg <- NULL
+   msg <- paste0("Completed ", i, " iteration from loop @ ", Sys.time())
+   aMsg <- c(aMsg, msg)
+   slackr(paste(msg))
+ }
> slackr(aMsg[1], aMsg[2])
> 
> aMsg
[1] "Completed 1 iteration from loop @ 2015-07-29 10:12:45"
[2] "Completed 2 iteration from loop @ 2015-07-29 10:12:46"
> SendToSlack <- function(n) {
+   
+   slackr("Now post to Slack from function")
+   aMsg <- NULL
+   for ( i in 1:n ) {
+     msg <- NULL
+     msg <- paste0("Completed ", i, " iteration from function @ ", Sys.time())
+     aMsg <- c(aMsg, msg)
+     slackr(paste(msg))
+   }
+   slackr(aMsg[1], aMsg[2])
+   return (aMsg)
+   
+ }
> 
> output <- SendToSlack(n)
> output
[1] "Completed 1 iteration from function @ 2015-07-29 10:13:04"
[2] "Completed 2 iteration from function @ 2015-07-29 10:13:04"
> 

From the above snippet, I would expect two messages from the loop outside of the function, then two messages from the call to slackr() after the loop. The min:seconds in the output would be 12:45, 12:46 then 12:45, 12:46 again.

From the call to the function, I would again expect two messages from the loop within the function, then the same two messages from the call to slackr() after the loop. The min:seconds should be 13:04, 13:04 then 13:04, 13:04.

The attached image is the output I actually get in Slack. The loop works as expected, but the call to slackr() from the function for some reason uses the variable values as they were before the function was called (i.e. msg and aMsg are not changed by the function at all). However, slackr does post the string 'Now post to Slack from function' up to Slack from the function.

Anyone have any idea what is going on here? Something to do with global environments? Very odd!

Slack Output

@hrbrmstr hrbrmstr self-assigned this Jul 29, 2015
@hrbrmstr
Copy link
Collaborator Author

Newest build (1.3.0.9000) should fix this but needs extensive testing.

library(slackr)

slackr_setup(channel="#slackr")
n = 2

# Run a loop, send to slack.  Display content.
aMsg <- NULL
for ( i in 1:n ) {
  msg <- NULL
  msg <- paste0("Completed ", i, " iteration from loop @ ", Sys.time())
  aMsg <- c(aMsg, msg)
  slackr(paste(msg))
}
slackr(aMsg[1], aMsg[2])

aMsg
## [1] "Completed 1 iteration from loop @ 2015-07-29 14:52:21"
## [2] "Completed 2 iteration from loop @ 2015-07-29 14:52:22"
SendToSlack <- function(n) {

  slackr("Now post to Slack from function")
  aMsg <- NULL
  for ( i in 1:n ) {
    msg <- NULL
    msg <- paste0("Completed ", i, " iteration from function @ ", Sys.time())
    aMsg <- c(aMsg, msg)
    slackr(paste(msg))
  }
  slackr(aMsg[1], aMsg[2])
  return (aMsg)

}

output <- SendToSlack(n)
output
## [1] "Completed 1 iteration from function @ 2015-07-29 14:52:22"
## [2] "Completed 2 iteration from function @ 2015-07-29 14:52:22"

@billelev
Copy link

I put the original post up on Stack Overflow and agree that it has fixed the example I provided. I'll be using slackr() in functions quite a bit in the next few weeks and if I encounter and issues I'll update here. Thanks for the quick turnaround. Much appreciated!

@hrbrmstr
Copy link
Collaborator Author

cool. thx.

@billelev
Copy link

Here's an issue I just encountered with the new build...

slackrUpload(fName, initial_comment=sec, channels=Sys.getenv("SLACK_CHANNEL"))
Error in slackrChTrans(channels) : could not find function "bind_rows"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants