Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 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 @@ -35,6 +36,9 @@ 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(stringr,regex)
importFrom(stringr,str_detect)
importFrom(tibble,tibble)
40 changes: 40 additions & 0 deletions R/cfde_opportunity_numbers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' 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
#'
#'
#' @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)))
})