3

Being an R user, I'm now trying to learn the SPSS syntax.

I sed to add the command rm(list=ls()) at the being of R script to ensure that R is empty before I go on my work.

Is there a similar command for SPSS? Thanks.

lokheart
  • 23,743
  • 39
  • 98
  • 169
  • 5
    This is a really bad habit in R, you should learn (and encourage others to learn) how to manage the R workspace, and whether a saved one is restored on start up. It drives me crazy seeing this nasty bit of code copied all over the place as an "automatic" step. – mdsumner Jun 20 '11 at 03:53
  • 1
    It's bad because R isn't "empty", as you call it. This will not detach any of your packages. It will also not reset any options. Some environments may still be around. I'm also not sure that it clears memory, as you state. There is a different command for that, i.e. `gc()`. – Andrie Jun 20 '11 at 07:56
  • 2
    the bigger question is, why man, why?!?!? If you know R - why would ever want to go and learn how to use SPSS? – Chase Jun 20 '11 at 12:45
  • 1
    the memory will get cleared by R, perhaps more quickly by subsequent use of gc(), but still - the main point is that this is applying a "fix" to a sympton, where the underlying problem is one of starting R and it auto-loading a saved workspace. The most likely reason is that there is a .Rdata file in the working directory. This is something you should be in control of directly, in my opinion, in the same way as you should want your workflow to be carefully controlled, which code is run, which variables and functions defined, which packages are loaded, and so on. – mdsumner Jun 20 '11 at 16:07

2 Answers2

5

Close to the functional equivalent in SPSS would be

dataset close all.

This simply closes all open dataframes except for the active dataframe (and strips it of its name). If you open another dataset the previous dataframe will close automatically.

Andy W
  • 5,031
  • 3
  • 25
  • 51
5

Since the way SPSS uses memory is fundamentally different from how R uses it, there really isn't a close equivalent between rm and SPSS memory management mechanisms. SPSS does not keep datasets in memory in most cases - which is why it can process files of unlimited size. When you close an SPSS dataset, all its associated metadata - which is in memory, is removed. DATASET CLOSE ALL closes all open datasets, but there can still be an unnamed dataset remaining. To really remove everything, you would write dataset close all. new file.

because a dataset cannot remain open if another one is opened unless it has a dataset name.

You might also be interested to know that you can run R code from within SPSS via BEGIN PROGRAM R. END PROGRAM.

SPSS provides apis for reading the active SPSS data, creating SPSS pivot tables, creating new SPSS datasets etc. You can even use the SPSS Custom Dialog Builder to create a dialog box interface for your R program. In addition, there is a mechanism for building SPSS extension commands that are actually implemented in R or Python. All this apparatus is free once you have the basic SPSS Statistics. So it is easy to use SPSS to provide a nice user interface and nice output for an R program.

You can download the R Essentials and a good number of R extensions for SPSS from the SPSS Community website at www.ibm.com/developerworks/spssdevcentral. All free, but registration is required.

p.s. rm(ls()) is useful in some situations - it is often used with R code within SPSS, because the state of the R workspace is retained between R programs within the same SPSS session.

Regards, Jon Peck

JKP
  • 5,419
  • 13
  • 5