Last month I saw an interesting question on Stack Overflow, in which the OP wanted to print a series of data frames as tables, and tried double loops, which did not work:
for (i in c("CP", "BK", "IT", "WP")) {
for (j in c("DD", "SI")) {
data <- get(paste0(i, "_", j, "_comb1"))
print(data)
}
}
There are two reasons why this won’t work:
print()doesn’t create tables. You must create a table either explicitly (e.g., viaknitr::kable()) or inexplicitly (e.g., via thedf_printoption in R Markdown).- Tables must be printed in top-level expressions. They won’t be generated inside loops by default.
I posted an answer there to create top-level expressions to print the data
frames, base on the chunk option code. However, I think there must be a deeper
problem, and I might have provided a right answer to a wrong question. That is,
if you have to get() a series of objects by their names, and the names share
a pattern (e.g., *_*_comb1 in the original question), perhaps these objects
shouldn’t have existed in the first place!
If they have existed, that means the author must have created them in a way like this:
CP_DD_comb1 <- ...
BK_DD_comb1 <- ...
IT_DD_comb1 <- ...
WP_DD_comb1 <- ...
CP_SI_comb1 <- ...
BK_SI_comb1 <- ...
IT_SI_comb1 <- ...
WP_SI_comb1 <- ...
The code in ... must also have a pattern, like
subset(data, X1 == "CP" & X2 == "DD"), which is likely to violate the DRY
principle (Don’t Repeat Yourself).
When you find yourself create a series of objects with a pattern in their names,
I’d say you are probably doing it wrong. Instead of creating these global
objects, you really should create one object only—a list that contains these
objects. For example, if you want to partition the mtcars data by the cyl
and am variables, you shouldn’t create objects like cyl_8_am_0. Instead, you
create a list:
mtcars_list <- split(mtcars, ~ cyl + am)
Then if you want to create a table for each combination of cyl and am, you
simply write one line of code:
knitr::kable(mtcars_list)
That’s much more elegant and simpler than first creating several global objects
(which can pollute the global environment), then retrieving them via get()
(which can bite you if you are not careful enough about its envir, mode, and
inherits arguments), and finally printing them one by one.
I’d argue that you should rarely need to use get() in your daily code, just
like you should rarely need eval(). If you have to
resort to get(), there is likely to be a deeper problem and you may need to
pause and rethink.
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).