Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 15, 2021 12:30
Show Gist options
  • Select an option

  • Save simonthompson99/f39c96173e87fcb1b1c5f5e6603ed56b to your computer and use it in GitHub Desktop.

Select an option

Save simonthompson99/f39c96173e87fcb1b1c5f5e6603ed56b to your computer and use it in GitHub Desktop.
[Send Dataframe to Slack] Send a dataframe to be rendered as table in Slack #r #slack
#-- use the CDT Bot API token see https://cnfl.extge.co.uk/display/CDT/Generic+Login+Credentials+Information#GenericLoginCredentialsInformation-SlackCDTBotUser
#-- function to send df (d) to a specific channel with optional message, will break up if there are too many rows
send_df_to_slack_as_msg <- function(d, channel, api_token, msg = NA){
require(knitr)
require(slackr)
slackr_setup(channel = channel,
api_token = api_token)
d <- kable(d, format = 'rst')
if(!is.na(msg)){
slackr_msg(msg)
}
slackr(d, channel = channel)
}
#-- function to send df to slack as a file upload will give a preview and option to expand lines if filename is *.txt
send_df_to_slack_as_file <- function(d, channel, api_token, filename = NA, comment = NA){
require(knitr)
require(slackr)
slackr_setup(channel = channel,
api_token = api_token)
fn <- tempfile()
writeLines(kable(d, format = 'rst'), fn)
slackr_upload(fn, title = filename, initial_comment = comment, channels = channel, api_token = api_token)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment