String interpolation with R's glue on a vector, without calling it multiple times

Giora Simchoni

I'm looking to do string interpolation with R's glue::glue() on a vector, without calling it multiple times.

Example:

df <- data.frame(x = 1:10)

glue::glue("No. of Rows: {dim(df)[1]}, No. of Columns: {dim(df)[2]}")

Would give as required:

No. of Rows: 10, No. of Columns: 1

But I'm calling dim(df) twice, where it is a vector of length 2.

I was wondering if glue can handle this similar to string interpolation in Python with the % operator:

import pandas as pd

df = pd.DataFrame({"x": range(10)})
print('No. of Rows: %d, No. of Columns: %d' % df.shape)

Which gives the same required output without calling df.shape twice.

Gregor Thomas

Yes, you can do this:

glue("nr = {x[1]}, nc = {x[2]}", x = dim(mtcars))
# nr = 32, nc = 11

From the ?glue documentation, the description of ... is:

Unnamed arguments are taken to be expressions string(s) to format. Multiple inputs are concatenated together before formatting. Named arguments are taken to be temporary variables available for substitution.

(Emphasis mine, highlighting the part relevant to this question.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

cbind a vector multiple times in R

From Dev

Calling [promise's] resolve multiple times

From Dev

R loop over vector multiple times

From Dev

How to use the Scanner in a separate method without calling it multiple times

From Dev

NSNotificationCenter is calling multiple times

From Dev

.NET how to prevent calling a method returning a string multiple times

From Dev

Why is React calling some of my App's functions multiple times?

From Dev

Showcaseview library calling multiple times on Recyclerview's first item

From Dev

Swift: NavigationLink calling destination's init method multiple times

From Dev

R: calling function multiple times and outputting unique files

From Dev

Finding multiple substrings in a string without iterating over it multiple times

From Dev

better string interpolation in R

From Dev

Why is the interpolation expression executed multiple times?

From Dev

Bash string interpolation without subshell

From Dev

R: Finding multiple string matches in a vector of strings

From Dev

How to find a word in a string, which is used multiple times without a quote

From Dev

Underscore Debounce Calling Multiple Times

From Dev

Calling async function multiple times

From Dev

Calling an async method multiple times

From Dev

ViewPager calling multiple times -- Android

From Dev

ngOnChanges is calling up multiple times

From

Calling "fit" multiple times in Keras

From Dev

Alert dialog calling multiple times

From Dev

AddEventListener Event calling multiple times

From Dev

prevent calling ajax multiple times

From Dev

Birt Report Calling multiple times

From Dev

Calling onmouseover function multiple times

From Dev

Select words that are in a vector multiple times

From Dev

How to use a function call in a case statement without calling the function multiple times

Related Related

  1. 1

    cbind a vector multiple times in R

  2. 2

    Calling [promise's] resolve multiple times

  3. 3

    R loop over vector multiple times

  4. 4

    How to use the Scanner in a separate method without calling it multiple times

  5. 5

    NSNotificationCenter is calling multiple times

  6. 6

    .NET how to prevent calling a method returning a string multiple times

  7. 7

    Why is React calling some of my App's functions multiple times?

  8. 8

    Showcaseview library calling multiple times on Recyclerview's first item

  9. 9

    Swift: NavigationLink calling destination's init method multiple times

  10. 10

    R: calling function multiple times and outputting unique files

  11. 11

    Finding multiple substrings in a string without iterating over it multiple times

  12. 12

    better string interpolation in R

  13. 13

    Why is the interpolation expression executed multiple times?

  14. 14

    Bash string interpolation without subshell

  15. 15

    R: Finding multiple string matches in a vector of strings

  16. 16

    How to find a word in a string, which is used multiple times without a quote

  17. 17

    Underscore Debounce Calling Multiple Times

  18. 18

    Calling async function multiple times

  19. 19

    Calling an async method multiple times

  20. 20

    ViewPager calling multiple times -- Android

  21. 21

    ngOnChanges is calling up multiple times

  22. 22

    Calling "fit" multiple times in Keras

  23. 23

    Alert dialog calling multiple times

  24. 24

    AddEventListener Event calling multiple times

  25. 25

    prevent calling ajax multiple times

  26. 26

    Birt Report Calling multiple times

  27. 27

    Calling onmouseover function multiple times

  28. 28

    Select words that are in a vector multiple times

  29. 29

    How to use a function call in a case statement without calling the function multiple times

HotTag

Archive