Loading a .mat file that has dots in the variable name - r

I am trying to load a .mat file using the 'load' function. The file seems to load correctly, but some of the variables are not accessible. My best guess is that this is because the name of the variables (which was set by someone else in R and then exported to a .mat file) contains a dot (see pic)
As an example, one of the variables is a vector called 'final.product'. If I try to access the first element, I would get this
>>final.product(1)
Undefined variable "final" or class "final.product".
This works fine with the variables whose name do not contain any dots.
I have tried to modify the name from the Workspace (i.e. by clicking once on the name and removing the dot), but I am prompted with an error message saying
All the other things I have tried in the Command Window give me errors related to the fact that my variables don't exist or that the name is invalid, for example
>> T = from.product;
Undefined variable "from" or class "from.product".
Or
>>T = load('C:\Users\Loup\Downloads\5. Source-Intermediates-Products.mat', 'final.product');
T = T.('final.product');
Error using load
Invalid field name: 'final.product'.
So I guess I have two questions:
1) can someone confirm that my issue is caused by having dots in the variable names?
2) is there a way to rename a variable that does not require being able to read them?
Thanks a lot!

I did not manage to find a good answer to what I wanted to do. In case someone has the same issue, I ended up having to get the file in R and change the name there.
Cheers

Related

Write_ods keeps writing an empty data frame into directory?

I am trying to export a data frame into an ods sheet, however it is not working correctly.
I have tried exporting the information to many different directories but all have failed. I have also been able to use read_ods in the correct way. When using write_ods I keep getting zero errors yet the directory I am writing to is always empty after I open it.
print(final)
write_ods(x = final, path = "C:/Users/Administrator/Desktop/SpendingOptimizerStreamlined/CoeffSheet.ods")
temp <- read_ods(path = "C:/Users/Administrator/Desktop/SpendingOptimizerStreamlined/CoeffSheet.ods")
print(temp)
I have printed the final data frame that I want to export and it is full of the correct data.
I then write to the directory and get no errors.
To confirm that the previous command worked correctly, I then read in the previously exported data.
Then I print the data out and see zero columns and zero rows.
Not quite sure why this keeps happening? I am curious to know if the write_ods command is still supported or am I just doing something wrong? Thank you!

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.

write r file to csv -- "object not found"

Hello I need to make my R file into a CSV file. I have searched stackoverflow for different methods to accomplish this, but nothing is working. Each time, it says:
Error in is.data.frame(x) : object 'refine_clean' not found
My file is definitely named refine_clean.R. I have linked a screenshot here:
R Studio CSV Error Message
Your file is named refine_clean but you have no data.frame or object named refine_clean, which is what write_table and write.csv are looking for.
based on the screen shot, it is not clear what you want to produce. If it is the last object you created (d5), you may want something like this
write.csv(d5, "refine_clean.csv")
you cant convert all your file as an csv, you need to select the object, like d5
write.csv(d5,file = "name_of_the_file")

R Shiny: 'error cannot open the connection'

so I have a Shiny app where I'm trying to read in a user-identified input file.
Towards that end, my ui.R has the line fileInput("predictor2", label = "Predictor Values") and I try to read the file using the line predictor <- read.delim("input$predictor2") in my server.R file.
However, I get a message saying Error: Cannot open the connection. If I don't try to read in the file and use another matrix of values, the code works fine. Any advice for how to fix this problem or more detail that would be useful?
You code is looking for a file with the literal name input$predictor2 which presumably does not exist. You first need to remove the quotes from around it, then add which column of the return actually has the path to the data, e.g.:
read.delim(input$predictor2$datapath)
See the help for fileInput for an example that checks to make sure something has been uploaded first.

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