Save a data frame to a file addressing by name - r

I have a data frame and a text variable containing the name of this data frame:
adsl = data.frame(a=2, b=7, w=17)
ds_name = "adsl"
I want to save my data frame from the workspace to the file named "dest_file". The code should be wrapped into a function get_r()
with the data frame name as an argument:
get_r(ds_name="adsl")
So I need to avoid using the explicit name "adsl" inside the code.
The following works almost correctly but the resulting data frame is called "temp_dataset", not "adsl":
get_r = function(ds_name){
temp_dataset = eval(parse(text=ds_name))
save(temp_dataset, file = "dest_file")
}
Here is another option which works wrong (the text string is being saved, not the data frame):
get_r = function(ds_name){
save(ds_name, file = "dest_file")
}
What should I do to make R just execute
save(adsl, file="dest_file")
inside the function? Thank you for any help.

Try
save(list = ds_name, file = "dest_file")
The list argument in save() allows you to pass the name of the data as a character string. See help(save) for more.

Related

ConvertFrom-StringData values stored in variable

I'm new to Powershell and I'm putting together a script that will populate all variables from data stored in a Excel file. Basically to create numerous VMs.
This works fine apart from where i have a variable with multiple name/value pairs which powershell needs to be a hashtable.
As each VM will need multiple tags applying, i have a column in excel called Tags.
The data in the field would look something like: "Top = Red `n Bottom = Blue".
I'm struggling to use ConvertFrom-StringData to create the hashtable however for these tags.
If i run:
ConvertFrom-StringData $exceldata.Tags
I end up with something like:
Name Value
---- -----
Top Red `n bottom = blue
I need help please with formatting the excel field correctly so ConvertFrom-StringData properly creates the hashtable. Or a better way of achieving this.
Thanks.
Sorted it, formatted the excel field as: Dept=it;env=prod;owner=Me
Then ran the following commands. No ConvertFrom-StringData required.
$artifacts = Import-Excel -Path "C:\temp\Artifacts.xlsx" -WorkSheetname "VM"
foreach ($artifact in $artifacts) {
$inputkeyvalues = $artifact.Tags
# Create hashtable
$tags = #{}
# Split input string into pairs
$inputkeyvalues.Split(';') |ForEach-Object {
# Split each pair into key and value
$key,$value = $_.Split('=')
# Populate $tags
$tags[$key] = $value
}
}

Saving .maf file as a table

I am trying to save a .maf file as a table, but I always get the error below:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘structure("MAF", package = "maftools")’ to a data.frame
This is the code I am using:
library(maftools)
laml.maf <- "/Users/PC/mc3.v0.2.8.PUBLIC.maf"
laml = read.maf(maf = laml.maf)
write.table(laml, file="/Users/PC/tp53atm.txt")
I understand that the .maf file has several fields, but I am not sure how to isolate them to save as a table. Any help would be much appreciated!
The problem is, that the write.table function doesn't know how to deal with an object of class MAF.
However, you can access the underlying data like this:
write.table(laml#data, file="/Users/PC/tp53atm.txt")
But note that this way you will only export the raw data, whereas the MAF object contains various other meta data:
> slotNames(laml)
[1] "data" "variants.per.sample" "variant.type.summary" "variant.classification.summary"
[5] "gene.summary" "summary"
"maf.silent" "clinical.data"
>

Getting Error "could not find function "assign<-" inside of colnames()

I'm using assign() to assign some new data frames from some other data frame. I then want to name some of the columns in the new data frame. When I use assign() to create the new data frames it works fine. But when I use the assign() inside of colnames() is gives the error 'Error "could not find function "assign<-".'
Here's my snippet of code(abbreviated of course):
for(i in 1:value) {
assign(Name[i], Old.Data.Frame[Old.Data.Frame$1 == Index[i]]) #I'm going to call this line of code 'New Data Frame' for brevity
for(j in 1:ncol(New Data Frame)) {
colnames(New Data Frame)[j] = as.character(Old.Data.Frame[3,j])
I do all this assign() stuff because the names of the Old Data Frame constantly change and I can create any concrete variables in my code, only the dimentions of the frame stay the same.
The only error in this code is that R cannot "find function assign<- in colnames(...". I'm flustered because assign() had just worked in the line before, any help is appreciated, thanks!
You have a list of variable names in Name, which you assign a value (your code block).
for(i in 1:value) { assign(Name[i], Old.Data.Frame[Old.Data.Frame$1 == Index[i]]) }
Could you then try (note I'm separating this code block for debugging purposes):
for(i in 1:value) { colnames(get(Names[i])) <- as.character(Old.Data.Frame[3,] }
get will retrieve the data (data.frame) assigned to the variable name Names[i] (character)

Concatenate variables in R

I want to create an object in R, which will contain one string, with a few variables (in my case it is file path). When I try to use paste to concatenate the file paths I can see only one last variable instead of all variables in one string. I use next code:
for(i in seq_len(nrow(samples))) {
lib = samples$conditions[i]
txtFile = file.path(lib, "hits.txt")
testfiles = paste(txtFile, sep = ',')
}
print(testfiles)
and get something like
cond/hits.txt,
instead of
cond/hits.txt,cond1/hits.txt,cond2/hits.txt and so on
Thank you very much for help

R Write a dbf file

I want to create a dbf file to export a data frame to, I already tried:
write.dbf(MyDF,MyDF.dbf,factor2char = F)
but get the error code:
Error in write.dbf(MyDF, MyDF.dbf, factor2char = F) :
object 'MyDF.dbf' not found
I can tell why but I just can't find a solution.
The 2nd parameter should be a string. Try this:
write.dbf(MyDF,"MyDF.dbf",factor2char = F)
The error you are getting is that MyDF.dbf is being treated as a variable name and you haven't defined a variable with that name.

Resources