Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Description:
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Depends:
R (>= 4.1.0)
Imports:
Expand All @@ -27,7 +27,8 @@ Imports:
stringr,
tibble,
glue,
ghql
ghql,
rvest
Suggests:
gargle,
gitcreds,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(cfde_opportunity_numbers)
export(epmc_search)
export(ga_dataframe)
export(ga_meta_simple)
Expand Down Expand Up @@ -46,6 +47,10 @@ importFrom(rlang,.data)
importFrom(rlang,abort)
importFrom(rlang,format_error_bullets)
importFrom(rlang,inform)
importFrom(rvest,html_attr)
importFrom(rvest,html_nodes)
importFrom(rvest,read_html)
importFrom(stats,na.omit)
importFrom(stats,setNames)
importFrom(stringr,regex)
importFrom(stringr,str_detect)
Expand Down
41 changes: 41 additions & 0 deletions R/cfde_opportunity_numbers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' All CFDE Funding Opportunity Numbers
#'
#' This function retrieves all CFDE funding
#' opportunity numbers from the CFDE funding
#' website, \url{https://commonfund.nih.gov/dataecosystem/FundingOpportunities}.
#'
#' Note that this function is specific to the CFDE
#' program and is not a general-purpose web scraping
#' function.
#'
#' @importFrom rvest read_html html_nodes html_attr
#' @importFrom stats na.omit
#'
#'
#' @param url The URL of the CFDE funding webpage
#' @return a character vector of funding opportunity numbers.
#'
#' @examples
#'
#' \dontrun{
#' browseURL("https://commonfund.nih.gov/dataecosystem/FundingOpportunities")
#' }
#'
#' cfde_opportunity_numbers()
#'
#' @export
cfde_opportunity_numbers <- function(
url = "https://commonfund.nih.gov/dataecosystem/FundingOpportunities"
) {

hrefs <- rvest::read_html(url) |>
rvest::html_nodes("a") |>
rvest::html_attr("href")
hrefs_filtered <- grep('NOT|RFA|OTA', hrefs, value = TRUE)

pattern <- "(RFA|OTA|NOT)-[A-Z]{2}-\\d{2}-\\d{3}"

matches <- regmatches(hrefs_filtered, regexpr(pattern, hrefs_filtered, perl=TRUE))
matches <- na.omit(matches)
matches[nzchar(matches)]
}
35 changes: 35 additions & 0 deletions man/cfde_opportunity_numbers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test_cfde_opportunity_numbers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("cfde_opportunity_numbers returns a character vector", {
# Test with a known CFDE opportunity number
result <- cfde_opportunity_numbers()
expect_type(result, "character")
})

test_that("cfde_opportunity_numbers returns expected pattern", {
# Test that the returned opportunity numbers match the expected pattern
result <- cfde_opportunity_numbers()
pattern <- "(RFA|OTA|NOT)-[A-Z]{2}-\\d{2}-\\d{3}"
expect_true(all(grepl(pattern, result)))
})