Skip to content

Commit

Permalink
Fixed define_cells
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-corcoran-barrios committed Mar 30, 2023
1 parent ec78267 commit 765a4bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
15 changes: 1 addition & 14 deletions R/Troublemaker.R
Expand Up @@ -84,20 +84,7 @@ troublemaker <- function(Rasterdomain = NULL, Rastercurrent = NULL, species_name
name = "Problem",
verbose = FALSE){
if(!is.null(Rasterdomain)){
TempDomain <- TroublemakeR::define_cells(Rasterdomain = Rasterdomain)
if(file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = T)
cat(TempDomain)
cat("\n")
sink()
}
if(!file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = F)
cat(TempDomain)
cat("\n")
sink()
}

TempDomain <- TroublemakeR::define_cells(Rasterdomain = Rasterdomain, name = name)
if(verbose){
message("TempDomain ready")
}
Expand Down
18 changes: 16 additions & 2 deletions R/define_cells.R
Expand Up @@ -4,6 +4,7 @@
#' This function takes a Raster object and creates a character vector made for the troublemaker function
#'
#' @param Rasterdomain A Raster object with any value in the cells that are part of the problem and NA values where the problem is not to be solved
#' @param name The name of the output file
#' @importFrom terra as.data.frame
#'
#' @return A character vector to be used in troblemaker
Expand All @@ -20,9 +21,22 @@
#'
#' define_cells(Test[[1]])
#'
#' file.remove("Problem.dat")
#'
#' @author Derek Corcoran
define_cells <- function(Rasterdomain){
define_cells <- function(Rasterdomain, name = "Problem"){
Temp <- terra::as.data.frame(Rasterdomain, cells = T, xy = T)
Result <- paste0(paste(c("set Cells :=", Temp$cell, ";"), collapse = " "), "\n")
return(Result)
if(file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = T)
cat(Result)
cat("\n")
sink()
}
if(!file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = F)
cat(Result)
cat("\n")
sink()
}
}
6 changes: 5 additions & 1 deletion man/define_cells.Rd

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

0 comments on commit 765a4bc

Please sign in to comment.