Saving data contained in a file - r

We are working in the Nought and Crosses game and we have the following problem:
We have created a function that suggests you to put 2 names. If you don't have any file saved, the function creates a new one. But, in the case that you already have one, we need the last archive to be edited and to add the new data without deleting the previous ones.
The following is the code we currently have
option1<-function() {
Name1=readline(prompt="Please enter the name of player 1: ")
Name2=readline(prompt="Please enter the name of player 2: ")
if(file.exists("C:\\Documents\\Playernames.txt")) {
Data=read.table("C:\\Users\\Documents\\Playernames.txt")
print(Data)
Newnames=c(Name1, Name2)
Newdata=data.frame(Newnames)
write.table(Newdata, "C:\\Documents\\Playernames.txt")
} else {
Names=c(Name1, Name2)
Data=data.frame(Names)
write.table(Data, "C:\\Users\\Documents\\Playernames.txt")
}
}

Related

Create a series of new folders in for loop in R

I have create a small script that passes a vector through a loop. In this loop I am using an if else statement to check if folder exists and if not to create the folder. However, I am getting error: Error in file.exists(i) : invalid 'file' argument. This has to due with file.exist(). I dont understand why this isnt ok. I check the man using help. Seems like this should be working.
folders<- c("RawData", "Output", "BCV", "DEplots", "DEtables", "PathwayOuts", "VolcanoPLots")
for(i in 1:length(folders)){
if (file.exists(i)){
cat(paste0(i, "already exists"))
} else {
cat(paste0(i, "does not exists"))
dir.create(i)
}
}
You are looping over an index (that is, 1:length(folders) is just the vector 1:7, not the values of the folders vector itself. The easiest solution is to loop over the vector itself:
for (i in folders) {
Or, if you still want to loop over the index:
for (i in 1:length(folders)) {
if (file.exists(folders[i])){
cat(paste0(folders[i], "already exists"))
}
else {
cat(paste0(folders[i], "does not exists"))
dir.create(folders[i])
}
}
A quick tip: if you are debugging a for-loop, the place to start is to add print(i) at the start of the loop. You would have immediately seen the problem: i was an integer, not the first value of the vector.

Vectorized Operation in R causing problems with custom function

I'm writing out some functions for Inventory management. I've recently wanted to add a "photo url column" to my spreadsheet by using an API I've used successfully while initially building my inventory. My Spreadsheet header looks like the following:
SKU | NAME | OTHER STUFF
I have a getProductInfo function that returns a list of product info from an API I'm calling.
getProductInfo<- function(barcode) {
#Input UPC
#Output List of product info
CallAPI(barcode)
Process API return, remove garbage
return(info)
}
I made a new function that takes my inventory csv as input, and attempts to add a new column with product photo url.
get_photo_url_from_product_info_output <- function(in_list){
#Input GetProductInfo Output. Returns Photo URL, or nothing if
#it doesn't exist
if(in_list$DisplayStockPhotos == TRUE){
return(in_list$StockPhotoURL)
} else {
return("")
}
}
add_Photo_URL <- function(in_csv){
#Input CSV data frame, appends photourl column
#Requires SKU (UPC) assumes no photourl column
out_csv <- mutate(in_csv, photo =
get_photo_url_from_product_info_output(
getProductInfo(SKU)
)
)
}
return (out_csv)
}
#Call it
new <- add_Photo_URL(old)
My thinking was that R would simply input the SKU of the from the row, and put it through the double function call "as is", and the vectorized DPLYR function mutate would just vectorize it. Unfortunately I was running into all sorts of problems I couldn't understand. Eventually I figured out that API call was crashing because the SKU field was all messed up as it was being passed in. I put in a breakpoint and found out that it wasn't just passing in the SKU, but instead an entire list (I think?) of SKUs. Every Row all at once. Something like this:
#Variable 'barcode' inside getProductInfo function contains:
[1] 7.869368e+11 1.438175e+10 1.256983e+10 2.454357e+10 3.139814e+10 1.256983e+10 1.313260e+10 4.339643e+10 2.454328e+10
[10] 1.313243e+10 6.839046e+11 2.454367e+10 2.454363e+10 2.454367e+10 2.454348e+10 8.418870e+11 2.519211e+10 2.454375e+10
[19] 2.454381e+10 2.454381e+10 2.454383e+10 2.454384e+10 7.869368e+11 2.454370e+10 2.454390e+10 1.913290e+11 2.454397e+10
[28] 2.454399e+10 2.519202e+10 2.519205e+10 7.742121e+11 8.839291e+11 8.539116e+10 2.519211e+10 2.519211e+10 2.519211e+10
Obviously my initial getProductInfo function can't handle that, so it'll crash.
How should I modify my code, whether it be in the input or API call to avoid this vectorized operation issue?
Well, it's not totally elegant but it works.
I figured out I need to use lapply, which is usually not my strong suit. Initally I tried to nest them like so:
lapply(SKU, get_photo_url_from_product_info_output(getProductInfo())
But that didn't work. So I just came up with bright idea of making another function
get_photo_url_from_sku <- function(barcode){
return(get_photo_url_from_product_info_output(getProductInfo(barcode)))
}
Call that in the lapply:
out_csv<- mutate(in_csv, photocolumn = lapply(SKU, get_photo_url_from_sku))
And it works great. My speed is only limited by my API calls.

I have a problem with a "repeat" function in R

checknames <- function(){
gamers <- c("Rebeca","Luis","Paco")
games <- c("3","2","7")
scores <- c(100,110,50)
table <- data.frame(gamers,games,scores)
r=0
p=0
repeat{
print("Name Player 1: ")
name1=scan(,what="character",1)
for(i in 1:length(gamers)){
if(name1==gamers[i]){
r=readline(prompt = "This player is already in the file. Would you like to change the name? \n 1. Yes \n 2. No \n Select an option: ")
}
}
if(r==2){
break
}
if(r==0){
gamers=c(gamers,name1)
name1 <- data.frame(gamers=name1,games="1",scores="100")
table <- rbind(table,name1)
break
}
}
table
repeat{
print("Name Player 2: ")
name2=scan(,what="character",1)
for(i in 1:length(gamers)){
if(name2==gamers[i]){
print("This player is already in the file. Would you like to change the name?")
r=scan(,what="character",1)
}
}
if(p=="No"){
break
}
if(p==0){
gamers=c(gamers,name2)
name2 <- data.frame(gamers=name2,games="1",scores="100")
table <- rbind(table,name2)
break
}
}
table
}
table <-checknames()
I was doing a code that ask the user 2 names, and it should prove if the table has this name, and if it not, add it, and if it has the name, ask the player if he/she wants to change it.
The problem is that when the player say that he want to change the name, the repeat function do not ever break, I think that is because of I assign something wrong (I assign r==2/r==0) and that is why the repeat function still repeating.
I see a couple of issues with your code. We will focus on the 2nd repeat loop you've written for player 2.
First of all, we can simplify the the for(if( loop you use to check if the name has already been taken. by checking the list all at once instead of looping through the entire index.
if(any(name2==gamers)){
print("This player is already in the file. Would you like to change the name?")
r=scan(,what="character",1)
}
Now, moving on to some of your problems.you store the variable r in your scan but you are checking for the variable p. You should change the line in the above code block to read p=scan(,what="character",1).
Furthermore, you now have a situation where if your user2 put in a name already taken, then changed it to something else, your code will never let them out as you do not have never set p back to 0. This can be adressed by adding an else { p=<-0 } line.
All of the above changes are placed together here for you:
if(any(name2==gamers)){
print("This player is already in the file. Would you like to change the name?")
p=scan(,what="character",1)
} else {
p <- 0}

Loop works outside function but in functions it doesn't.

Been going around for hours with this. My 1st question online on R. Trying to creat a function that contains a loop. The function takes a vector that the user submits like in pollutantmean(4:6) and then it loads a bunch of csv files (in the directory mentioned) and binds them. What is strange (to me) is that if I assign the variable id and then run the loop without using a function, it works! When I put it inside a function so that the user can supply the id vector then it does nothing. Can someone help ? thank you!!!
pollutantmean<-function(id=1:332)
{
#read files
allfiles<-data.frame()
id<-str_pad(id,3,pad = "0")
direct<-"/Users/ped/Documents/LearningR/"
for (i in id) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
}
Your function is missing a return value. (#Roland)
pollutantmean<-function(id=1:332) {
#read files
allfiles<-data.frame()
id<-str_pad(id,3,pad = "0")
direct<-"/Users/ped/Documents/LearningR/"
for (i in id) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
return(allfiles)
}
Edit:
Your mistake was that you did not specify in your function what you want to get out from the function. In R, you create objects inside of function (you could imagine it as different environment) and then specify which object you want it to return.
With my comment about accepting my answer, I meant this: (...To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in...).
Consider even an lapply and do.call which would not need return being last line of function:
pollutantmean <- function(id=1:332) {
id <- str_pad(id,3,pad = "0")
direct_files <- paste0("/Users/ped/Documents/LearningR/", id, ".csv")
# READ FILES INTO LIST AND ROW BIND
allfiles <- do.call(rbind, lapply(direct_files, read.csv))
}
ok, I got it. I was expecting the files that are built to be actually created and show up in the environment of R. But for some reason they don't. But R still does all the calculations. Thanks lot for the replies!!!!
pollutantmean<-function(directory,pollutant,id)
{
#read files
allfiles<-data.frame()
id2<-str_pad(id,3,pad = "0")
direct<-paste("/Users/pedroalbuquerque/Documents/Learning R/",directory,sep="")
for (i in id2) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
#averaging polutants
mean(allfiles[,pollutant],na.rm = TRUE)
}
pollutantmean("specdata","nitrate",23:35)

Qt qsettings save value in quotes

I try to save some values in ini file, it's ok while i save only one value, like:
AAA=BBB
but i found a problem, when i try to save several values like this:
AAA=BBB;CCC
it's ok, if i want to save, but if i want to read this value, i will get only BBB. I found solution of this problem, if value was saved in quotes it will be read normally:
AAA="BBB;CCC"
but, i don't know how to save value in quotes. I use following code, to save values:
configs.setValue("Settings/name", value);
I tried to save with ASCII symbol and with "\"", but it's not work. Save code:
if (!Ui->commonIncludesLineEdit->text().isEmpty()) {
value = Ui->commonIncludesLineEdit->text();
configs.setValue("Settings/CommonIncludes", value);
} else {
if (configs.contains("Settings/CommonIncludes"))
configs.remove("Settings/CommonIncludes");
}
read code:
if (configs.contains("Settings/CommonIncludes"))
Ui->commonIncludesLineEdit->setText(configs.value(
"Settings/CommonIncludes",
"Open problems").toString());
ini file:
[Settings]
CommonIncludes="C:/dev/ObfOr/Workset_latest;C:/dev/ObfOr/Workset_latest/Demo"

Resources