Skip to content

Commit

Permalink
wip: function to save to path
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjohnst86 committed Mar 19, 2024
1 parent ce54117 commit 6bd1498
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion R/save-to-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,39 @@
#' @return Save a CSV file, returns tibble invisibly.
#' @export
#'
save_as_csv <- function(data, path) {
#' @examples
#'
#' library(dplyr)
#' general_feedback <- get_feedback_survey("general")
#' extract_feedback_quantitative(general_feedback) |> save_feedback_to_csv(c("course_id", "course_version", "date"))
#' group_split(course_id, course_version, date) |>
#' walk(~ save_to_csv(.x, create_path_from_columns(.x, c("course_id", "course_version", "date"))))
#' View()
#'
save_to_csv <- function(data, path) {
fs::dir_create(fs::path_dir(path))
data |>
readr::write_csv(here::here(path))
path
}

create_path_from_columns <- function(data, columns) {
data |>
dplyr::select({{ columns }}) |>
dplyr::distinct() |>
dplyr::mutate(dplyr::across({{ columns }}, as.character)) |>
purrr::pmap(fs::path) |>
purrr::map(~usethis::proj_path("data", .x))
}

save_feedback_to_csv <- function(data, columns) {
data |>
tidyr::nest(.by = {{ columns }}) |>
dplyr::mutate(dplyr::across({{ columns }}, as.character)) |>
dplyr::mutate(path = fs::path(dplyr::c_across({{columns}})))
# purrr::map_chr(~ {
# path <- create_path_from_columns(.x, c("course_id", "course_version", "date")))
# save_to_csv(.x,
# })

}

0 comments on commit 6bd1498

Please sign in to comment.