kaitlyngaynor / gorongosa-mesocarnivores

2 stars 0 forks source link

Creating a function #107

Open kaitlyngaynor opened 3 years ago

kaitlyngaynor commented 3 years ago

https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/blob/5cdc28bb1b02b821b774527229423e7f37f89433/scripts/multi-season%20model/01-multi-season-data-prep.R#L77-L131

This isn't an issue that we need to solve, but something that I wanted to bring to your attention. I saw a comment in your code about how to run that chunk for multiple species, and I thought I would demonstrate how to wrap all of the code in a function that can be run.

Basically, anywhere that you had "Genet" (to subset the record table by species, and in the name of the output), I put in "species_name" which is the single argument that the function takes.

The line detectionHistory2016and2017 <- function(species_name) is telling R to make a new function called detectionHistory2016and2017 that takes a single argument, species_name. The brackets then contain the code that the function will execute. You can have functions in functions, as you see here.

Running the function will not create objects in the environment, but rather just in a temporary environment within the function. If you want it to create something (like a dataframe) you'll need to use return within the function, and then store the output of the function in a new object like you're used to (ex. y <- as.data.frame(x)) But in this case, the function will write three csv files.

And then you see that I run the function on "Genet" and "Civet" and it spits out data frames.

They are just a bunch of 0s and 1s with no row/column names, but maybe that's the next bridge we cross.... I admit that I didn't get further along in the code yet to see what we are doing with these csv files!

klg-2016 commented 3 years ago

Ah that's so cool! Thank you! I'll take a closer look through what you did tomorrow and see if I have any questions but that sounds wonderfully useful :)

On Thu, Dec 17, 2020, 8:46 PM Kaitlyn Gaynor notifications@github.com wrote:

https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/blob/5cdc28bb1b02b821b774527229423e7f37f89433/scripts/multi-season%20model/01-multi-season-data-prep.R#L77-L131

This isn't an issue that we need to solve, but something that I wanted to bring to your attention. I saw a comment in your code about how to run that chunk for multiple species, and I thought I would demonstrate how to wrap all of the code in a function that can be run.

Basically, anywhere that you had "Genet" (to subset the record table by species, and in the name of the output), I put in "species_name" which is the single argument that the function takes.

The line detectionHistory2016and2017 <- function(species_name) is telling R to make a new function called detectionHistory2016and2017 that takes a single argument, species_name. The brackets then contain the code that the function will execute. You can have functions in functions, as you see here.

Running the function will not create objects in the environment, but rather just in a temporary environment within the function. If you want it to create something (like a dataframe) you'll need to use return within the function, and then store the output of the function in a new object like you're used to (ex. y <- as.data.frame(x)) But in this case, the function will write three csv files.

And then you see that I run the function on "Genet" and "Civet" and it spits out data frames.

They are just a bunch of 0s and 1s with no row/column names, but maybe that's the next bridge we cross.... I admit that I didn't get further along in the code yet to see what we are doing with these csv files!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/issues/107, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP6KICRHPYX2SADUVPGZVS3SVKX7NANCNFSM4VAQLDUQ .

kaitlyngaynor commented 3 years ago

Haha I know, I felt like a magical R wizard when I first learned how to write functions.

In a course that I took, the instructor said any time you copy and paste a big chunk of code more than once, it's time to write a function!

On Thu, Dec 17, 2020 at 5:56 PM klg-2016 notifications@github.com wrote:

Ah that's so cool! Thank you! I'll take a closer look through what you did tomorrow and see if I have any questions but that sounds wonderfully useful :)

On Thu, Dec 17, 2020, 8:46 PM Kaitlyn Gaynor notifications@github.com wrote:

https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/blob/5cdc28bb1b02b821b774527229423e7f37f89433/scripts/multi-season%20model/01-multi-season-data-prep.R#L77-L131

This isn't an issue that we need to solve, but something that I wanted to bring to your attention. I saw a comment in your code about how to run that chunk for multiple species, and I thought I would demonstrate how to wrap all of the code in a function that can be run.

Basically, anywhere that you had "Genet" (to subset the record table by species, and in the name of the output), I put in "species_name" which is the single argument that the function takes.

The line detectionHistory2016and2017 <- function(species_name) is telling R to make a new function called detectionHistory2016and2017 that takes a single argument, species_name. The brackets then contain the code that the function will execute. You can have functions in functions, as you see here.

Running the function will not create objects in the environment, but rather just in a temporary environment within the function. If you want it to create something (like a dataframe) you'll need to use return within the function, and then store the output of the function in a new object like you're used to (ex. y <- as.data.frame(x)) But in this case, the function will write three csv files.

And then you see that I run the function on "Genet" and "Civet" and it spits out data frames.

They are just a bunch of 0s and 1s with no row/column names, but maybe that's the next bridge we cross.... I admit that I didn't get further along in the code yet to see what we are doing with these csv files!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/issues/107, or unsubscribe < https://github.com/notifications/unsubscribe-auth/AP6KICRHPYX2SADUVPGZVS3SVKX7NANCNFSM4VAQLDUQ

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/issues/107#issuecomment-747817381, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHA7WT4ULCNRCGQJQ5CTGD3SVKZDZANCNFSM4VAQLDUQ .

klg-2016 commented 3 years ago

I know you've mentioned it to me before and I'm excited to start working with them and stop copying so much!

On Thu, Dec 17, 2020 at 9:09 PM Kaitlyn Gaynor notifications@github.com wrote:

Haha I know, I felt like a magical R wizard when I first learned how to write functions.

In a course that I took, the instructor said any time you copy and paste a big chunk of code more than once, it's time to write a function!

On Thu, Dec 17, 2020 at 5:56 PM klg-2016 notifications@github.com wrote:

Ah that's so cool! Thank you! I'll take a closer look through what you did tomorrow and see if I have any questions but that sounds wonderfully useful :)

On Thu, Dec 17, 2020, 8:46 PM Kaitlyn Gaynor notifications@github.com wrote:

https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/blob/5cdc28bb1b02b821b774527229423e7f37f89433/scripts/multi-season%20model/01-multi-season-data-prep.R#L77-L131

This isn't an issue that we need to solve, but something that I wanted to bring to your attention. I saw a comment in your code about how to run that chunk for multiple species, and I thought I would demonstrate how to wrap all of the code in a function that can be run.

Basically, anywhere that you had "Genet" (to subset the record table by species, and in the name of the output), I put in "species_name" which is the single argument that the function takes.

The line detectionHistory2016and2017 <- function(species_name) is telling R to make a new function called detectionHistory2016and2017 that takes a single argument, species_name. The brackets then contain the code that the function will execute. You can have functions in functions, as you see here.

Running the function will not create objects in the environment, but rather just in a temporary environment within the function. If you want it to create something (like a dataframe) you'll need to use return within the function, and then store the output of the function in a new object like you're used to (ex. y <- as.data.frame(x)) But in this case, the function will write three csv files.

And then you see that I run the function on "Genet" and "Civet" and it spits out data frames.

They are just a bunch of 0s and 1s with no row/column names, but maybe that's the next bridge we cross.... I admit that I didn't get further along in the code yet to see what we are doing with these csv files!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/issues/107 , or unsubscribe <

https://github.com/notifications/unsubscribe-auth/AP6KICRHPYX2SADUVPGZVS3SVKX7NANCNFSM4VAQLDUQ

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/issues/107#issuecomment-747817381 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AHA7WT4ULCNRCGQJQ5CTGD3SVKZDZANCNFSM4VAQLDUQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kaitlyngaynor/gorongosa-mesocarnivores/issues/107#issuecomment-747821376, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP6KICU2CU3Z2R35E42JJJDSVK2U3ANCNFSM4VAQLDUQ .