Glue your strings together

data science R

Use the glue R package to join strings.

Matti Vuorre (University of Oxford)

We’ve all been there; writing manuscripts with R Markdown and dreaming of easy in-text code bits for reproducible reporting. Say you’ve fit a regression model to your data, and would then like to report the model’s parameters in your text, without writing the values in the text. (If the data or model changes, you’d need to re-type the values again.)

For example, you can print this model summary easily in the R console:

fit <- lm(mpg ~ disp, data = mtcars)

lm(formula = mpg ~ disp, data = mtcars)

    Min      1Q  Median      3Q     Max 
-4.8922 -2.2022 -0.9631  1.6272  7.2305 

             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
disp        -0.041215   0.004712  -8.747 9.38e-10 ***
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.251 on 30 degrees of freedom
Multiple R-squared:  0.7183,    Adjusted R-squared:  0.709 
F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

And to cite those values in the text body of your manuscript, you can write the text in R Markdown like this:

The model intercept was `r round(coef(fit)[1], 2)`, great.

Which would show up in your manuscript like this:

The model intercept was 29.6, great.


However, when you want to present more information, such as the parameter estimate with its standard error, you will have to paste() those strings together:

(x <- round(summary(fit)$coefficients, 3))
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   29.600      1.230  24.070        0
disp          -0.041      0.005  -8.747        0
intercept <- paste("b = ", x[1, 1], ", SE = ", x[1, 2], sep = "")

You can then just cite the intercept object in your text body:

The model intercept was very very significant (`r intercept`).

Which would render in your PDF or word document as:

The model intercept was very very significant (b = 29.6, SE = 1.23).

paste() is a base R function, and as such very robust and reproducible–all R installations will have it. However, as such it has a fairly terrible syntax where you have to quote strings, separate strings and variables with commas, etc. This task is made much easier with glue().


glue is a small R package that allows you to join strings together in a neat, pythonific way. It replaces the need for quoting and separating arguments in paste(), by asking you to wrap variables in curly braces. Here’s how to do the above pasting with glue:

intercept <- glue("b = {x[1, 1]}, SE = {x[1, 2]}")

Which gives you the same string as the much messier paste() approach: b = 29.6, SE = 1.23

Glue with data frames

Glue has other neat (more advanced) features, such as gluing variables row-by-row in a data frame:

library(dplyr) %>% 
    "{rownames(.)}'s point estimate was {Estimate}, with an SE of {`Std. Error`}."
(Intercept)'s point estimate was 29.6, with an SE of 1.23.
disp's point estimate was -0.041, with an SE of 0.005.

Appendix: papaja

For some models (like our simple linear model example here), the papaja R package (which deserves its own rpihkal post!) has very useful shortcuts

intercept <- apa_print(fit)$estimate$Intercept

If you now cite intercept in the text body of your manuscript, it renders into \(\LaTeX\) (which is interpreted nicely if you are outputting PDF or Word documents; here on this website it looks odd):

The model intercept was rather significant (`r intercept`).

The model intercept was rather significant (\(b = 29.60\), 95% CI \([27.09\), \(32.11]\)).

Read more about glue at

Support this work

Software used

The following software packages were used: R [Version 4.0.3; R Core Team (2020)] and the R-packages dplyr [Version 1.0.5; Wickham et al. (2021)], forcats [Version 0.5.1; Wickham (2021a)], ggplot2 [Version 3.3.3; Wickham (2016)], glue [Version 1.4.2; Hester (2020)], knitr [Version 1.31; Xie (2015)], papaja [Version; Aust and Barth (2020)], purrr [Version 0.3.4; Henry and Wickham (2020)], readr [Version 1.4.0; Wickham and Hester (2020)], stringr [Version 1.4.0; Wickham (2019)], tibble [Version 3.1.0; Müller and Wickham (2021)], tidyr [Version 1.1.3; Wickham (2021b)], and tidyverse [Version 1.3.0; Wickham et al. (2019)].

Aust, Frederik, and Marius Barth. 2020. papaja: Create APA Manuscripts with R Markdown.
Henry, Lionel, and Hadley Wickham. 2020. Purrr: Functional Programming Tools.
Hester, Jim. 2020. Glue: Interpreted String Literals.
Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames.
R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York.
———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations.
———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors).
———. 2021b. Tidyr: Tidy Messy Data.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation.
Wickham, Hadley, and Jim Hester. 2020. Readr: Read Rectangular Text Data.
Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC.



If you see mistakes or want to suggest changes, please create an issue on the source repository.


Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".


For attribution, please cite this work as

Vuorre (2018, Dec. 12). Sometimes I R: Glue your strings together. Retrieved from

BibTeX citation

  author = {Vuorre, Matti},
  title = {Sometimes I R: Glue your strings together},
  url = {},
  year = {2018}