Home Comments Thread
New Thread

One Little Thing: knitr::combine_words() - Combine objects into a phrase for human-readability | /en/2018/06/knitr-combine-words/

yihui yihui 2022-12-17 03:52:04

One Little Thing: knitr::combine_words() - Combine objects into a phrase for human-readability

https://yihui.org/en/2018/06/knitr-combine-words/

2 Comments

giscus-bot giscus-bot 2022-12-17 03:52:05
Guest *Daijiang Li* @ 2018-06-18 21:08:57 originally posted:

Good to know! Another question: when the output of a number is at the beginning of a sentence, is there a function in knitr or other packages that will replace the number as plain English? And more generally, when a number < 10 in the sentence, we normally want to convert it to plain English. E.g. 4 cats stand in the above picture to be Four ...; I saw 4 cats to be I saw four cats.

@daijiang

yihui yihui 2022-12-17 03:52:07

Good question. I'm not aware of such a function. Converting a number to the corresponding English word should be doable, but capitalizing it may be tricky, because an inline R expression is not aware of its position in the text. The worst case is that you use an argument to control the behavior (like number_to_word(..., cap = TRUE).

Originally posted on 2018-06-19 02:54:18

giscus-bot giscus-bot 2022-12-17 03:52:08
Guest *Daijiang Li* @ 2018-06-19 03:24:32 originally posted:

A quick search leads to here: https://tolstoy.newcastle.edu.au/R/help/05/04/2715.html Maybe add a helper function in knitr?

@daijiang

yihui yihui 2022-12-17 03:52:09

If the original author agrees, I can add it to my xfun package (if I were to write combine_words() now, I'd add it to xfun instead of knitr).

Originally posted on 2018-06-19 03:43:44

giscus-bot giscus-bot 2022-12-17 03:52:11
Guest *tjmahr* @ 2018-06-19 13:54:40 originally posted:

I've used qdap::replace_number() before to do this.

giscus-bot giscus-bot 2022-12-17 03:52:12
Guest *Daijiang Li* @ 2018-06-20 14:17:26 originally posted:

John Fox probably won't mind...

@daijiang

yihui yihui 2022-12-17 03:52:13

I guess so, but "probably" is not enough :) I need a formal permission. It is unclear what license the code in the email was under, so I cannot just copy it to a package.

Originally posted on 2018-06-20 15:17:59

giscus-bot giscus-bot 2022-12-17 03:52:14
Guest *Daijiang Li* @ 2018-06-21 21:33:36 originally posted:

I took a look at the source code of qdap::replace_number(). It is actually the code from the above link ... they did add the reference though (https://cran.r-project.org/doc/Rnews/Rnews_2005-1.pdf page 51-55). But still not sure about the license. What is the license for code presented in R news?

@daijiang

yihui yihui 2022-12-17 03:52:14

I have no idea, but if the code was copied from John Fox's article, at least John should be listed as a contributor of the package, and author of the function on the help page.

Originally posted on 2018-06-22 02:52:40

giscus-bot giscus-bot 2022-12-17 03:52:06
Guest *Christophe Dervieux* @ 2018-06-19 07:03:01 originally posted:

About the someone else that could have implemented this elsewhere, 'glue' package as a 'glue_collapse' function with a 'and=' argument that does change the last separator in a the paste. See
https://glue.tidyverse.org/reference/glue_collapse.html

yihui yihui 2022-12-17 03:52:10

So glue::glue_collapse() does not seem to support the Oxford comma, or extra characters before/after each word. On the other hand, knitr::combine_words() does not support truncation.

BTW, knitr::knit_expand() overlaps with the glue package, and is a few years older than it :)

Originally posted on 2018-06-19 07:09:17

Sign in to join the discussion

Sign in with GitHub