automating the files location in R
I am trying to get my head around an R code and to automate it so that I don't have to specify the file location every single time but only the location of the parent folder. Within the parent folder there will be n number of child folders housing the files of need.
Presently, what I am doing is to manually specify the locations of all the child folders.
The code goes like this:
First a function:
```{r include = T}
loadSC <- function(path){
sc <- read10XResults(path)
Genes <- as.character(rowData(sc)[startsWith(rowData(sc)$symbol, "mt-"),]$id)
isSpike(sc, "MT") <- rownames(sc) %in% Genes
sc <- calculateQCMetrics(sc,
feature_controls = list(
MT = isSpike(sc, "MT")
))
}
```
pointing to the files (This is the want, where I want it to automatically identify the number of child folders. Here, we have 5 child folders K1,K2,K3,K4,K5 and look for files within them)
```{r include = T}
sc_1 = loadSC("Sample/K1/outs/matrices/mm10/")
sc_2 = loadSC("Sample/K2/outs/matrices/mm10/")
sc_3 = loadSC("Sample/K3/outs/matrices/mm10/")
sc_4 = loadSC("Sample/K4/outs/matrices/mm10/")
sc_5 = loadSC("Sample/K5/outs/matrices/mm10/")
```
Also, i have similar chunk below this one that will also need to be looped in.
```{r include = T}
metadata(sc_1)["name"] <- "iM-1"
metadata(sc_2)["name"] <- "iM-2"
metadata(sc_3)["name"] <- "iM-3"
metadata(sc_4)["name"] <- "iM-4"
metadata(sc_5)["name"] <- "iM-5"
```
I am doing this in Rmarkdown.
A little guidance or a working example will really be helpful.
Thank you in advance
r r-markdown
add a comment |
I am trying to get my head around an R code and to automate it so that I don't have to specify the file location every single time but only the location of the parent folder. Within the parent folder there will be n number of child folders housing the files of need.
Presently, what I am doing is to manually specify the locations of all the child folders.
The code goes like this:
First a function:
```{r include = T}
loadSC <- function(path){
sc <- read10XResults(path)
Genes <- as.character(rowData(sc)[startsWith(rowData(sc)$symbol, "mt-"),]$id)
isSpike(sc, "MT") <- rownames(sc) %in% Genes
sc <- calculateQCMetrics(sc,
feature_controls = list(
MT = isSpike(sc, "MT")
))
}
```
pointing to the files (This is the want, where I want it to automatically identify the number of child folders. Here, we have 5 child folders K1,K2,K3,K4,K5 and look for files within them)
```{r include = T}
sc_1 = loadSC("Sample/K1/outs/matrices/mm10/")
sc_2 = loadSC("Sample/K2/outs/matrices/mm10/")
sc_3 = loadSC("Sample/K3/outs/matrices/mm10/")
sc_4 = loadSC("Sample/K4/outs/matrices/mm10/")
sc_5 = loadSC("Sample/K5/outs/matrices/mm10/")
```
Also, i have similar chunk below this one that will also need to be looped in.
```{r include = T}
metadata(sc_1)["name"] <- "iM-1"
metadata(sc_2)["name"] <- "iM-2"
metadata(sc_3)["name"] <- "iM-3"
metadata(sc_4)["name"] <- "iM-4"
metadata(sc_5)["name"] <- "iM-5"
```
I am doing this in Rmarkdown.
A little guidance or a working example will really be helpful.
Thank you in advance
r r-markdown
5
?list.files
/?dir
– hrbrmstr
Nov 16 '18 at 11:27
Do you mean to find all subfolders and files from a directory? If so, you can have a look at list.files
– CIAndrews
Nov 16 '18 at 11:27
2
There is also a?list.dirs()
. As for creating 5 different objects namedsc_n
you should consider creating just one object of class"list
and save the objects there. (This has been said over and over again.)
– Rui Barradas
Nov 16 '18 at 11:31
add a comment |
I am trying to get my head around an R code and to automate it so that I don't have to specify the file location every single time but only the location of the parent folder. Within the parent folder there will be n number of child folders housing the files of need.
Presently, what I am doing is to manually specify the locations of all the child folders.
The code goes like this:
First a function:
```{r include = T}
loadSC <- function(path){
sc <- read10XResults(path)
Genes <- as.character(rowData(sc)[startsWith(rowData(sc)$symbol, "mt-"),]$id)
isSpike(sc, "MT") <- rownames(sc) %in% Genes
sc <- calculateQCMetrics(sc,
feature_controls = list(
MT = isSpike(sc, "MT")
))
}
```
pointing to the files (This is the want, where I want it to automatically identify the number of child folders. Here, we have 5 child folders K1,K2,K3,K4,K5 and look for files within them)
```{r include = T}
sc_1 = loadSC("Sample/K1/outs/matrices/mm10/")
sc_2 = loadSC("Sample/K2/outs/matrices/mm10/")
sc_3 = loadSC("Sample/K3/outs/matrices/mm10/")
sc_4 = loadSC("Sample/K4/outs/matrices/mm10/")
sc_5 = loadSC("Sample/K5/outs/matrices/mm10/")
```
Also, i have similar chunk below this one that will also need to be looped in.
```{r include = T}
metadata(sc_1)["name"] <- "iM-1"
metadata(sc_2)["name"] <- "iM-2"
metadata(sc_3)["name"] <- "iM-3"
metadata(sc_4)["name"] <- "iM-4"
metadata(sc_5)["name"] <- "iM-5"
```
I am doing this in Rmarkdown.
A little guidance or a working example will really be helpful.
Thank you in advance
r r-markdown
I am trying to get my head around an R code and to automate it so that I don't have to specify the file location every single time but only the location of the parent folder. Within the parent folder there will be n number of child folders housing the files of need.
Presently, what I am doing is to manually specify the locations of all the child folders.
The code goes like this:
First a function:
```{r include = T}
loadSC <- function(path){
sc <- read10XResults(path)
Genes <- as.character(rowData(sc)[startsWith(rowData(sc)$symbol, "mt-"),]$id)
isSpike(sc, "MT") <- rownames(sc) %in% Genes
sc <- calculateQCMetrics(sc,
feature_controls = list(
MT = isSpike(sc, "MT")
))
}
```
pointing to the files (This is the want, where I want it to automatically identify the number of child folders. Here, we have 5 child folders K1,K2,K3,K4,K5 and look for files within them)
```{r include = T}
sc_1 = loadSC("Sample/K1/outs/matrices/mm10/")
sc_2 = loadSC("Sample/K2/outs/matrices/mm10/")
sc_3 = loadSC("Sample/K3/outs/matrices/mm10/")
sc_4 = loadSC("Sample/K4/outs/matrices/mm10/")
sc_5 = loadSC("Sample/K5/outs/matrices/mm10/")
```
Also, i have similar chunk below this one that will also need to be looped in.
```{r include = T}
metadata(sc_1)["name"] <- "iM-1"
metadata(sc_2)["name"] <- "iM-2"
metadata(sc_3)["name"] <- "iM-3"
metadata(sc_4)["name"] <- "iM-4"
metadata(sc_5)["name"] <- "iM-5"
```
I am doing this in Rmarkdown.
A little guidance or a working example will really be helpful.
Thank you in advance
r r-markdown
r r-markdown
asked Nov 16 '18 at 11:23
AngeloAngelo
1,69962345
1,69962345
5
?list.files
/?dir
– hrbrmstr
Nov 16 '18 at 11:27
Do you mean to find all subfolders and files from a directory? If so, you can have a look at list.files
– CIAndrews
Nov 16 '18 at 11:27
2
There is also a?list.dirs()
. As for creating 5 different objects namedsc_n
you should consider creating just one object of class"list
and save the objects there. (This has been said over and over again.)
– Rui Barradas
Nov 16 '18 at 11:31
add a comment |
5
?list.files
/?dir
– hrbrmstr
Nov 16 '18 at 11:27
Do you mean to find all subfolders and files from a directory? If so, you can have a look at list.files
– CIAndrews
Nov 16 '18 at 11:27
2
There is also a?list.dirs()
. As for creating 5 different objects namedsc_n
you should consider creating just one object of class"list
and save the objects there. (This has been said over and over again.)
– Rui Barradas
Nov 16 '18 at 11:31
5
5
?list.files
/ ?dir
– hrbrmstr
Nov 16 '18 at 11:27
?list.files
/ ?dir
– hrbrmstr
Nov 16 '18 at 11:27
Do you mean to find all subfolders and files from a directory? If so, you can have a look at list.files
– CIAndrews
Nov 16 '18 at 11:27
Do you mean to find all subfolders and files from a directory? If so, you can have a look at list.files
– CIAndrews
Nov 16 '18 at 11:27
2
2
There is also a
?list.dirs()
. As for creating 5 different objects named sc_n
you should consider creating just one object of class "list
and save the objects there. (This has been said over and over again.)– Rui Barradas
Nov 16 '18 at 11:31
There is also a
?list.dirs()
. As for creating 5 different objects named sc_n
you should consider creating just one object of class "list
and save the objects there. (This has been said over and over again.)– Rui Barradas
Nov 16 '18 at 11:31
add a comment |
1 Answer
1
active
oldest
votes
Imagine your data is stored like:
- Sample (parent folder)
- --> /K1 (subfolder)
- --> /K2
- --> /K3
- ...
An easy way to access the data is:
# This function gets all the folder paths inside "Sample"
paths <- list.dirs(path = "Sample", recursive = FALSE)
# To access the data, you should get used to lapply()
data <- lapply(paths, loadSC)
# To keep an overview where the data is from
names(data) <- paths
As a result you get a comprehensive named list with your desired data.
You might have to adapt your loadSC(), but in future you only have to change your parent folder to analyze another data set.
Cheers
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336885%2fautomating-the-files-location-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Imagine your data is stored like:
- Sample (parent folder)
- --> /K1 (subfolder)
- --> /K2
- --> /K3
- ...
An easy way to access the data is:
# This function gets all the folder paths inside "Sample"
paths <- list.dirs(path = "Sample", recursive = FALSE)
# To access the data, you should get used to lapply()
data <- lapply(paths, loadSC)
# To keep an overview where the data is from
names(data) <- paths
As a result you get a comprehensive named list with your desired data.
You might have to adapt your loadSC(), but in future you only have to change your parent folder to analyze another data set.
Cheers
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
add a comment |
Imagine your data is stored like:
- Sample (parent folder)
- --> /K1 (subfolder)
- --> /K2
- --> /K3
- ...
An easy way to access the data is:
# This function gets all the folder paths inside "Sample"
paths <- list.dirs(path = "Sample", recursive = FALSE)
# To access the data, you should get used to lapply()
data <- lapply(paths, loadSC)
# To keep an overview where the data is from
names(data) <- paths
As a result you get a comprehensive named list with your desired data.
You might have to adapt your loadSC(), but in future you only have to change your parent folder to analyze another data set.
Cheers
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
add a comment |
Imagine your data is stored like:
- Sample (parent folder)
- --> /K1 (subfolder)
- --> /K2
- --> /K3
- ...
An easy way to access the data is:
# This function gets all the folder paths inside "Sample"
paths <- list.dirs(path = "Sample", recursive = FALSE)
# To access the data, you should get used to lapply()
data <- lapply(paths, loadSC)
# To keep an overview where the data is from
names(data) <- paths
As a result you get a comprehensive named list with your desired data.
You might have to adapt your loadSC(), but in future you only have to change your parent folder to analyze another data set.
Cheers
Imagine your data is stored like:
- Sample (parent folder)
- --> /K1 (subfolder)
- --> /K2
- --> /K3
- ...
An easy way to access the data is:
# This function gets all the folder paths inside "Sample"
paths <- list.dirs(path = "Sample", recursive = FALSE)
# To access the data, you should get used to lapply()
data <- lapply(paths, loadSC)
# To keep an overview where the data is from
names(data) <- paths
As a result you get a comprehensive named list with your desired data.
You might have to adapt your loadSC(), but in future you only have to change your parent folder to analyze another data set.
Cheers
answered Nov 16 '18 at 13:09
purakypuraky
113
113
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
add a comment |
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Hi, Great it works like a charm. As you explain it so nicely. I have two additional questions (i) What if I have many sub folders within the child directory and I want the path to the last directory, (ii) How can I do the same with my metadata chunk of code with assigning of the variables. Many thanks in advance
– Angelo
Nov 16 '18 at 13:30
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
Yes, I thought about it when I saw your code: (i) Since you now receive the folder name from lapply(), u might use instead of read10XResults(path) //// read10XResults(paste(path,"/outs/matrices/mm10/ ",sep=""), this should do what you want. (ii) I don't know your metadata(), but why would you like to name the results again? just use "sample$", while hitting the $ the environment will propose you the subfolders by name.
– puraky
Nov 16 '18 at 13:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336885%2fautomating-the-files-location-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
?list.files
/?dir
– hrbrmstr
Nov 16 '18 at 11:27
Do you mean to find all subfolders and files from a directory? If so, you can have a look at list.files
– CIAndrews
Nov 16 '18 at 11:27
2
There is also a
?list.dirs()
. As for creating 5 different objects namedsc_n
you should consider creating just one object of class"list
and save the objects there. (This has been said over and over again.)– Rui Barradas
Nov 16 '18 at 11:31