Home Comments Thread
New Thread

An Example of Shortening R Code | /en/2023/10/shorten-code/

giscus giscus 2023-10-10 03:02:43

An Example of Shortening R Code

While I was answering a question from a user last week, I saw a piece of code that, in my opinion, could be shortened. What it does is to set two chunk options, message and warning, to NA or FALSE …

https://yihui.org/en/2023/10/shorten-code/

👍 2

1 Comment

eliocamp eliocamp 2023-10-10 12:50:00

If I recall correctly, installed.packages() currently navigates the whole folder structure in your libraries, so it's relatively slow; and painfully slow for large package libraries, so that's another plus of not testing if evaluate is installed. (I think the "correct" way of testing if a package is installed is requireNamespace("evaluate", quietly = TRUE); at least that's what I've seen in other packages and what I use in mine).

👍 1
yihui yihui 2023-10-10 13:16:12

You are right that installed.packages() can be slow.

If we only test for the existence of a package but don't care if it can actually be loaded, I think system.file(package = 'evaluate') != '' is a faster way to go.

Sign in to join the discussion

Sign in with GitHub