Package 'RKaggle'

Title: 'Kaggle' Dataset Downloader 'API'
Description: Easily download datasets and models from Kaggle <https://www.kaggle.com/> directly into your R environment using 'RKaggle'. Streamline your data analysis workflows by importing datasets and models effortlessly to focus on insights rather than manual data handling. Perfect for data enthusiasts and professionals looking to integrate Kaggle datasets and pre-trained models into their R projects with minimal hassle.
Authors: Benjamin Smith [aut, cre] (ORCID: <https://orcid.org/0009-0007-2206-0177>)
Maintainer: Benjamin Smith <[email protected]>
License: MIT + file LICENSE
Version: 0.1.3
Built: 2026-11-28 14:43:03 UTC
Source: https://github.com/benyamindsmith/rkaggle

Help Index


Download and Read a Dataset from Kaggle

Description

This function retrieves a dataset from Kaggle by downloading its metadata and associated ZIP file and then reads all supported files contained in its archive. Each supported file is loaded into appropriate function (see details for more information about this). The function returns a single data frame if there is only one file detected and an unnamed list of data frames otherwise. This function is only capable of pulling data from Kaggle Datasets and not competitions.

Usage

get_dataset(dataset)

Arguments

dataset

A character string specifying the dataset identifier on Kaggle. It should follow the format "username/dataset-name".

Details

The function constructs the metadata URL based on the provided dataset identifier, then sends a GET request using the httr package. If the request is successful, the returned JSON metadata is parsed. The function searches the metadata for a file with an encoding format of "application/zip", then downloads that ZIP file using a temporary file (managed by the withr package). After unzipping the file into a temporary directory, the function locates all files with extensions corresponding to popular dataset formats (csv, tsv, xlsx, json, rds, parquet, ods, shp, geojson and feather). Each file is then read using the appropriate function:

  • readr::read_csv for CSV files.

  • readr::read_tsv for TSV files.

  • readxl::read_excel for xlsx files.

  • jsonlite::fromJSON for JSON files.

  • readRDS for RDS files.

  • arrow::read_parquet for Parquet files.

  • readODS::read_ods for ODS files

  • sf::read_sf for SHP and GEOJSON files.

  • arrow::read_feather for Feather files.

The function stops with an error if any of the following occur:

  • The HTTP request fails.

  • No ZIP file URL is found in the metadata.

  • No supported data files are found in the unzipped contents.

Value

An named list of dataframes corresponding to the files that were able to be read by get_data(). If only one file is able to be read, a individual dataframe is returned.

Examples

# Download and read the "canadian-prime-ministers" dataset from Kaggle
  canadian_prime_ministers <- get_dataset("benjaminsmith/canadian-prime-ministers")
  canadian_prime_ministers
  
  # csv 
  canadian_prime_ministers <- get_dataset("benjaminsmith/canadian-prime-ministers")
  # tsv 
  arabic_twitter <- get_dataset("mksaad/arabic-sentiment-twitter-corpus")
  # xlsx
  hr_data <- get_dataset("kmldas/hr-employee-data-descriptive-analytics")
  # json
  iris_json <- get_dataset("rtatman/iris-dataset-json-version")
  # rds
  br_pop_2019<-get_dataset("ianfukushima/br-pop-2019")
  # parquet
  iris_datasets<-get_dataset("gpreda/iris-dataset")
  #ods
  new_houses <- get_dataset("nm8883/new-houses-built-each-year-in-england")
  #shp
  india_states <- get_dataset("dhruvanurag20/final-shp")
  #geojson
  montreal <- get_dataset("rinichristy/montreal-geojson")
  #feather
  ncaa <- get_dataset("corochann/ncaa-march-madness-2020-womens")

List a Kaggle model's variations

Description

Lists every framework/variation of a public Kaggle model, to help find the handle to pass to [load_model()].

Usage

list_model_variations(model)

Arguments

model

'"owner/model"' or a 'https://www.kaggle.com/models/owner/model' link.

Value

A data frame with columns 'framework', 'variation', 'version' (latest version number) and 'handle'.

See Also

[load_model()]

Examples

## Not run: 
kaggle_variations("qwen-lm/qwen2.5")

## End(Not run)

Download a public model and return a prediction function

Description

Downloads a model from Kaggle, TF Hub or any direct URL, caches it on disk, detects its format and returns an R function that runs it.

Usage

load_model(
  url,
  model_type = c("auto", "keras", "torch_script", "xgboost", "gguf"),
  cache_dir = tools::R_user_dir(.pkg_name(), which = "cache")
)

Arguments

url

Where the model lives. Any of: * a Kaggle handle: '"owner/model/framework/variation"' (latest version) or '"owner/model/framework/variation/version"'; * a Kaggle web link, e.g. 'https://www.kaggle.com/models/keras/densenet/keras/densenet_121_imagenet/3'; * a Kaggle API link, e.g. 'https://www.kaggle.com/api/v1/models/.../3/download'; * a TF Hub link, e.g. 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2'; * any direct URL to an archive ('.tar.gz', '.tgz', '.tar', '.zip') or to a single model file.

model_type

One of '"auto"' (detect from the downloaded files), '"keras"', '"torch_script"', '"xgboost"' or '"gguf"'.

cache_dir

Directory where downloads are unpacked and reused on later calls. Defaults to this package's user cache directory (see [tools::R_user_dir()]). Use 'NULL' to download into a fresh temporary directory every time.

Details

No Kaggle account or token is needed: public Kaggle models can be downloaded anonymously. Private and license-gated models still require signing in to Kaggle and accepting the model's terms.

Value

A function. For most models it is 'function(input_data)' and returns predictions as R objects. For language models it is 'function(prompt, max_length, sampler, strip_prompt, ...)' and returns a character vector of generated text.

Optional dependencies

Only the packages for the model types you use need to be installed: * Keras / KerasHub: 'keras3' and 'reticulate' * TensorFlow SavedModel / TF Hub: 'tensorflow' * TorchScript: 'torch' (then run 'torch::install_torch()' once) * XGBoost: 'xgboost' * GGUF language models: 'localLLM' (llama.cpp; no Python needed)

See Also

[list_model_variations()] to find the handle of a Kaggle model.

Examples

## Not run: 
# Kaggle (no token needed): DenseNet-121 KerasHub preset
predict_fn <- load_model("keras/densenet/keras/densenet_121_imagenet/3")

# TF Hub link
predict_fn <- load_model(
  "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2"
)

tmp <- tempfile(fileext = ".jpg")
download.file(
  paste0(
    "https://storage.googleapis.com/download.tensorflow.org/",
    "example_images/grace_hopper.jpg"
  ),
  tmp,
  mode = "wb"
)
img <- keras3::image_load(tmp, target_size = c(224, 224)) |>
  keras3::image_to_array() |>
  abind::abind(along = 0)

# TF Hub MobileNet expects pixels in [0, 1]; KerasHub presets rescale
# 0-255 input themselves.
result <- predict_fn(img / 255)
keras::mobilenet_decode_predictions(result[, -1, drop = FALSE])

# Small language model: GPT-2 base (124M parameters, ~500 MB download).
# One-time Python setup:
#   keras3::install_keras(backend = "tensorflow")
#   reticulate::py_install("keras-hub", envname = "r-keras")
generate <- load_model("keras/gpt2/keras/gpt2_base_en")

# Greedy decoding: deterministic, same output every time
generate("The R programming language is", max_length = 40, sampler = "greedy")

# Sampling: more varied text; several prompts at once
generate(
  c("Once upon a time,", "The best way to learn statistics is"),
  max_length = 50,
  sampler = "top_k"
)

# Return only the continuation, without the prompt
generate("My favourite R package is", max_length = 30, strip_prompt = TRUE)

## End(Not run)