Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pkg data updates not reflected in new R sessions #11

Closed
ChrisMuir opened this issue Mar 13, 2018 · 1 comment
Closed

Pkg data updates not reflected in new R sessions #11

ChrisMuir opened this issue Mar 13, 2018 · 1 comment
Labels

Comments

@ChrisMuir
Copy link
Member

ChrisMuir commented Mar 13, 2018

The core functions of this package require access to two reference data files - one is a df of countries and country codes, the other is a df of commodities and commodity codes. The package ships with both as package data rda files in inst/extdata, but there's also a function for updating either/both of these data sets, ct_update_databases() (the API issues data updates periodically, and there are a number of different commodity data sets that the user can choose to use, and access to those is made available via the update function). The purpose of the update function is to save data updates/changes to comtradr/extdata within the local R library directory (over-writing the rda file(s) in process).

Something is going wrong though, currently the update function will indeed save updates down to file, however if R is restarted, the pkg data files will revert back to their state prior to updating.

Here's an example:

library(comtradr)

# Check which commodity DB exists currently on file.
ct_commodity_db_type()
#> [1] "HS"

# This should download a different commodity DB file and save it to file.
ct_update_databases(commodity_type = "HS1992")
#> Updates found. The following datasets have been downloaded: commodities DB

# Check that download was successful.
ct_commodity_db_type()
#> "HS1992"

Here I restart R.

library(comtradr)

# Check which commodity DB exists currently on file.
ct_commodity_db_type()
#> [1] "HS"

The commodities data set that gets loaded upon package load is the data set that existed on file prior to our updating, which is not what we want.

The weird part is I can navigate to my local R packages directory, and I can see that file commodity_table.rda was indeed last modified during the update function. I can also manually read it in and check its type:

file_test <- system.file("extdata", "commodity_table.rda", package = "comtradr")

# This loads df 'commodity_df' into the global env.
load(file_test)

attributes(commodity_df)$type
#> [1] "HS1992"

cat(file_test)
#> [1] "path/to/local/R/libraries/comtradr/extdata/commodity_table.rda"

I don't have the comtradr package installed in any old R installation directories, I removed the dev git repo (for the purpose of this test), I've searched through temp folders and don't see anything suspicious. I have no idea where the "old" data is even being loaded from.

edit to add: I've tested this on both PC and Mac.

@ChrisMuir
Copy link
Member Author

Fix for this was simply to not load the pkg data within the zzz.R file. Each data file is now sourced via functions get_commodity_db() and get_country_db() the first time either is needed in the current R session. Both functions load package data from file into a package environment, and the data is made available to any pkg functions that need them.

I still have no idea how the "old" data was being loaded in the previous set up. But for now, this issue has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant