Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdougherty committed Apr 11, 2024
2 parents ce7f8dc + b3a1158 commit eeb6e32
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions R/sf2csv.R
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions man/sf2csv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test_sf2csv.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit eeb6e32

Please sign in to comment.