0

If I restart R my data.table is already filled from previous sessions. How to make sure to empty a data.table before (re)filling it again?

I searched both google and data.table/vignettes and but could not get any simple answer

[edit] As clarification, I do use vim. I do NOT mind if old data is loaded via .RData I just need to empty one data.table

Bastiaan Wakkie
  • 303
  • 3
  • 9
  • sounds like rstudio is loading data from the `.RData` file. Try clicking the broom icon to *clear objects from workspace* – David Jan 19 '21 at 20:46

2 Answers2

2

Absolutely do not attempt to empty any data structures manually (otherwise Jenny Bryan will set your computer on fire!).

Instead, ensure that R isn’t loading any old data when it gets restarted. Delete any .RData file in the current directory (this file is invisible on most systems!), and when quitting R answer “no” when asked whether you want to save the workspace.

If you’re using RStudio, you can configure it to never save/restore data across sessions.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks Konrad, The thing is, you do not answer my question. I obviously have my reasons to ask this. So I DO empty only one data.table the rest of the data can stay. So removing .RData or remove the data when closing is not what I was looking for. – Bastiaan Wakkie Jan 20 '21 at 15:56
  • @BastiaanWakkie I urge you to really read the linked blog post. — My advice is precisely because preserving data across sessions *is a bad idea*. If you want to save *specific* data, you’re advised to do so explicitly rather than relying on R’s ill-advised save/restore feature. That is, use the functions [`saveRDS`/`readRDS`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/readRDS.html) to save and restore data explicitly. – Konrad Rudolph Jan 20 '21 at 22:28
0

Assuming you do not have RStudio and therefore an easy UI you can remove all objects, including hidden objects, from the global environment with:

rm(list = ls(all.names = TRUE))

If you want to just clear a specific data table you can simply reassign a blank data table to the object. For example if dt is your full data table you can do:

dt <- data.table()

Which will return a blank data table that can then be (re)filled.

dodo1672
  • 51
  • 4
  • Thank you @dodo1672, that worked! I am using vim as my r IDE. I do have rstudio but I *need* my real vim and not the fake one ;-) – Bastiaan Wakkie Jan 20 '21 at 13:35