Zoological-Society-of-London / rlpi

The rlpi package can calculate indices using the Living Planet Index methodology, as presented in McRae et al. (in review) *The diversity-weighted Living Planet Index: controlling for taxonomic bias in a global biodiversity indicator*.
http://www.livingplanetindex.org
31 stars 16 forks source link

LPIMain error: [...]_splambnda.csv: no such file or directory #15

Open jimjam-slam opened 1 month ago

jimjam-slam commented 1 month ago

Hi there,

I'm trying to calculate LPI using the population CSV file provided from the LPI website.

I've created an infile and a population file from that CSV:

library(tidyverse)
library(here)
library(rlpi)

# load the data
all_data <-  read_csv(here("data", "raw", "LPD2022_public.csv"), na = "NULL")

# create population file from all species in dataset
all_data |>
  select(Binomial, ID, matches("^\\d{4}$")) |>
  pivot_longer(cols = -c(Binomial, ID), names_to = "year",
    values_to = "popvalue") |>
  filter(!is.na(popvalue)) |>
  write_tsv(here("data", "raw", "pop-allspecies-global.tsv"))

# create infile
paste(
  '"FileName"   "Group" "Weighting"',
  '"data/raw/pop-allspecies-global.tsv" 1   1',
  sep = "\n") |>
  writeLines(here("data", "raw", "infile-allspecies-global.tsv"))

But when I try to run LPIMain, I get an error:

dir.create(here("lpi_temp"), showWarnings = FALSE)
dir.create(here("data", "raw", "lpi_temp"), showWarnings = FALSE)

result <- LPIMain(here("data", "raw", "infile-allspecies-global.tsv"))

Number of groups:  1 
processing file: data/raw/pop-allspecies-global.tsv
Calculating LPI for Species
Number of species: 4922 (in 32664 populations)
  |******************************************************************| 100%

# Saving species lambda to file: ./lpi_temp/781b0610d7e95c2caaf26bbdd9c8230b_splambda.csv
# Saving species lambda to file: ./data/raw/pop-allspecies-global.tsv
# Calculating DTemp
# Saving DTemp to file:  ./lpi_temp/781b0610d7e95c2caaf26bbdd9c8230b_dtemp.csv 
# Saving DTemp to file:  ./data/raw/pop-allspecies-global.tsv 
# Error in file(file, "rt") : cannot open the connection
# In addition: Warning message:
# In file(file, "rt") :
#   cannot open file './lpi_temp/bec35d80d781265128fcf2cd203cf3f1_splambda.csv': No such file or directory

My infile and popfile are in /data/raw. I created both /lpi_temp and /data/raw/lpi_temp because I wasn't sure how the package handles the relative links. But I'm not sure if I'm supposed to create bec35d80d781265128fcf2cd203cf3f1_splambda.csv before running LPIMain or if it's an intermediate file that the function is supposed to generate on the way through (I can see that another file ending in _splambda.csv is generated, but with a different ID prefix).

Session info ``` R version 4.3.0 (2023-04-21) Platform: aarch64-unknown-linux-gnu (64-bit) Running under: Ubuntu 22.04.2 LTS Matrix products: default BLAS: /usr/lib/aarch64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK: /usr/lib/aarch64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0 locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C time zone: Etc/UTC tzcode source: system (glibc) attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] here_1.0.1 rlpi_0.1.0 lubridate_1.9.3 forcats_1.0.0 [5] stringr_1.5.0 dplyr_1.1.4 purrr_1.0.1 readr_2.1.5 [9] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0 loaded via a namespace (and not attached): [1] Matrix_1.5-4 bit_4.0.5 gtable_0.3.5 jsonlite_1.8.5 [5] crayon_1.5.2 compiler_4.3.0 tidyselect_1.2.1 parallel_4.3.0 [9] splines_4.3.0 scales_1.3.0 lattice_0.21-8 R6_2.5.1 [13] generics_0.1.3 iterators_1.0.14 munsell_0.5.1 rprojroot_2.0.3 [17] pillar_1.9.0 tzdb_0.4.0 rlang_1.1.1 utf8_1.2.3 [21] stringi_1.7.12 doParallel_1.0.17 bit64_4.0.5 timechange_0.3.0 [25] cli_3.6.1 mgcv_1.8-42 withr_2.5.0 magrittr_2.0.3 [29] foreach_1.5.2 grid_4.3.0 vroom_1.6.5 hms_1.1.3 [33] nlme_3.1-162 lifecycle_1.0.3 vctrs_0.6.5 glue_1.6.2 [37] codetools_0.2-19 fansi_1.0.4 colorspace_2.1-0 tools_4.3.0 [41] pkgconfig_2.0.3 ```
jimjam-slam commented 1 month ago

I've also tried following the Creating infiles guide, but that seems to fail faster than manually generating them:

library(tidyverse)
library(rlpi)
library(here)

here("data", "raw", "LPD2022_public.csv") |>
  read_csv(na = "NULL") ->
all_data

# this works
all_infile_name <- create_infile(all_data,
  index_vector = rep(TRUE, nrow(all_data)),
  name = "all_species")

result <- LPIMain(all_infile_name)
# Number of groups:  1 
# processing file: all_species_pops.txt
# Calculating LPI for Species
# Number of species: 3626 (in 21260 populations)
# Error in { : task 1 failed - "invalid 'ncol' value (< 0)"

Here's what the infile generated by the package looks like:

"FileName"  "Group" "Weighting"
"all_species_pops.txt"  1   1

Here's what the infile generated by my code looks like:

"FileName"  "Group" "Weighting"
"data/raw/pop-allspecies-global.tsv"    1   1