Print table in slack with slackr

I'm trying to send a table to slack via slackr (slackr::slack_msg), but using tibble::format.tbl_df gives me a bunch of formatting guff that slack doesn't handle so elegantly:

[38;5;246m# A tibble: 6 x 11[39m
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
[38;5;250m*[39m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m [3m[38;5;246m<dbl>[39m[23m
[38;5;250m1[39m  21.0    6.  160.  110.  3.90  2.62  16.5    0.    1.    4.    4.
[38;5;250m2[39m  21.0    6.  160.  110.  3.90  2.88  17.0    0.    1.    4.    4.
[38;5;250m3[39m  22.8    4.  108.   93.  3.85  2.32  18.6    1.    1.    4.    1.
[38;5;250m4[39m  21.4    6.  258.  110.  3.08  3.22  19.4    1.    0.    3.    1.
[38;5;250m5[39m  18.7    8.  360.  175.  3.15  3.44  17.0    0.    0.    3.    2.
[38;5;250m6[39m  18.1    6.  225.  105.  2.76  3.46  20.2    1.    0.    3.    1.

I thought an alternative solution would be to convert a dataframe into a string containing a formatted markdown table.

For instance:

df_to_markdown(head(mtcars))

#> | mpg  | cyl | disp | hp  | drat | wt    | qsec  | vs | am | gear | carb |
#> |------|-----|------|-----|------|-------|-------|----|----|------|------|
#> | 21   | 6   | 160  | 110 | 3.9  | 2.62  | 16.46 | 0  | 1  | 4    | 4    |
#> | 21   | 6   | 160  | 110 | 3.9  | 2.875 | 17.02 | 0  | 1  | 4    | 4    |
#> | 22.8 | 4   | 108  | 93  | 3.85 | 2.32  | 18.61 | 1  | 1  | 4    | 1    |
#> | 21.4 | 6   | 258  | 110 | 3.08 | 3.215 | 19.44 | 1  | 0  | 3    | 1    |
#> | 18.7 | 8   | 360  | 175 | 3.15 | 3.44  | 17.02 | 0  | 0  | 3    | 2    |
#> | 18.1 | 6   | 225  | 105 | 2.76 | 3.46  | 20.22 | 1  | 0  | 3    | 1    |

Is there an existing, easy way to acheive this?

If there's a better solution to this problem, let me know.

About the data.frame to markdown table, here are some examples:
with knitr :package:

subset_iris <- iris[1:5, ]
knitr::kable(subset_iris, format = "markdown")
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
cat(knitr::kable(subset_iris, format = "markdown"), sep = "\n")
#> | Sepal.Length| Sepal.Width| Petal.Length| Petal.Width|Species |
#> |------------:|-----------:|------------:|-----------:|:-------|
#> |          5.1|         3.5|          1.4|         0.2|setosa  |
#> |          4.9|         3.0|          1.4|         0.2|setosa  |
#> |          4.7|         3.2|          1.3|         0.2|setosa  |
#> |          4.6|         3.1|          1.5|         0.2|setosa  |
#> |          5.0|         3.6|          1.4|         0.2|setosa  |

You also have {pander} :package: which use pandoc markdown syntax but could also work

pander::pandoc.table(subset_iris)
#> 
#> -------------------------------------------------------------------
#>  Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
#> -------------- ------------- -------------- ------------- ---------
#>      5.1            3.5           1.4            0.2       setosa  
#> 
#>      4.9             3            1.4            0.2       setosa  
#> 
#>      4.7            3.2           1.3            0.2       setosa  
#> 
#>      4.6            3.1           1.5            0.2       setosa  
#> 
#>       5             3.6           1.4            0.2       setosa  
#> -------------------------------------------------------------------

that render in html as

pander::pandoc.table(subset_iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5 3.6 1.4 0.2 setosa

Created on 2018-08-13 by the reprex package (v0.2.0).

You should look at the options of these two functions.

3 Likes

Thank you very much, Christophe. This is exactly what I was looking for.

1 Like

I took the opportunity of your question to try slackr :package:. So here are some adding comments:

I think slack_msg is meant for text output. When I use the more generic slackr::slack function, it prints ok on my slack channel
slackr("iris info", head(iris) %>% as_tibble)

In slack
image

1 Like