|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +if (!requireNamespace("devtools", quietly = TRUE)) { |
| 4 | + stop("Please install devtools first: install.packages('devtools')") |
| 5 | +} |
| 6 | + |
| 7 | +if (!requireNamespace("usethis", quietly = TRUE)) { |
| 8 | + stop("Please install usethis first: install.packages('usethis')") |
| 9 | +} |
| 10 | + |
| 11 | +cat("Package name: ") |
| 12 | +pkg_name <- readLines("stdin", n = 1) |
| 13 | + |
| 14 | +cat("Package title: ") |
| 15 | +pkg_title <- readLines("stdin", n = 1) |
| 16 | + |
| 17 | +cat("Author name: ") |
| 18 | +author_name <- readLines("stdin", n = 1) |
| 19 | + |
| 20 | +cat("Author email: ") |
| 21 | +author_email <- readLines("stdin", n = 1) |
| 22 | + |
| 23 | +cat("Package description (a few sentences): ") |
| 24 | +pkg_description <- readLines("stdin", n = 1) |
| 25 | + |
| 26 | +usethis::create_package(pkg_name, open = FALSE) |
| 27 | + |
| 28 | +setwd(pkg_name) |
| 29 | + |
| 30 | +desc_content <- paste0( |
| 31 | + "Package: ", pkg_name, "\n", |
| 32 | + "Title: ", pkg_title, "\n", |
| 33 | + "Version: 0.0.0.9000\n", |
| 34 | + "Authors@R: person('", author_name, "', email = '", author_email, "', role = c('aut', 'cre'))\n", |
| 35 | + "Description: ", pkg_description, "\n", |
| 36 | + "License: MIT + file LICENSE\n", |
| 37 | + "Encoding: UTF-8\n", |
| 38 | + "Roxygen: list(markdown = TRUE)\n" |
| 39 | +) |
| 40 | +writeLines(desc_content, "DESCRIPTION") |
| 41 | + |
| 42 | +hello <- paste0( |
| 43 | + "#' Hello World Function\n", |
| 44 | + "#' @return A greeting\n", |
| 45 | + "#' @export\n", |
| 46 | + "hello <- function() {\n", |
| 47 | + " 'Hello, world!'\n", |
| 48 | + "}" |
| 49 | +) |
| 50 | +writeLines(hello, file.path("R", "hello.R")) |
| 51 | + |
| 52 | +devtools::document() |
| 53 | + |
| 54 | +usethis::use_mit_license() |
| 55 | + |
| 56 | +devtools::check() |
| 57 | + |
| 58 | +qmd_file <- file("example.qmd", open = "w") |
| 59 | + |
| 60 | +user_os <- R.version$os |
| 61 | + |
| 62 | +writeLines(c( |
| 63 | + "---", |
| 64 | + paste("title: \"", pkg_title, "\"", sep = ""), |
| 65 | + paste("author: \"", author_name, "\"", sep = ""), |
| 66 | + "params:", |
| 67 | + " name: \"World\"", |
| 68 | + paste0(" os: \"", user_os, "\""), |
| 69 | + "engine: knitr", |
| 70 | + "---", |
| 71 | + "", |
| 72 | + paste("# Hello from ", pkg_title), |
| 73 | + "", |
| 74 | + "Hello, \`r params$name\`!", |
| 75 | + "", |
| 76 | + "This example shows basic parameterization. Your operating system appears to be: **\`r params$os\`**.", |
| 77 | + "", |
| 78 | + paste("Package created by ", author_name, ".") |
| 79 | + ), |
| 80 | + qmd_file |
| 81 | +) |
| 82 | + |
| 83 | +close(qmd_file) |
| 84 | + |
| 85 | +system("git init") |
| 86 | +system("git add .") |
| 87 | +system("git commit -m 'Initial package setup'") |
| 88 | + |
| 89 | +cat("Package created successfully!\n") |
| 90 | +cat("To push to GitHub:\n") |
| 91 | +cat("1. Create a new repository on GitHub\n") |
| 92 | +cat("2. git remote add origin https://github.com/yourusername/", pkg_name, ".git\n") |
| 93 | +cat("3. git push -u origin main\n") |
0 commit comments