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()))
Related
I'm using
sapply(list.files('scritps/', full.names=TRUE),source)
to run 80 scripts at once in the folder "scripts/" and I do not know exactly how does this work. There are "intermediate" objects equally named across scripts (they are iterative scritps across 80 different biological populations). Does each script only use its own objects? Is there any risk of an script taking the objects of other "previous" script that has not been yet deleted out of the memory, or does this process works exactly like if was run manually sequentially one by one?
Many thanks in advance.
The quick answer is: each script runs independently. Imagine you run a for loop iterating through all the script files instead of using sapply - it should be the same in results.
To prove my thoughts, I just did an experiment:
# This is foo.R
x <- mtcars
write.csv(x, "foo.csv")
# This is bar.R
x <- iris
write.csv(x, "bar.csv")
# Run them at once
sapply(list.files(), source)
Though the default of "local" argument in source is FALSE, it turns out that I have two different csv files in my working directory, one named "foo.csv" with mtcars data frame, and the other named "bar.csv" with iris data frame.
There are global variables that you can declare out a function. As it's name says they are global and can be re-evaluated. If you declare a var into a function it will be local variable and only will take effect inside this concrete function, it will not exists out of its own function.
Example:
Var globalVar = 'i am global';
Function foo(){
Var localVar = 'i don't exist out of foo function';
}
If you declared globalVar on the first script, and you call it on the latest one, it will answer. If you declared localVar on some script and you call it into another or out of the functions or in another function you'll get an error (var localVar is not declared / can't be found).
Edit:
Perhaps, if there aren't dependences between scripts (you don't need one to finish to continue with another) there's no matter on running them on parallel or running them secuentialy. The behaviour will be the same.
You've only to take care with global vars, local ones can't infer into another script/function.
Just a question... supposing I have the following R file, called script.R:
calculation1 <- function(){
# multiple calculations goes here.
}
calculation2 <- function(){
# multiple calculations goes here.
}
And I want to open this file on another file in order to use calculation1 and calculation2 multiple times in different cases.
Is that possible? I mean, something like the following.
calculation_script <- Some way, function or method that instantiate script.R in this variable.
calculation_script.calculation1()
calculation_script.calculation2()
I need to do this because I need to open script.R file in a Tableau Calculated field.
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.
I need to simulate some data, and I would like to have a function with everything built in, so I just need to run
simulate(scenario="xxx")
This function stores all the simulated datasets for the designated scenario in a list called simdat. Within the function, I want to rename that list "simdat.xxx" and save it out as "simdat_xxx.RData", so later on I can just load this file and have access to the list simdat.xxx. I need the list to have a name that refers specifically to which batch it is, because I am dealing with a lot of batches and I may want to load several at the same time.
Is there a way to, within a function, make a name and use it to name an object? I searched over and over again and could not find a way to do this. In desperation, I am resorting to doing this: within the function,
(a) write a temporary script using paste, which looks like this
temp.fn <- function(simdat){
simdat.xxx <- simdat
save(simdat.xxx,file="simdat_xxx.RData")
}
(b) use writeLines to write it out to a .R file
(c) source the file
(d) run it
This seriously seems like overkill to me. Is there a better way to do this?
Thanks much for your help!
Trang
Try this,
simulate <- function(scenario="xxx"){
simdat <- replicate(4, rnorm(10), simplify=FALSE)
data_name <- paste("simdat", scenario, sep=".")
assign(data_name, simdat)
save(list = data_name, file = paste0("simdat_", scenario, ".Rdata"))
}
I'm working on a script in R that processes some data and writes an output file. I'd like that output file to be named in a way that reflects the input file, and I'd like something about the file to be unique so older files aren't overwritten.
So I thought to use a timestamp. But this isn't working the way I'd hoped, and I'd like to understand what's happening and how to do this correctly.
This is how I'm trying to name the file (file_base is the name of the input file):
now<-format(Sys.time(), "%b%d%H%M%S")
outputfile<-cat(file_base, "-",now,"-output.txt", sep="")
The output of this pair of functions looks great. But executing 'outputfile' subsequently results in 'NULL' as output.
What's happening here and how can I create an output filename with the properties that I'd like?
You're confusing cat and paste. You want:
outputfile <- paste(file_base, "-",now,"-output.txt", sep="")
You can also use the function sprintf(), it's a wrapper for the C function.
example:
filepath <- file.path(outdir, sprintf("abcdefg_%s.rda", name))
You could also use the separator argument of paste:
outputfile <- paste(file_base,now,"output.txt", sep="-")