prioritizr / wdpar

Interface to the World Database on Protected Areas
https://prioritizr.github.io/wdpar
GNU General Public License v3.0
37 stars 5 forks source link

wdpa_fetch #44

Closed gabyavm97 closed 2 years ago

gabyavm97 commented 2 years ago

Hi! I'm having problems to run this function, this is my code:

library(wdpar) library(prepr) library(dplyr) library(tibble)

define country names to download

country_codes <- c("CRI", "NIC", "HND", "SLV","GTM", "BLZ", "MEX")

download data for each country

mult_data <- lapply(country_codes, wdpa_fetch, wait = TRUE)

Even if I close the session and opened it again it keeps showing this error:

mult_data <- lapply(country_codes, wdpa_fetch, wait = TRUE) [100%] Downloaded 15368060 bytes...

Error in wdman::phantomjs(verbose = FALSE) : PhantomJS signals port = 4567 is already in use.

I don't know how to solve it, thanks for reading.

jeffreyhanson commented 2 years ago

Hi @gabyavm97,

This issue can happen if you start downloading some data using wdpa_fetch() and then stop/restart R before it has finished. To my knowledge, the easiest way to fix this is to restart the computer. Another strategy is that you can look for a PhantomJS process running on your system (e.g. using the task manager, htop, etc) and then kill that process. The developmental version of the package on GitHub has some features that try to prevent this issue, so another option could be to try installing the GitHub version of wdpar (see README for installation instructions). Does that help? Let me know if none of those suggestions fix the issue?

gabyavm97 commented 2 years ago

Hi Jeff!  I re-installed the package from GitHub, and also actualize another package "rlag" and now it's solved. Thank you so much for your help. If it's not too much trouble, I would like to export the data obtained as a txt file but the output doesn't look like the table I can view (with the function view() in R), the problem is in the data of the geometry column wich appears as new rows instead of a sole column but I don't know why, and if you know some way I could export the data to this format or other (xls, xlsx, csv) I would be really help full, sorry if this is too basic but I'm starting to know and use R.  Regards,  

jeffreyhanson commented 2 years ago

Hi,

No worries! Great to hear it's working now! Yeah, to export the dataset as a CSV, you could do something like this:

# load packages
library(wdpar)

# download protected area data for Malta
mlt_raw_pa_data <- wdpa_fetch("Malta", wait = TRUE,
                              download_dir = rappdirs::user_data_dir("wdpar"))

# clean Malta data
mlt_pa_data <- wdpa_clean(mlt_raw_pa_data)

# convert data from sf format to tibble format
mlt_pa_tbl <- st_drop_geometry(mlt_pa_data)

# save as CSV file
write.table(mlt_pa_tbl, "mlt-pa.csv", sep = ",", row.names = FALSE)

Does that help? Let me know if you have any further questions?

jeffreyhanson commented 2 years ago

@gabyavm97, I just wanted to follow up and ask if that fixed the issue?

gabyavm97 commented 2 years ago

Hi Jeffrey! sorry, I forgot to respond, but yeah, it worked really well, I just made one change in the name for the output file, I changed the last "-" for a " . " before de "csv", thank you so much for all your help.

jeffreyhanson commented 2 years ago

No worries! Glad to hear it's sorted now. Thanks for mentioning that change - I'll update the code so its correct for future reference.