How to cope with variable names that include forward slash ("/")? - r

I am writing a script in R to perform automated analysis of some files we receive as output from a lab instrument.
The final output of my script is a wide .csv file with multiple headers, and to better suit the end user, the headers are properly formatted as they would expect.
For example, one of the parameter is the following: 'dm/ds_max'.
Everything works fine when this parameter is written as variable in the table header or when I have to use this parameter as variable in ggplot (for example to automatically generate a plot title with dm/ds_max), but becomes problematic when I am trying to use the name of the variable in the paste() command to save the graph.
As you can imagine, the / in the paste() is interpreted as directory delimiter and the files are then saved in the wrong folder.
For example, when the variable name is colour, the plot are properly saved in the proper folder as img/colour.jpg. When the variable name is dm/ds_max, the file is saved as img/dm/ds_max.jpg, putting it in a non-existing folder.
Is there a way -beside changing the name of the variable- to bypass this conflict between writing tables in a nice human readable format and making automated plots with the same variable?
Thank you

Related

How to keep style format unchanged after writing data using openxlsx in R

I am using openxlsx in order to write the outputs of my data.
I have used the following code to read my data using readxl.
df1=read_excel("C:/my_data.xlsx",skip=2);
Now I want to write the output and keep the original Excel file using any possible package. I have used the following codes, but it does not keep the original Excel file. Can we do it it in R packages?
write.xlsx(df1, 'C:/mydata.xlsx',skip=2)
Given your code, you should nhave two different data files in your working directory:
"my_data.xlsx" (the one that you loaded), and "mydata.xlsx" (the one that you created through R). R shouldn't overwrite your files if you give them different names.
If there's only one file, are you sure you didn't use the same name for both files? If so, then everything should work fine if you give the files different names (e.g. "my_file1.xlsx" and "my_file2.xlsx")!
Also, in general, it's a good idea to give data files an informative name so that you don't accidentally delete/overwrite files that you need. For example, if the original excel data is you raw data, consider naming it "data_raw.xlsx", and make sure that you only read it, and whenever you make some changes to it, save it under a different name (e.g. "data_processed1.xlsx").
You can also save data files in the native R format .rds using the save_rds() function, this is especially helpful if you want to keep special attributes of variables such as factors, etc...
Hope this helps!

BlySky Statistics - File naming conventions

When opening file 'TestFile.RData' in BlueSky Statistics it is opened with this name PLUS Dataset3 attached. Looks like this in tab TestFile.RData(Dataset3)
I would like to use my original name when using r code in the r command editor but from what I see BlueSky wants me to use the Dataset3 name.
Please clarify this file name issue for me.
If my original name is changed I see issues with reproducing things - as the given name of Dataset3 is not controllable.
Regards
Your observation is correct. When ever a file is opened in BlueSky Statistics (that is not an R datafile) we create a dataframe object in R. We name these objects sequentially namely Dataset1, Dataset2,Dataset3, etc. We could always use the name of the original file, however we went with Dataset1,Dataset2,Dataset3 for compatibility with SPSS. Many of our users come from SPSS and that is exactly what SPSS does. There is a simple work around, see below.
To work around this you need to change the default code we use to open the dataset. To see the code in the output window, Go to the top level menu Tools , Tools->Configuration settings->Select the Output tab and select the checkbox near the text "Show syntax in output window"
The code you will see when you open a dataset in the output Window is
BSkyloadDataset(fullpathfilename='C:/Users/Aaron_2/Documents/BlueSky Statistics/Sample Datasets/IRT/engagement.csv', filetype='CSV', worksheetName='',load.missing=FALSE, character.to.factor=FALSE, csvHeader=TRUE, isBasketData=FALSE, trimSPSStrailing=FALSE, sepChar=',', deciChar='.', datasetName='Dataset2')
All you need to do is change the datasetName parameter to the name you want to use
I will also add an enhancement to make the default behavior of naming the dataset when opening files to be the name of the file. This is easy to do.
With R datasets this is not a problem because we load all dataframe objects into the grid. The name of the dataset in the grid, continues to be the dataset object
BlueSky is one of the few packages that use R and allow you to open and work on multiple data files at once. This naming approach is its way of allowing that while using files that have not yet been stored as R data files (.RData). After importing data from a non-R file, simply use "File> Save as" and save it as an R Object (.RData). The next time you open that file, it will maintain the name you've given it.

Save text files with a different extension in r

I'm trying to write out an R output to a text file that is not saved as .txt but as some other unique identifier (for example .prt). I know that's possible with matlab, but I don't know how to get that to work with R.
I can't find any package to do that, and when I try to specify the extension in the file name it give me an error and doesn't save.
Any idea would be greatly welcome! Thank you.
Unless you are using some specialized package, a lot of standard R functions for writing data to files have a file= parameter (or similar) to let you specify whatever the filename (and extension) you want. For example:
dummy.data <- matrix(rnorm(25),ncol=5)
### in reality you could just write file="dummyfile.prt" as one string
### but for demonstration purposes, you can use paste0() or paste(,sep='')
### to create a new file name using some variable prefix and your extension
### ".prt"
### sep='\t' makes the output tab-delimited
write.table(dummy.data,file=paste0("dummyfile",".prt"),sep='\t')

Saving R dataframe from script

It should be very simple, but for now cannot figure it out. Say I create a generic dataframe with some data in a loop, let's call it df.
Now I want to assign a specific name to it and want to save it to specific destination. I generate two character variables - filename and file_destination and try to use the following in the script code:
assign(filename, df)
save(filename, file = file_destination)
Of course it save just a string with a name in the file and not the actual data.
How do i save the dataframe created via assign(filename,df)?
Try save(list=filename,file=file_destination). Also, use better names for your variables. filename for an object which is not a file name is very odd.
Put this as answer, to ensure other people find it easily.

Request user to identify file location and auto-extract variable name from file location in R

I am EXTREMELY new to R, and programming in general, so thank you for your patience.
I am trying to write a script which reads values from a .txt file and after some manipulation plots the results. I have two questions which are somewhat coupled.
First, is there a function which asks the user to identify the location of a file? i.e. User runs script. Script opens up file navigation prompt and requests user to navigate to and select relevant file.
Currently, I have to manually identify the file and location in R. e.g.
spectra.raw <- read.table("C:\Users\...\file1.txt", row.names=NULL, header = TRUE)
I'd rather have the user identify the file location each time the script is run. This will be used by non-tech people, and I don't trust them to copy/paste file locations into R.
The second question I've been struggling with is, is it possible to create a variable name based off the file selected? For example, if the user selects "file1.txt" I'd like R to assign the output of read.table() to a variable named "file1.raw" much like the above "spectra.raw"
If it helps, all the file names will have the exact same number of characters, so if it's possible to select the last say 5 characters from the file location, that would work.
Thank you very much, and please excuse my ignorance.
See file.choose. Though I believe it behaves slightly differently on different platforms, so beware of that.
See assign, i.e. assign("fileName",value). You'll want to parse the file path that file.choose spits back using string manipulation functions like substr or strsplit.
Try
file.choose
I think it can do what you want.
For example,
myfile <- file.choose()
Enter file name: adataset.Rdata
load(myfile)
myfile contains the name of the file so you don't have to do anything special.

Resources