R/find.packages_path.R

Defines functions find.packages_path

Documented in find.packages_path

#' Find the R Packages to Import a File Format
#'
#' This function takes a file path as input and searches the CRAN repository for R packages that can import the file format.
#'
#' @param file_path A character string specifying the file path of the file to be imported.
#'
#' @return A character string that lists the R packages that can be used to import the file format of the input file.
#'
#' @importFrom RCurl url.exists
#' @importFrom httr GET content
#' @import xml2
#'
#' @examples
#' # Search for packages that can import a CSV file
#' find.packages_path("sample.csv")
#'
#' # Search for packages that can import a JSON file
#' find.packages_path("sample.json")
#'
#' @export
find.packages_path <- function(file_path) {

  # Get the file extension
  file_extension <- sub(".*\\.", "", basename(file_path))

  # Check if the internet connection is available
  if (!RCurl::url.exists("https://cran.r-project.org")) {
    return("The internet connection is not available. Please check your internet connection.")
  }

  # Search the CRAN repository for packages that can import the file format
  search_url <- "https://cran.r-project.org/web/packages/available_packages_by_name.html"
  search_params <- list(pkg_name = file_extension)
  search_result <- httr::GET(search_url, query = search_params)

  # Check if there are any suggested packages
  if (grepl("No packages found matching", httr::content(search_result, encoding = "UTF-8"))) {
    return(paste("Sorry, no packages found that can be used to import the file format", file_extension))
  }

  # Extract the suggested packages from the search result
  packages <- gsub(".*<pre>", "", httr::content(search_result, encoding = "UTF-8"))
  packages <- gsub("</pre>.*", "", packages)
  packages <- strsplit(packages, "\n")[[1]]
  packages <- packages[grep(file_extension, packages, fixed = TRUE)]
  packages <- gsub("^ +| +$", "", packages)

  # input string
  string <- packages

  # extract the words between /packages/ and /index.html\
  result <- gsub('.*packages/(.*)/index.html.*', '\\1', string)

  # remove any other lines from result
  result <- result[grep("^[[:alnum:]]*$", result)]

  return(paste("The following R packages can be used to import the file format", file_extension, ": ",
               paste(result, collapse = ", ")))
}

Try the fixr package in your browser

Any scripts or data that you put into this service are public.

fixr documentation built on April 4, 2025, 12:30 a.m.