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

Feature: CSV export #40

Open
StatsRhian opened this issue Oct 14, 2022 · 1 comment
Open

Feature: CSV export #40

StatsRhian opened this issue Oct 14, 2022 · 1 comment

Comments

@StatsRhian
Copy link
Contributor

It could be nice to have the ability to export a CSV of the datasaurus data from the R package itself

@StatsRhian StatsRhian changed the title ee as well. Feature: CSV export Oct 14, 2022
osheen1 added a commit that referenced this issue Oct 17, 2022
StatsRhian added a commit that referenced this issue Oct 19, 2022
@cyberLaVoy
Copy link

Here is a possible solution to this issue.

#' Save Datasets from an R Package
#'
#' The save_datasets function saves all datasets in a specified R package, saving them as CSV files in a specified folder.
#'
#' @param package_name The name of the R package containing the datasets to be saved. The default value is "datasauRus".
#' @param folder_name The name of the folder where the CSV files will be saved. The default value is "datasaurus_dozen_datasets".
#'
#' @return The save_datasets function doesn't return anything.
#' @export
save_datasets <- function(package_name="datasauRus", folder_name="datasaurus_dozen_datasets") {
  # Get a list of all datasets in the package
  datasets <- data(package = package_name)
   
  # Create the specified folder in the current working directory, if it doesn't already exist
  if (!dir.exists(folder_name)) {
    dir.create(folder_name)
  }

  # Loop through each dataset
  for ( dataset in datasets$results[, "Item"] )  {
    # Write the dataset to a csv file in the specified folder
    write.csv(get(dataset), file = paste0(folder_name, "/", dataset, ".csv"))
  }
}

This function is package ready, with all documentation. Note that we are able to set default arguments for package_name and folder_name because this function would be apart of this package. And therefore would be contextually correct.

Next iteration on this function could provide an option to save the datasets in different formats, such as in Excel and/or RDA format.

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

No branches or pull requests

2 participants