Opening Workspace in R and rename it - r

I'm pretty new with R and have a basic issue.
I am trying to open multiple Workspaces in Rstudio to merge them. Unfortunately, every time I'm opening a Workspace, it takes the name "x" (instead of its file's name). Then when I want to open another workspace it overwrites on the previous one and also takes the name "x".
Can anyone help me with this pretty easy issue?
Thanks a lot in advance!

You can try something like this:
myvalue1 <- get(load("YourWorkspace1.RData"))
myvalue2 <- get(load("YourWorkspace2.RData"))
So that if YourWorkspace1 and YourWorkspace2 contains a variable that has the same name, myvalue1 will take the value of your variable stored in YourWorkspace1 and same for myvalue2.
But if the workspace you want to save only contains one variable, I suggest you store it using saveRDS:
saveRDS(x, file = "x.rds")
And then load it like that :
myvalue <- readRDS("x.rds")
I hope this is clear.

Related

Can convert a string to an object but can't save() it -- why? [duplicate]

I am repeatedly applying a function to read and process a bunch of csv files. Each time it runs, the function creates a data frame (this.csv.data) and uses save() to write it to a .RData file with a unique name. Problem is, later when I read these .RData files using load(), the loaded variable names are not unique, because each one loads with the name this.csv.data....
I'd like to save them with unique tags so that they come out properly named when I load() them. I've created the following code to illustrate .
this.csv.data = list(data=c(1:9), unique_tag = "some_unique_tag")
assign(this.csv.data$unique_tag,this.csv.data$data)
# I want to save the data,
# with variable name of <unique_tag>,
# at a file named <unique_tag>.dat
saved_file_name <- paste(this.csv.data$unique_tag,"RData",sep=".")
save(get(this.csv.data$unique_tag), saved_file_name)
but the last line returns:
"Error in save(get(this_unique_tag), file = data_tag) :
object ‘get(this_unique_tag)’ not found"
even though the following returns the data just fine:
get(this.csv.data$unique_tag)
Just name the arguments you use. With your code the following works fine:
save(list = this.csv.data$unique_tag, file=saved_file_name)
My preference is to avoid the name in the RData file on load:
obj = local(get(load('myfile.RData')))
This way you can load various RData files and name the objects whatever you want, or store them in a list etc.
You really should use saveRDS/readRDS to serialize your objects.
save and load are for saving whole environments.
saveRDS(this.csv.data, saved_file_name)
# later
mydata <- readRDS(saved_file_name)
you can use
save.image("myfile.RData")
This worked for me:
env <- new.env()
env[[varname]] <- object_to_save
save(list=c(varname), envir=env, file='out.Rda')
You could probably do it without a new env (but I didn't try this):
.GlobalEnv[[varname]] <- object_to_save
save(list=c(varname), envir=.GlobalEnv, file='out.Rda')
You might even be able to remove the envir variable.

How to read results from one R script to another

Is there anyway I can store the results of one variable in one R script, and make them available to another R script?
I have this basic script in one file:
B5b=fit(y~.,d_treino_both,model="randomforest",task="class")
P5b=predict(B5b,d_teste)
x=d_teste$y
m5b=mmetric(x,P5b,metric=c("ACC","ACCLASS","CONF", "ROC"))
mgraph(x,P5b,graph= "ROC", baseline=TRUE)
print(m5b)
P5b
Then, I want to make the resuts of P5b variable available to another script.
Any help?
Not sure if this is what you are looking for. I think one way you can do that is to source the script1 in script2. I would do something like this and remove any additional variables using rm.
source("script1.R")
Perhaps you could try something with dput and the clipboard. Basically, this is just copying the dput of an object to the clipboard and then evaluating the clipboard in the second script. Note that you cannot use the clipboard in the meantime.
# first script
obj <- capture.output(dput(matrix(1:100, 10, 10)))
writeClipboard(str = obj)
# second script
obj2 <- eval(parse(text = readClipboard()))

Reading a binary file in R [duplicate]

I saw some similar qestions and I tried to work it out on my own, but I couldn't. This is my problem:
I have to load a isfar.RData file to use it in other computation (which are not important to describe here). And I would like to simply see how looks data in this isfar.RData file e.g. what numbers, columns, rows it carries.
First I load my file:
isfar<-load("C:/Users/isfar.RData")
When I try to obtain this information (I'm using Rcmdr) by ls() function or marking isfar at the beginning after loading I get in the output window: [1] "isfar" instead of the table. Why?
Thanks a lot, I appreciate all of the answers! Hope it's comprehensible what I wrote, Im not a native speaker.
I think the problem is that you load isfar data.frame but you overwrite it by value returned by load.
Try either:
load("C:/Users/isfar.RData")
head(isfar)
Or more general way
load("C:/Users/isfar.RData", ex <- new.env())
ls.str(ex)
you can try
isfar <- get(load('c:/users/isfar.Rdata'))
this will assign the variable in isfar.Rdata to isfar . After this assignment, you
can use str(isfar) or ls(isfar) or head(isfar) to get a rough look of the isfar.
Look at the help page for load. What load returns is the names of the objects created, so you can look at the contents of isfar to see what objects were created. The fact that nothing else is showing up with ls() would indicate that maybe there was nothing stored in your file.
Also note that load will overwrite anything in your global environment that has the same name as something in the file being loaded when used with default behavior. If you mainly want to examine what is in the file, and possibly use something from that file along with other objects in your global environment then it may be better to use the attach function or create a new environment (new.env) and load the file into that environment using the envir argument to load.
This may fit better as a comment but I don't have enough reputation, so I put it here.
It worth mentioning that the load() function will retain the object name that was originally saved no matter how you name the .Rdata file.
Please check the name of the data.frame object used in the save() function. If you were using RStudio, you could check the upper right panel, Global Environment-Data, to find the name of the data you load.
If you have a lot of variables in your Rdata file and don't want them to clutter your global environment, create a new environment and load all of the data to this new environment.
load(file.path("C:/Users/isfar.RData"), isfar_env <- new.env() )
# Access individual variables in the RData file using '$' operator
isfar_env$var_name
# List all of the variable names in RData:
ls(isfar_env)
You can also import the data via the "Import Dataset" tab in RStudio, under "global environment."
Use the text data option in the drop down list and select your .RData file from the folder.
Once the import is complete, it will display the data in the console.
Hope this helps.
It sounds like the only varaible stored in the .RData file was one named isfar.
Are you really sure that you saved the table? The command should have been:
save(the_table, file = "isfar.RData")
There are many ways to examine a variable.
Type it's name at the command prompt to see it printed. Then look at str, ls.str, summary, View and unclass.
You don't seem to need to assign it to a variable. That bit magically happens. In fact, assigning it to a variable might mean you end up with two variables with the same data.
get(load('C:/Users/isfar.Rdata'))
Or if it's in the same folder as your R code...
get(load('isfar.Rdata'))
isfar<-load("C:/Users/isfar.RData")
if(is.data.frame(isfar)){
names(isfar)
}
If isfar is a dataframe, this will print out the names of its columns.
num <- seq(0, 5, length.out=10) #create object num
num
[1] 0.00 1.25 2.50 3.75 5.00
save(num, file = 'num.RData') #save num ro RData
rm(num) #remove num
load("num.RData") #load num from RData
num
[1] 0.00 1.25 2.50 3.75 5.00
> isfar<-load("num.RData")
> typeof(isfar)
[1] "character"
> isfar #list objects saved in RData
[1] "num"

How to see data from .RData file?

I saw some similar qestions and I tried to work it out on my own, but I couldn't. This is my problem:
I have to load a isfar.RData file to use it in other computation (which are not important to describe here). And I would like to simply see how looks data in this isfar.RData file e.g. what numbers, columns, rows it carries.
First I load my file:
isfar<-load("C:/Users/isfar.RData")
When I try to obtain this information (I'm using Rcmdr) by ls() function or marking isfar at the beginning after loading I get in the output window: [1] "isfar" instead of the table. Why?
Thanks a lot, I appreciate all of the answers! Hope it's comprehensible what I wrote, Im not a native speaker.
I think the problem is that you load isfar data.frame but you overwrite it by value returned by load.
Try either:
load("C:/Users/isfar.RData")
head(isfar)
Or more general way
load("C:/Users/isfar.RData", ex <- new.env())
ls.str(ex)
you can try
isfar <- get(load('c:/users/isfar.Rdata'))
this will assign the variable in isfar.Rdata to isfar . After this assignment, you
can use str(isfar) or ls(isfar) or head(isfar) to get a rough look of the isfar.
Look at the help page for load. What load returns is the names of the objects created, so you can look at the contents of isfar to see what objects were created. The fact that nothing else is showing up with ls() would indicate that maybe there was nothing stored in your file.
Also note that load will overwrite anything in your global environment that has the same name as something in the file being loaded when used with default behavior. If you mainly want to examine what is in the file, and possibly use something from that file along with other objects in your global environment then it may be better to use the attach function or create a new environment (new.env) and load the file into that environment using the envir argument to load.
This may fit better as a comment but I don't have enough reputation, so I put it here.
It worth mentioning that the load() function will retain the object name that was originally saved no matter how you name the .Rdata file.
Please check the name of the data.frame object used in the save() function. If you were using RStudio, you could check the upper right panel, Global Environment-Data, to find the name of the data you load.
If you have a lot of variables in your Rdata file and don't want them to clutter your global environment, create a new environment and load all of the data to this new environment.
load(file.path("C:/Users/isfar.RData"), isfar_env <- new.env() )
# Access individual variables in the RData file using '$' operator
isfar_env$var_name
# List all of the variable names in RData:
ls(isfar_env)
You can also import the data via the "Import Dataset" tab in RStudio, under "global environment."
Use the text data option in the drop down list and select your .RData file from the folder.
Once the import is complete, it will display the data in the console.
Hope this helps.
It sounds like the only varaible stored in the .RData file was one named isfar.
Are you really sure that you saved the table? The command should have been:
save(the_table, file = "isfar.RData")
There are many ways to examine a variable.
Type it's name at the command prompt to see it printed. Then look at str, ls.str, summary, View and unclass.
You don't seem to need to assign it to a variable. That bit magically happens. In fact, assigning it to a variable might mean you end up with two variables with the same data.
get(load('C:/Users/isfar.Rdata'))
Or if it's in the same folder as your R code...
get(load('isfar.Rdata'))
isfar<-load("C:/Users/isfar.RData")
if(is.data.frame(isfar)){
names(isfar)
}
If isfar is a dataframe, this will print out the names of its columns.
num <- seq(0, 5, length.out=10) #create object num
num
[1] 0.00 1.25 2.50 3.75 5.00
save(num, file = 'num.RData') #save num ro RData
rm(num) #remove num
load("num.RData") #load num from RData
num
[1] 0.00 1.25 2.50 3.75 5.00
> isfar<-load("num.RData")
> typeof(isfar)
[1] "character"
> isfar #list objects saved in RData
[1] "num"

How to save() with a particular variable name

I am repeatedly applying a function to read and process a bunch of csv files. Each time it runs, the function creates a data frame (this.csv.data) and uses save() to write it to a .RData file with a unique name. Problem is, later when I read these .RData files using load(), the loaded variable names are not unique, because each one loads with the name this.csv.data....
I'd like to save them with unique tags so that they come out properly named when I load() them. I've created the following code to illustrate .
this.csv.data = list(data=c(1:9), unique_tag = "some_unique_tag")
assign(this.csv.data$unique_tag,this.csv.data$data)
# I want to save the data,
# with variable name of <unique_tag>,
# at a file named <unique_tag>.dat
saved_file_name <- paste(this.csv.data$unique_tag,"RData",sep=".")
save(get(this.csv.data$unique_tag), saved_file_name)
but the last line returns:
"Error in save(get(this_unique_tag), file = data_tag) :
object ‘get(this_unique_tag)’ not found"
even though the following returns the data just fine:
get(this.csv.data$unique_tag)
Just name the arguments you use. With your code the following works fine:
save(list = this.csv.data$unique_tag, file=saved_file_name)
My preference is to avoid the name in the RData file on load:
obj = local(get(load('myfile.RData')))
This way you can load various RData files and name the objects whatever you want, or store them in a list etc.
You really should use saveRDS/readRDS to serialize your objects.
save and load are for saving whole environments.
saveRDS(this.csv.data, saved_file_name)
# later
mydata <- readRDS(saved_file_name)
you can use
save.image("myfile.RData")
This worked for me:
env <- new.env()
env[[varname]] <- object_to_save
save(list=c(varname), envir=env, file='out.Rda')
You could probably do it without a new env (but I didn't try this):
.GlobalEnv[[varname]] <- object_to_save
save(list=c(varname), envir=.GlobalEnv, file='out.Rda')
You might even be able to remove the envir variable.

Resources