Upload files in a folder without referencing name with R - r

I do not know how to create a reproducible example of this issue since it would require access to a folder on my computer(if anyone has a suggestion I am fine to follow up). However, what I would like to do is upload a file in a folder without actually referencing the name but only referencing the order it appears in the file, and I would like to do it in R if possible(although python would be fine as well). I want to do this because the folder has 40 or so files and I need to format them all the same, but they all have different names.
In other words I want a code that would look something like this:
setwd(folder)
for(i in 1:number of files in folder){
upload file i
process file i
rbind(master file,file i)
}
Obviously this is not an actual code, but merely a framework, so I did not put it in the code framework on the site. The line I do not know how to do is the first line in the loop(download file i). Is it possible to do this, or do I need to download every file individually with their specified name?

I think you're looking for list.fles()
ff <- list.files()
for(i in seq_along(ff)){
print(ff[i])
read.csv(ff[i], ...) # etc
...
}

Related

"../" relative path directory not working R

I often have to work with shared data/code from Dropbox, and all of the files are usually loaded in as df <- import("../data/branch/file_name.csv")
However, this never loads on my computer, so I always have to type in my full directory as df <- import("C:/Users/Name/Dropbox/Folder1/Folder2/data/branch/file_name.csv")
Which makes it difficult for others to reproduce the code. Is there any way to fix this so that R can read the "../" part properly? I always get this error: Error: path does not exist: ../data/branch/file_name.csv
(I used the import function as an example but this problem occurs with other packages or loading functions as well)
I've also tried using "~/" but the problem persists. The shared code I work with always uses "../" though so I'd prefer a solution that gets that to work instead so that I don't have to change the original script each time I need to use it.
Thank you very much.
Edit: Despite the conversation below, I am still having trouble with this. I tried setting the working directory and then using "../" and it sometimes works for read_xls() (it was working a few hours ago, not anymore, depsite doing the exact same thing and setting the same working directory) but not for rio::import() or other reading functions. I've even tried setting my working directory to the exact same folder and using "./" but no luck. Errors now say "Cannot open the connection" or "No such file"
You can try setting your working directory(wd) to the current folder first, then use read.csv to import the data.
To set the working directory, first obtain the current directory using rstudioapi::getActiveDocumentContext()$path then use setwd() to set the wd to this path.
Then use whatever function to import your data.
For example, let's say your R code is saved in the "branch" folder, then the following code should work just fine:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
mydata <- read.csv("./file_name.csv")
head(mydata)
Otherwise, if your R code was saved in a subfolder under Folder2 called code such that your R script's directory is: "C:/Users/Name/Dropbox/Folder1/Folder2/code/mycode.R", then the following should work:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
mydata <- read.csv("../data/branch/file_name.csv")
head(mydata)
It looks like the person you are collaborating with has their working directory set to a separate folder within Folder2 (perhaps a folder with all the .R code?). The .. means that the relative path is referencing the next folder up from the working directory (the next folder up is presumably Folder2).
To solve your problem, every time you start working you need to set your working directory to the same folder that your collaborator is using for their working directory. For example, this may be something like setwd("C:/Users/Name/Dropbox/Folder1/Folder2/RCode"). Unfortunately you will have to do that every time you open up the script, but if you do it at the very beginning, then the rest of the code should work.
Working directories can be hard to manage, especially when collaborating. A better solution to the working directory problem is to create a project in R Studio and collaborate on that project using GitHub. If you don't have experience with GitHub, these videos are an approachable resource for helping you get started.

R Can i store a location in something so i can reference it multiple times without having to retype it or if it changes?

I have five files that are all part of the same thing and intermingled in them are occasional exports of csv files. Currently, i have the full path written out for each of them, but i had to change a folder and then go through all of the files and find every reference and change them too.
Is there a way in R to store text, like "C:\R\Folder\New" so that then i can just reference an object or something and then i only have to change the path once?
In SAS i'd use a %let statement.
Maybe something like??
path <- "C:\R\Folder\New"
And then:
write.table(grouped_ageck, file="??PATH??\\qc\\ageck.csv", sep=",", row.names=F)

R: importing data impossible (windows)

I used to work with R on my mac and never had any problems.
Now I would like to use it on my work computer (windows). The problem is I can't import any files to start working with them. I tried several options:
mydata<-read.table("c:/temp/myfile.csv",header=TRUE)
mydata<-read.csv("myfile.csv",header=TRUE)
mydata<-read.table("c:/myfile.csv",header=TRUE)
mydata<-read.table("Desktop/myfile.csv",header=TRUE)
I also tried to change / into \ in all variants above.
Nothing seems to work. R displays the command in red, sometimes with a comment "connection can't be opened" or "no such file or directory" (my translation from German).
I tried copying the file I want to open to a different location (desktop, c:, temp), but alas, nothing helps.
Do you have any ideas why I have this problem and how I can solve it? Thanks in advance.
There is a safer way to work with paths; just using file.path().
So, if you're trying to get a file in C:/temp/turtles.csv, then you'd use:
targetFile <- file.path('C:/', 'temp', 'turtles.csv')
read.csv( targetFile, header=TRUE )
Minor point since it showed up on Twitter; DO NOT USE PATHS THAT EXIST ONLY IN YOUR ENVIRONMENT.
Try to keep the data in a path either in or directly under where the script is.
You have three ways to do this with read.csv() function
To avoid inserting actual path you can do it simply nesting function
read.csv(file.choose(),header=TRUE)
it will open pop up for selecting your file just select file from directory
where you have saved it.
Now if you have to insert a path then just get actual location of your file
by
read.csv("C:\path\to\your\file\filename.csv",header=TRUE)
for Example
read.csv("C:\Users\Amway\Desktop\resources.csv",header=TRUE)
Best way is to have your own work space directory
so create a directory by your preferred name and just set that directory as a
R session work space by
setwd("C:\path\to\your\workspace directory\")
check your current directory by
getwd()
now if you want to read a file into R session just copy your file to work
space and just write
read.csv("resources.csv",header=TRUE)
So, it should be like this.
setwd("c:/mydir") # note / instead of \ in windows
Also.
MyData <- read.csv(file="c:/mydir/TheDataIWantToReadIn.csv", header=TRUE, sep=",")
Windows uses the other backslash.
https://www.howtogeek.com/181774/why-windows-uses-backslashes-and-everything-else-uses-forward-slashes/

How to converge multiple R files into one single file

Situation
I wrote an R program which I split up into multiple R-files for the sake of keeping a good code structure.
There is a Main.R file which references all the other R-files with the 'source()' command, like this:
source(paste(getwd(), dirname1, 'otherfile1.R', sep="/"))
source(paste(getwd(), dirname3, 'otherfile2.R', sep="/"))
...
As you can see, the working directory needs to be set correctly in advance, otherwise, this could go wrong.
Now, if I want to share this R program with someone else, I have to pass all the R files and folders in relative order of each other for things to work. Hence my next question.
Question
Is there a way to replace all the 'source' commands with the actual R script code which it refers to? That way, I have a SINGLE R script file, which I can simply pass along without having to worry about setting the working directory.
I'm not looking for a solution which is an 'R package' (which by the way is one single directory, so I would lose my own directory structure). I simply wondering if there is an easy way to combine these self-referencing R files into one single file.
Thanks,
Ok I think you could use something like scaning all the files and then writting them again in the same new one. This can be done using readLines and sink:
sink("mynewRfile.R")
for(i in Nfiles){
current_file = readLines(filedir[i])
cat("\n\n#### Current file:",filedir[i],"\n\n")
cat(current_file, sep ="\n")
}
sink()
Here I have supposed all your file directories are in a vector filedir with length Nfiles, I guess you can adapt that

Where to store .xls file for xlsReadWrite in R

I am relatively new to R and am having some trouble with how to access my data. I have my test.xls file created in my MYDocuments. How to I access it from R
library(xlsReadWrite)
DF1 <- read.xls("test.xls") # read 1st sheet
Set the working directory with:
setwd("C:/Documents and Settings/yourname/My Documents")
This link may be useful as a method of making working folders per project and then placing all relevant info in that folder. It's a nice tutorial for making project files that contain everything you need. This is one approach.
http://www.dangoldstein.com/flash/Rtutorial2/Rtutorial2.html
The setwd() is another approach. I use a combination of the two in my work.

Resources