This is more of an R question - but I ask here as it has to do with meta data retrieved from SPSS.
So I have a set of FILE HANDLES, and I want to extract the "save" FILE HANDLE and map the current R directory to that location. I'm confused how to extract the locations out of the list that R returns when I use the spssdata.GetFileHandles() function. Example syntax below: ****************************************************. FILE HANDLE data /NAME = "H:\DATA FILES\KINGSTON\2014". FILE HANDLE save /NAME = "data\AndyChartsGraphs". BEGIN PROGRAM R. handles <- spssdata.GetFileHandles() handles #handles[[2]][1] #this is how I thought you would extract the save handle #it only returns - [1] "save" - though END PROGRAM. ****************************************************. Below is the subsequent output: ---------------------------------------------------------------------------- [[1]] [1] "data" "H:\\DATA FILES\\KINGSTON\\2014" "windows-1252" [[2]] [1] "save" "H:\\DATA FILES\\KINGSTON\\2014\\AndyChartsGraphs" [3] "windows-1252" ---------------------------------------------------------------------------- Reading the R output is difficult, but you can see what I thought would be the code to extract the "H:\\DATA FILES\\KINGSTON\\2014\\AndyChartsGraphs" portion, but I was incorrect. Can any R guru's here tell me how to extract the relevant string so I can set my R working directory accordingly? Also the FILE HANDLE I will always want will be named "save" - but I may have more than 2 FILE HANDLES, so I presume just grabbing the string based on the index number of the list is not optimal, as its location may change if I add in more FILE HANDLES in different code or a later date. |
I should have been more patient - I see that R's print method led me slightly astray, I needed to use handles[[2]][2] instead of handles[[2]][1] (the print object was just not annotated with the index order. Still my question stands though how to figure out that I want [[2]] (because [[2]][1] == "save") and then extract [[2]][2] - if you can follow me with all those indices!
****************************************************. FILE HANDLE data /NAME = "H:\DATA FILES\KINGSTON\2014". FILE HANDLE save /NAME = "data\AndyChartsGraphs". BEGIN PROGRAM R. handles <- spssdata.GetFileHandles() handles x <- handles[[2]][2] #this is how I thought you would extract the save handle #it only returns - [1] "save" - though setwd(x) getwd() png(file="Test.png",height = 750, width = 750) print(plot(0:10)) dev.off() END PROGRAM. ****************************************************. |
In reply to this post by Andy W
You could bracket this (even more) to death,
but in the interest of vision health, try it like this
begin program r. handles <- spssdata.GetFileHandles() for (i in 1:length(handles)) { z = handles[i][[1]] if (z[[1]] == "save") { thepath = z[[2]] } } print(thepath) end program. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: Andy W <[hidden email]> To: [hidden email], Date: 02/25/2014 08:21 AM Subject: [SPSSX-L] Extracting string from "spssdata.GetFileHandles()" list (in R) Sent by: "SPSSX(r) Discussion" <[hidden email]> This is more of an R question - but I ask here as it has to do with meta data retrieved from SPSS. So I have a set of FILE HANDLES, and I want to extract the "save" FILE HANDLE and map the current R directory to that location. I'm confused how to extract the locations out of the list that R returns when I use the spssdata.GetFileHandles() function. Example syntax below: ****************************************************. FILE HANDLE data /NAME = "H:\DATA FILES\KINGSTON\2014". FILE HANDLE save /NAME = "data\AndyChartsGraphs". BEGIN PROGRAM R. handles <- spssdata.GetFileHandles() handles #handles[[2]][1] #this is how I thought you would extract the save handle #it only returns - [1] "save" - though END PROGRAM. ****************************************************. Below is the subsequent output: ---------------------------------------------------------------------------- [[1]] [1] "data" "H:\\DATA FILES\\KINGSTON\\2014" "windows-1252" [[2]] [1] "save" "H:\\DATA FILES\\KINGSTON\\2014\\AndyChartsGraphs" [3] "windows-1252" ---------------------------------------------------------------------------- Reading the R output is difficult, but you can see what I thought would be the code to extract the "H:\\DATA FILES\\KINGSTON\\2014\\AndyChartsGraphs" portion, but I was incorrect. Can any R guru's here tell me how to extract the relevant string so I can set my R working directory accordingly? Also the FILE HANDLE I will always want will be named "save" - but I may have more than 2 FILE HANDLES, so I presume just grabbing the string based on the index number of the list is not optimal, as its location may change if I add in more FILE HANDLES in different code or a later date. ----- Andy W [hidden email] http://andrewpwheeler.wordpress.com/ -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Extracting-string-from-spssdata-GetFileHandles-list-in-R-tp5724621.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
In reply to this post by Andy W
I like to store all the handles in one object.
file handle tempdir /name = "%temp%". * in R. begin program r. fh <- spssdata.GetFileHandles() file_handles <- sapply(fh, function(x) x[2]) names(file_handles) <- sapply(fh, function(x) x[1]) tempdir <- file_handles["tempdir"] print(sprintf("tempdir in R: %s", tempdir)) end program. * the equivalent in Python. begin program python. import spss, spssaux file_handles = {f[0]: f[1] for f in spss.GetFileHandles()} tempdir = file_handles["tempdir"] print "(1) tempdir in Python", tempdir print "(2) tempdir in Python", spssaux.FileHandles().resolve("tempdir") end program. Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -------------------------------------------- On Tue, 2/25/14, Andy W <[hidden email]> wrote: Subject: Re: [SPSSX-L] Extracting string from "spssdata.GetFileHandles()" list (in R) To: [hidden email] Date: Tuesday, February 25, 2014, 4:31 PM I should have been more patient - I see that R's print method led me slightly astray, I needed to use handles[[2]][2] instead of handles[[2]][1] (the print object was just not annotated with the index order. Still my question stands though how to figure out that I want [[2]] (because [[2]][1] == "save") and then extract [[2]][2] - if you can follow me with all those indices! ****************************************************. FILE HANDLE data /NAME = "H:\DATA FILES\KINGSTON\2014". FILE HANDLE save /NAME = "data\AndyChartsGraphs". BEGIN PROGRAM R. handles <- spssdata.GetFileHandles() handles x <- handles[[2]][2] #this is how I thought you would extract the save handle #it only returns - [1] "save" - though setwd(x) getwd() png(file="Test.png",height = 750, width = 750) print(plot(0:10)) dev.off() END PROGRAM. ****************************************************. ----- Andy W [hidden email] http://andrewpwheeler.wordpress.com/ -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Extracting-string-from-spssdata-GetFileHandles-list-in-R-tp5724621p5724622.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
In reply to this post by Jon K Peck
Thank you Jon - exactly what I was looking for.
Albert-Jan - thanks for that example as well. Wrapping my head around the different R objects I find difficult (my googling to find similar examples was not being very productive either - list is a difficult search term). It took me a bit to figure out that "names(file_handles) <- sapply(fh, function(x) x[1])" returns a vector with the index "name" labels as the original SPSS mapped string. If I had figured something out myself I would have probably tried to coerce it to a data frame (as I just tend to think in data frames) - but this is clearly a better format for the data to be in than a dataframe. Thank you both again, Andy |
Free forum by Nabble | Edit this page |