owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
23 October, 2018
Welcome!
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```r
library(tidyverse)
...
```
(*you can copy paste this example and add your code further down, but do not fill in your code in this section*)
Your snippets:
### how solve bug about png device
<https://stackoverflow.com/questions/24999983/r-unable-to-start-device-png-capabilities-has-true-for-png#25064603>
```
#try this
png("abc", type="cairo")
# somebody else wrote a more definitive solution
I had to add options(bitmapType='cairo') to my ~/.Rprofile
```
### (jeroen s) solution challenge 1
```
for (y in 2000:2003) {
year_plot <- groei_gent_df %>% filter(year == y) %>%
ggplot(aes(x = wijk, y = growth)) + geom_bar(stat = "identity") +
coord_flip()
ggsave(file.path("..", "images", paste0("district_evol_",y,".png")), year_plot)
}
```
### (floris) variant for paste0()
```
str_c("district_evol_", i,".png")
```
### joost (alle jaren in dataset)
```
for(y in unique(groei_gent_df$year)) {
year_plot <- groei_gent_df %>% filter(year == y) %>%
ggplot(aes(x = wijk, y = growth)) + geom_bar(stat = "identity") +
ggtitle(paste0("year = ", y)) +
coord_flip()
ggsave(file.path("images", paste0("district_evol_",y,".png")), year_plot)
}
```
### Thierry
```
for (this_year in unique(groei_gent_df$year)) {
year_plot <- groei_gent_df %>%
filter(year == this_year) %>%
ggplot(aes(x = wijk, y = growth)) +
geom_bar(stat = "identity") +
coord_flip()
filename <- sprintf("district_evol_%i.png", this_year)
ggsave(
file.path("..", "images", filename),
year_plot
)
}
```
```
plots <- groei_gent_df %>%
group_by(year) %>%
nest() %>%
mutate(
filename = sprintf("district_evol_%i.png", year),
path = file.path("..", "images", filename),
plot = map(
data,
function(data) {
ggplot(data, aes(x = wijk, y = growth)) +
geom_bar(stat = "identity") +
coord_flip()
}
)
)
walk2(plots$path, plots$plot, ggsave)
```
## Challenge 2
```
library(purrr)
library(rgbif)
animallist <- map(animals, name_backbone, rank = "GENUS")
animallist <- map_dfr(animals, name_backbone, rank = "GENUS")
```
```
matched_names <- vector(mode = "list", length = length(animals))
names(matched_names) <- animals
for (this_animal in animals) {
matched_names[[this_animal]] <- name_backbone(this_animal, rank = "GENUS")
}
```
```
matched_names <- lapply(animals, name_backbone, rank = "GENUS")
```
```
matched_names <- map(animals, name_backbone)
names(matched_names[[1]])
map_chr(matched_names, "family")
```
### floris
```
library(rgbif)
animals <- c("Branta", "Sus")
map_dfr(animals, .f = name_backbone, rank = "GENUS")
```
## Challenge 3
```
df_species <- read_csv("../data/20180222_species.csv")
df_species %>%
mutate(
scientific_name = str_c(genus, species, sep = " "),
gbif = map(scientific_name, name_backbone, rank = "SPECIES") %>%
map(as.tibble)
) %>%
unnest() -> result
glimpse(result)
```
### fv
Something came out of it, and it looked ok.
```
df_species <- read_csv("../data/20180222_species.csv")
df_species %>%
mutate(scientific_name = str_c(genus, species,
sep = " ")) %>%
.$scientific_name %>%
map_dfr(.f = name_backbone) %>%
bind_cols(df_species, .)
```
### Anneleen
```
df_species <- read_csv("./data/20180222_species.csv")
df_species <- df_species %>%
mutate(scientific_name = str_c(genus, species,
sep = " "))
df_speciesoutput <- map_dfr(df_species$scientific_name, name_backbone)
df_species <-cbind(df_species,df_speciesoutput)
```
### Simple solution (Dirk)
```
df_species <- read.csv("C:/Users/dirk_maes/Google Drive/CodingClub/20181023/data/20180222_species.csv", sep = ",")
df_species2 <- df_species %>%
mutate(scientific_name = str_c(genus, species, sep = " "))
.x <- df_species2$scientific_name
.f = name_backbone
map_dfr(.x, .f, rank = "SPECIES")
```
## Feedback
In case your answer is already enlisted by someone else, do NOT add the answer as a new item, but provide a `*` sign before the answer.
(*For more info, at the end of the hackmd is a 'tutorial' available*)
### Which topics would you like to explore in the next Coding Club sessions?
*(answer in between the triple backticks)*
```
* piping
***** advanced piping
* tidyr and such
* bookdown
**** advanced purrr
* spacetime data classes
*** more geospatial data handling
** memory-efficient programming (zodat je bv. lange for-loops kunt laten lopen zonder dat je RAM volloopt)
* advanced ggplot
* document functions and put them in a package
* programming with R: calling scripts, good habits to automate things in a repository (where to put things, how to separate R and Rmd files, maintaining the toolchain, dockerizing things?)
```
## When using R, I'm still struggling with...
*(answer in between the triple backticks)*
```
** data controle
*** Functions
*** lists
```
## Manual of the questionnaire
For each of the questions, add your answer to the list. In case your answer is already enlisted by someone else, do NOT add the answer as a new item, but provide a `*` sign before the answer.
As an **example**, suppose the question: *What's your favorite Simpson character*? and the already enlisted answers are:
```
* Bart
* Lisa
```
and your answer would be *Bart* as well; the result after your edit will be:
```
** Bart
* Lisa
```
if your answer would be *Homer*; the result after your edit will be:
```
* Bart
* Lisa
* Homer
```
Make sure your answer is provided in between the backtick sections!
When everyone is doing this according to this manual, we will get an on the fly bar/star-chart all together:
```
***** Bart
*********** Lisa
******* Homer
*** Marge
********* Flanders
* Apu
```
OK? Ready to go!