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

Getting problems when trying to do a basic print of epidist. #301

Closed
2 tasks
jlessler opened this issue May 16, 2024 · 4 comments
Closed
2 tasks

Getting problems when trying to do a basic print of epidist. #301

jlessler opened this issue May 16, 2024 · 4 comments
Assignees
Milestone

Comments

@jlessler
Copy link

Please place an "x" in all the boxes that apply

  • [ X] I have the most recent version of this package and R
  • [X ] I have found a bug
  • I have a reproducible example
  • I want to request a new feature

Please include a brief description of the problem with a code example:

Get a error when I try to print epidist_db

library(epiparameter)

db <- epidist_db()

db

I get the following error:
ERROR while rich displaying an object: Error in vapply(x, function(y) y$citation$DOI, FUN.VALUE = character(1)): values must be length 1,
but FUN(X[[1]]) result is length 0

This is my R version:
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

Sys.info:
Sys.info()
sysname release
"Linux" "5.15.146.1-microsoft-standard-WSL2"
version nodename
"#1 SMP Thu Jan 11 04:09:03 UTC 2024" "JLesslerYoga"
machine login
"x86_64"


@jlessler
Copy link
Author

The problem appears to come from that on some operating systems (and or R versions) calling citation$DOI returns NULL since the doi value is actually stored as lower case citation$doi. This comes up on line 579 of epidist_db.R.

@jfunction
Copy link

Interesting issue - I'm interested to know why epiparameter:::multi_epidist[[1]]$citation$DOI works for me on Windows but not others (seemingly those using Linux). As referenced it's called here

x, function(y) y$citation$DOI,
and the underlying issue is in
doi = DOI
which assigns the name doi rather than DOI. Not sure what the fix should be though.

@joshwlambert
Copy link
Member

Thanks for posting this @jlessler. I've not encountered this before, and I'm not able to reproduce locally (on mac). It may be an issue with running R on linux, or potentially on the specific version of R/linux stated in your Sys.info(). However, I've now implemented a fix in PR #317.

In diagnosing the problem I've checked that the case insensitive matching is not standard behaviour for lists in R.

lst = list(first = 1, second = 2)
lst$fir
#> [1] 1
lst$first
#> [1] 1
lst$FIRST
#> NULL
lst$firsts
#> NULL

Created on 2024-05-24 with reprex v2.1.0

Reproducing the code example above I also get case insensitive matching when accessing elements of the citation <bibentry> object.

library(epiparameter)
db <- epidist_db()
#> Returning 122 results that match the criteria (99 are parameterised). 
#> Use subset to filter by entry variables or single_epidist to return a single entry. 
#> To retrieve the citation for each use the 'get_citation' function
db
#> List of <epidist> objects
#>   Number of entries in library: 122
#>   Number of studies in library: 47
#>   Number of diseases: 23
#>   Number of delay distributions: 112
#>   Number of offspring distributions: 10
db[[1]]$citation$DOI
#> [1] "10.1016/S1473-3099(09)70069-6"
db[[1]]$citation$Doi
#> [1] "10.1016/S1473-3099(09)70069-6"
db[[1]]$citation$doi
#> [1] "10.1016/S1473-3099(09)70069-6"

Created on 2024-05-24 with reprex v2.1.0

This seems to be due to the way R creates and accesses <bibentry> objects. Looking through the R source code, when a <bibentry> object is created the fields are converted to lowercase, independent of how they are given to the function:

https://github.com/wch/r-source/blob/8279cf2d59107a530b27e14d86266160ee851e8e/src/library/utils/R/citation.R#L586

This can be seen if I create a dummy <bibentry> and look at the element names:

bib = bibentry(
  bibtype = "article",
  author = "smith",
  year = 2000,
  title = "a title",
  journal = "a journal",
  DOI = "dfhbdshjdbs",
  PMID = 102897823
)
unclass(bib)
#> [[1]]
#> [[1]]$author
#> [1] "smith"
#> 
#> [[1]]$year
#> [1] "2000"
#> 
#> [[1]]$title
#> [1] "a title"
#> 
#> [[1]]$journal
#> [1] "a journal"
#> 
#> [[1]]$doi
#> [1] "dfhbdshjdbs"
#> 
#> [[1]]$pmid
#> [1] "102897823"
#> 
#> attr(,"bibtype")
#> [1] "Article"

Created on 2024-05-24 with reprex v2.1.0

The <bibentry> accessor ($.bibentry) then checks whether the names match for both the case sensitive and then for the lowercase element name. This explains the weird case insensitive behaviour shown above.

https://github.com/wch/r-source/blob/8279cf2d59107a530b27e14d86266160ee851e8e/src/library/utils/R/citation.R#L1044-L1059

@jfunction I hope this brings some light to the issue, see #317 for the fix, although I'm still not sure on what is the underlying difference causing this on some OSs/R versions.

Overall, this issue should now be resolved. If you continue to experience this issue or have a similar issue please feel free to reopen this issue or open a new one.

@jfunction
Copy link

Thanks to the awesome sleuthing above I was able to get to the bottom of this without too much additional effort and just wanted to report back.

The underlying issue was that the patch fixing the case insensitive values issue was made 2 years ago as part of R 4.2.0 but Ubuntu is still stuck on 4.1.2 see here. Note the NEWS says it's for version 4.2.0.

I also confirmed with utils:::`$.bibentry` which shows else lapply(unclass(x), `[[`, tolower(name)) in the newer version (which I have on my Windows machine) but not on the older version (which my Ubuntu machine installed). I double checked the version of utils was tied to the version of R with installed.packages()["utils",c("Package", "Version")].

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

No branches or pull requests

3 participants