We at STATWORX work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects. At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package. Up till the 24th December I will present one function each day from
helfRlein. So, on the 9th day of Christmas my true love gave to me…
What can it do?
This little helper shows a table with the size of each object in the given environment.
If you are in a situation where you have coded a lot and your environment is now quite messy,
object_size_in_env helps you to find the big fish with respect to memory usage. Personally, I ran into this problem a few times when I looped over multiple executions of my models. At some point, the sessions became quite large in memory and I did not know why! With the help of
object_size_in_env and some degubbing I could locate the object that caused this problem and adjusted my code accordingly.
How to use it?
First, let us create an environment with some variables.
# building an environment this_env <- new.env() assign("Var1", 3, envir = this_env) assign("Var2", 1:1000, envir = this_env) assign("Var3", rep("test", 1000), envir = this_env)
To get the size information of our objects, internally
format(object.size()) is used. With the
unit the output format can be changed (eg.
# checking the size object_size_in_env(env = this_env, unit = "B") OBJECT SIZE UNIT 1: Var3 8104 B 2: Var2 4048 B 3: Var1 56 B
Have a merry advent season!