esqLABS / ESQlabs-r-guidelines

https://esqlabs.github.io/ESQlabs-r-guidelines/
0 stars 0 forks source link

Move info from bluspice to this #18

Open Felixmil opened 5 months ago

Felixmil commented 5 months ago

From: https://wiki.esqlabs.com/wiki/Software/R_tips_and_tricks

RStudio

Useful shortcuts

Profiling

Profiling of code can be done within R-Studio with the package profvis, a descirption of the process is given here . In short, pass the code to be profiled as argument to the function profvis:

profvis({
  data(diamonds, package = "ggplot2")

  plot(price ~ carat, data = diamonds)
  m <- lm(price ~ carat, data = diamonds)
  abline(m, col = "red")
})

Markdown

Performance

If you want to see if a vector contains a single value, any(x == 10) is much faster than 10 %in% x because testing equality is simpler than testing set inclusion. unlist(x, use.names = FALSE) is much faster than unlist(x). A pernicious source of slow R code is growing an object with a loop. Whenever you use c(), append(), cbind(), rbind(), or paste() to create a bigger object, R must first allocate space for the new object and then copy the old object to its new home. If you repeat this many times, like in a for loop, this can be quite expensive.

External sources

General R

List of useful external information about R and RStudio.

ggplot2

dlefaudeux commented 5 months ago

And from esqlabsR github wiki (more or less the same as in bluespice): https://github.com/esqLABS/esqlabsR/wiki/R-useful-information

Felixmil commented 5 months ago

related to #9