Skip to content

Commit

Permalink
Remove dependency on package stringr (use gsub)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Oct 2, 2023
1 parent 97f6f80 commit b404691
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Imports:
gridExtra,
kwb.utils,
remotes,
stringr,
xml2
Suggests:
knitr,
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ importFrom(kwb.utils,asNoFactorDataFrame)
importFrom(kwb.utils,catAndRun)
importFrom(kwb.utils,createDirectory)
importFrom(kwb.utils,hsShowPdf)
importFrom(kwb.utils,isTryError)
importFrom(kwb.utils,preparePdf)
importFrom(kwb.utils,printIf)
importFrom(kwb.utils,replaceFileExtension)
importFrom(kwb.utils,selectElements)
importFrom(remotes,available_packages)
importFrom(stringr,str_replace_all)
importFrom(utils,download.file)
importFrom(utils,getFromNamespace)
importFrom(utils,read.table)
Expand Down
29 changes: 16 additions & 13 deletions R/abimo_functions_am.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ abimo_xml_BER <- function (
}

#' Helper function: replace value
#' @description searches for string for parameter=pattern_value
#' pattern and replaces with parameter="new_value" for all found
#' entries
#'
#' @description searches string for parameter=<pattern_value> and replaces with
#' parameter=<new_value> for all found entries
#'
#' @param string string with ABIMO config
#' @param new_value new parameter value
#' @param parameter parameter name to search for (default: "etp")
#' @param pattern_value pattern of value field (default: '\"\[0-9\]+?\\.?\[0-9\]+?\"')
#' @return returns string with modified parameter = value
#' @importFrom stringr str_replace_all
#' @export
#' @examples
#' ### Simple string
Expand All @@ -281,15 +281,18 @@ abimo_xml_BER <- function (
#' ### Default ABIMO config
#' config <- readLines(kwb.abimo::default_config())
#' replace_value(config, new_value = 100, parameter = "etp")
replace_value <- function(string,
new_value,
parameter = "etp",
pattern_value = "\"[0-9]+?\\.?[0-9]+?\"") {

pattern <- sprintf("%s=%s", parameter, pattern_value)
replacement <- sprintf("%s=\"%s\"", parameter, new_value)

stringr::str_replace_all(string, pattern, replacement)
replace_value <- function(
string,
new_value,
parameter = "etp",
pattern_value = "\"[0-9]+?\\.?[0-9]+?\""
)
{
gsub(
pattern = sprintf("%s=%s", parameter, pattern_value),
replacement = sprintf("%s=\"%s\"", parameter, new_value),
x = string
)
}

#' change potential evaporation in Abimo config.xml
Expand Down
5 changes: 2 additions & 3 deletions man/replace_value.Rd

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

0 comments on commit b404691

Please sign in to comment.