From 896c5a93555d7eb1ca1b3122833d231b71ac848d Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 11 Apr 2024 15:31:44 -0500 Subject: [PATCH] added sf2csv --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/sf2csv.R | 22 ++++++++++++++++++++++ man/sf2csv.Rd | 21 +++++++++++++++++++++ tests/testthat/test_sf2csv.R | 12 ++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 R/sf2csv.R create mode 100644 man/sf2csv.Rd create mode 100644 tests/testthat/test_sf2csv.R diff --git a/DESCRIPTION b/DESCRIPTION index 9ff7d95..9659722 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -63,4 +63,4 @@ Imports: tidyr, tmap, utils -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index e8778e9..a1dac57 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,6 +43,7 @@ export(planform_dimensions) export(reach_rhg_graph) export(sf2arc) export(sf2arc_table) +export(sf2csv) export(shear_stress) export(slope_sinuosity) export(stream_power) diff --git a/R/sf2csv.R b/R/sf2csv.R new file mode 100644 index 0000000..dc58ca2 --- /dev/null +++ b/R/sf2csv.R @@ -0,0 +1,22 @@ +#' @title Save `sf` object to a .csv file. +#' +#' @export +#' @param sf_object `sf` object; The object whose data frame will be saved +#' as a csv file. +#' @param csv_path character; Path to the output csv file. +#' +#' @return Writes the `sf_object` as a csv file specified by +#' `csv_path` +#' +sf2csv <- function(sf_object, csv_path) { + # Check parameters + assert_that("sf" %in% class(sf_object), + msg = "sf_object must be of class `sf`") + + # Remove geometry + df <- sf::st_drop_geometry(sf_object) + + # write sf_object data frame to csv file + readr::write_csv(x = df, + file = csv_path) +} diff --git a/man/sf2csv.Rd b/man/sf2csv.Rd new file mode 100644 index 0000000..86f8964 --- /dev/null +++ b/man/sf2csv.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sf2csv.R +\name{sf2csv} +\alias{sf2csv} +\title{Save `sf` object to a .csv file.} +\usage{ +sf2csv(sf_object, csv_path) +} +\arguments{ +\item{sf_object}{`sf` object; The object whose data frame will be saved +as a csv file.} + +\item{csv_path}{character; Path to the output csv file.} +} +\value{ +Writes the `sf_object` as a csv file specified by +`csv_path` +} +\description{ +Save `sf` object to a .csv file. +} diff --git a/tests/testthat/test_sf2csv.R b/tests/testthat/test_sf2csv.R new file mode 100644 index 0000000..d8678bf --- /dev/null +++ b/tests/testthat/test_sf2csv.R @@ -0,0 +1,12 @@ +library(fluvgeo) + +forward_slash_path <- function(fc_path) { + gsub("\\\\", "/", fc_path) +} + +test_that("check output csv file exists", { + sf_object <- fluvgeo::sin_flowline_sf + csv_path <- forward_slash_path(tempfile(fileext = ".csv")) + sf2csv(sf_object = sf_object, csv_path = csv_path) + expect_true(file.exists(csv_path)) +})