Skip to content

Commit a968993

Browse files
feat: Suppression dependance stringr
pourquoi: - supprimer la dependance à stringr quoi: - remplacement des fonctions de stringr par du rbase Issue #51
1 parent 330c7cb commit a968993

16 files changed

+49
-75
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Imports:
3030
magrittr (>= 2.0.3),
3131
purrr (>= 0.3.4),
3232
shiny (>= 1.7.1),
33-
stringr (>= 1.4.0),
3433
utils,
3534
XML (>= 3.99.0.10)
3635
Suggests:
@@ -39,6 +38,7 @@ Suggests:
3938
knitr (>= 1.39),
4039
readxl (>= 1.4.0),
4140
rmarkdown (>= 2.14),
41+
stringr (>= 1.4.0),
4242
testthat (>= 3.0.0),
4343
tibble (>= 3.1.7),
4444
withr (>= 2.5.0)

NAMESPACE

-4
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,5 @@ importFrom(shiny,tagList)
6868
importFrom(shiny,updateCheckboxInput)
6969
importFrom(shiny,updateNumericInput)
7070
importFrom(shiny,updateSelectInput)
71-
importFrom(stringr,str_detect)
72-
importFrom(stringr,str_pad)
73-
importFrom(stringr,str_replace_all)
74-
importFrom(stringr,str_which)
7571
importFrom(utils,file_test)
7672
importFrom(utils,read.csv2)

R/convert_file_to_dsfr.R

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88
#' @param tab_corresp la table de correspondance comportant 2 colonnes (composant_shiny, composant_dsfr)
99
#' @importFrom glue glue
1010
#' @importFrom purrr reduce2
11-
#' @importFrom stringr str_replace_all
1211
#' @importFrom utils file_test
1312
#'
1413
#' @return un fichier
1514
#' @noRd
1615
convert_file_to_dsfr <- function(file, tab_corresp) {
16+
1717
# verifie que c'est bien un fichier et non un dossier
1818
if (isTRUE(file_test("-f", file))) {
19+
1920
message(glue::glue("scan {file}"))
2021
file_read <- readLines(file)
21-
22+
2223
if (isTRUE(grep("tabPanel", file_read) > 0)) {
23-
message(glue::glue("Attention le {file} contient un 'tabPanel': \n - Si celui ci \u00e9tait dans un navbarPage alors le remplacer par un navbarPanel_dsfr. \n - Si celui ci \u00e9tait dans un 'tabsetPanel', alors le remplacer par un 'tabPanel_dsfr'"))
24+
message(glue::glue("Attention le {file} contient un 'tabPanel': \n - Si celui ci \u00e9tait dans un navbarPage alors le remplacer par un navbarPanel_dsfr. \n - Si celui ci \u00e9tait dans un 'tabsetPanel', alors le remplacer par un 'tabPanel_dsfr'"))
2425
}
2526

2627
if (isTRUE(grep("navbarPage", file_read) > 0)) {
27-
message(glue::glue("Attention le {file} contient un 'navbarPage()', la version dsfr n\u00e9cessite un header, voir \\?navbarPage_dsfr"))
28+
message(glue::glue("Attention le {file} contient un 'navbarPage()', la version dsfr n\u00e9cessite un header, voir \\?navbarPage_dsfr"))
2829
}
2930

30-
file_convert <- purrr::reduce2(
31-
paste0(tab_corresp$composant_shiny, "\\("),
32-
paste0(tab_corresp$composant_dsfr, "\\("),
33-
.init = file_read,
34-
stringr::str_replace_all
35-
)
31+
file_convert <- purrr::reduce2(paste0(tab_corresp$composant_shiny, "\\("),
32+
paste0(tab_corresp$composant_dsfr, "\\("),
33+
.init = file_read,
34+
function(string, pattern, replacement) {
35+
gsub(pattern, replacement, string)
36+
})
3637
writeLines(file_convert, con = file)
38+
3739
}
3840
return(NULL)
3941
}

R/convert_to_dsfr.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#' @importFrom purrr map
1313
#' @importFrom glue glue
1414
#' @importFrom utils read.csv2
15-
#' @importFrom stringr str_which
1615
#' @return des fichiers modifies
1716
#'
1817
#' @export
@@ -63,7 +62,7 @@ convert_to_dsfr <- function(path = "R/", version = get_dsfr_version()) {
6362
# ajout de la dependance a app_ui.R si il existe
6463
if (file.exists(file.path(path, "app_ui.R"))) {
6564
file_read <- readLines(file.path(path, "app_ui.R"))
66-
local <- stringr::str_which(string = file_read, pattern = "@import shiny")
65+
local <- grep(pattern = "@import shiny", x = file_read)
6766
for (i in local) {
6867
file_read <- append(file_read, "#' @import shinygouv", after = i)
6968
}

R/get_dsfr_version.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' get_dsfr_version()
1111
#' get_dsfr_version(with_v = TRUE)
1212
get_dsfr_version <- function(with_v = FALSE) {
13-
res <- utils::packageDescription(pkg = "shinygouv")[["VersionDsfr"]]
13+
res <- utils::packageDescription(pkg = 'shinygouv')[["VersionDsfr"]]
1414
if (isTRUE(with_v)) {
1515
res <- paste0("v", res)
1616
}

R/parse_html.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
#' Traduit du html en resultat shiny
44
#'
@@ -12,6 +12,7 @@
1212
#' html_code <- '<button id="test" type="button" class="btn btn-default action-button">test</button>'
1313
#' parse_html(html_code)
1414
parse_html <- function(html_code, zone = "/html/body/*") {
15+
1516
xml <- parse_to_xml(html_code, zone) %>% render_node(prefix = TRUE)
1617

1718
eval(parse(text = xml))

R/parse_to_xml.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
#' Traduit du code html de htmltools pour du code xml
44
#'

R/render_node.R

+6-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
#' render_node
44
#'
@@ -7,9 +7,7 @@
77
#' @param prefix si TRUE alors le resultat sera prefixe par tags
88
#'
99
#' @importFrom XML xmlName xmlValue xmlChildren
10-
#' @importFrom stringr str_pad
1110
#' @importFrom purrr partial
12-
#'
1311
#' @noRd
1412
render_node <- function(node, indent = 0, prefix = FALSE) {
1513
if (xmlName(node) == "text") {
@@ -24,43 +22,28 @@ render_node <- function(node, indent = 0, prefix = FALSE) {
2422
xmlName(node)
2523
}
2624

27-
my_render <- partial(
28-
render_node,
29-
indent = newIndent,
30-
prefix = prefix
31-
)
25+
my_render <- partial(render_node, indent = newIndent,
26+
prefix = prefix)
3227
newIndent <- indent + length(tagName) + 1
3328
xmlChildren(node) %>%
3429
Keep(my_render, .) %>%
3530
append(make_attrs(node), .) %>%
36-
paste(collapse = str_pad(
37-
",\n",
38-
width = newIndent,
39-
side = c("right")
40-
)) %>%
31+
paste(collapse = sprintf(paste0("%-",newIndent,"s"), ",\n")) %>%
4132
trimws(which = c("left")) %>%
4233
paste0(tagName, "(", ., ")")
4334
}
4435
}
4536

4637
#' @noRd
4738
#' @importFrom XML xmlAttrs
48-
#' @importFrom stringr str_detect
4939
make_attrs <- function(node) {
5040
attrs <- xmlAttrs(node)
5141
names(attrs) %>% Map(function(name) {
5242
val <- attrs[[name]]
53-
if (str_detect(string = name, pattern = "-") || str_detect(
54-
string = name,
55-
pattern = "for"
56-
)) {
43+
if (grepl(pattern = "-", x = name) || grepl(pattern = "for", x = name)) {
5744
name <- paste0("`", name, "`")
5845
}
59-
paste0(name, " = ", if (val == "") {
60-
"NA"
61-
} else {
62-
paste0("\"", val, "\"")
63-
})
46+
paste0(name, " = ", if (val == "") {"NA"} else { paste0("\"", val, "\"")})
6447
}, .)
6548
}
6649

R/update_inline.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
#' @noRd
44
update_inline <- function(inputId, inline, session) {

dev/tools/flat_convert_to_dsfr.Rmd

+8-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pkgload::load_all(export_all = FALSE)
2424
#' @param tab_corresp la table de correspondance comportant 2 colonnes (composant_shiny, composant_dsfr)
2525
#' @importFrom glue glue
2626
#' @importFrom purrr reduce2
27-
#' @importFrom stringr str_replace_all
2827
#' @importFrom utils file_test
2928
#'
3029
#' @return un fichier
@@ -43,12 +42,13 @@ convert_file_to_dsfr <- function(file, tab_corresp) {
4342
if (isTRUE(grep("navbarPage", file_read) > 0)) {
4443
message(glue::glue("Attention le {file} contient un 'navbarPage()', la version dsfr n\u00e9cessite un header, voir \\?navbarPage_dsfr"))
4544
}
46-
47-
file_convert <- purrr::reduce2(
48-
paste0(tab_corresp$composant_shiny, "\\("),
49-
paste0(tab_corresp$composant_dsfr, "\\("),
50-
.init = file_read,
51-
stringr::str_replace_all)
45+
46+
file_convert <- purrr::reduce2(paste0(tab_corresp$composant_shiny, "\\("),
47+
paste0(tab_corresp$composant_dsfr, "\\("),
48+
.init = file_read,
49+
function(string, pattern, replacement) {
50+
gsub(pattern, replacement, string)
51+
})
5252
writeLines(file_convert, con = file)
5353
5454
}
@@ -151,7 +151,6 @@ convert_to_dsfr(path = "le_chemin_de_mon_application")
151151
#' @importFrom purrr map
152152
#' @importFrom glue glue
153153
#' @importFrom utils read.csv2
154-
#' @importFrom stringr str_which
155154
#' @return des fichiers modifies
156155
#'
157156
#' @export
@@ -188,7 +187,7 @@ convert_to_dsfr <- function(path = "R/", version = get_dsfr_version()) {
188187
# ajout de la dependance a app_ui.R si il existe
189188
if (file.exists(file.path(path, "app_ui.R"))) {
190189
file_read <- readLines(file.path(path, "app_ui.R"))
191-
local <- stringr::str_which(string = file_read, pattern = "@import shiny")
190+
local <- grep(pattern = "@import shiny", x = file_read)
192191
for (i in local) {
193192
file_read <- append(file_read, "#' @import shinygouv", after = i)
194193
}

dev/tools/flat_tools.Rmd

+3-7
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ test_that("parse_to_xml works", {
7878
#' @param prefix si TRUE alors le resultat sera prefixe par tags
7979
#'
8080
#' @importFrom XML xmlName xmlValue xmlChildren
81-
#' @importFrom stringr str_pad
8281
#' @importFrom purrr partial
83-
#'
82+
#' @noRd
8483
render_node <- function(node, indent = 0, prefix = FALSE) {
8584
if (xmlName(node) == "text") {
8685
txt <- xmlValue(node)
@@ -100,22 +99,19 @@ render_node <- function(node, indent = 0, prefix = FALSE) {
10099
xmlChildren(node) %>%
101100
Keep(my_render, .) %>%
102101
append(make_attrs(node), .) %>%
103-
paste(collapse = str_pad(",\n", width = newIndent,
104-
side = c("right"))) %>%
102+
paste(collapse = sprintf(paste0("%-",newIndent,"s"), ",\n")) %>%
105103
trimws(which = c("left")) %>%
106104
paste0(tagName, "(", ., ")")
107105
}
108106
}
109107
110108
#' @noRd
111109
#' @importFrom XML xmlAttrs
112-
#' @importFrom stringr str_detect
113110
make_attrs <- function(node) {
114111
attrs <- xmlAttrs(node)
115112
names(attrs) %>% Map(function(name) {
116113
val <- attrs[[name]]
117-
if (str_detect(string = name, pattern = "-") || str_detect(string = name,
118-
pattern = "for")) {
114+
if (grepl(pattern = "-", x = name) || grepl(pattern = "for", x = name)) {
119115
name <- paste0("`", name, "`")
120116
}
121117
paste0(name, " = ", if (val == "") {"NA"} else { paste0("\"", val, "\"")})

tests/testthat/test-convert_file_to_dsfr.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# WARNING - Generated by {fusen} from /dev/tools/flat_convert_to_dsfr.Rmd: do not edit by hand
22

33
test_that("convert_file_to_dsfr works", {
4+
45
skip_on_ci()
56

67
library(tibble)
@@ -16,10 +17,8 @@ test_that("convert_file_to_dsfr works", {
1617
open = FALSE
1718
)
1819

19-
table_passage <- tibble(
20-
composant_shiny = c("actionButton", "fluidPage"),
21-
composant_dsfr = c("actionButton_dsfr", "fluidPage_dsfr")
22-
)
20+
table_passage <- tibble(composant_shiny = c("actionButton", "fluidPage"),
21+
composant_dsfr = c("actionButton_dsfr", "fluidPage_dsfr"))
2322

2423
fichier <- file.path(mydir, "R", "app_ui.R")
2524
convert_file_to_dsfr(file = fichier, table_passage)
@@ -30,10 +29,8 @@ test_that("convert_file_to_dsfr works", {
3029
expect_false(any(stringr::str_detect(fichier_transform, pattern = "fluidPage\\(")))
3130

3231

33-
table_passage <- tibble(
34-
composant_shiny = c("actionButton", "fluidPage", NA),
35-
composant_dsfr = c("actionButton_dsfr", "fluidPage_dsfr", "header_dsfr")
36-
)
32+
table_passage <- tibble(composant_shiny = c("actionButton", "fluidPage", NA),
33+
composant_dsfr = c("actionButton_dsfr", "fluidPage_dsfr", "header_dsfr"))
3734

3835
fichier <- file.path(mydir, "R", "app_ui.R")
3936
convert_file_to_dsfr(file = fichier, table_passage)
@@ -42,4 +39,5 @@ test_that("convert_file_to_dsfr works", {
4239
#' @description les composants dsfr n ayant pas d equivalent en shiny ne sont pas remplaces
4340

4441
expect_false(any(stringr::str_detect(fichier_transform, pattern = "header_dsfr\\(")))
42+
4543
})

tests/testthat/test-convert_to_dsfr.R

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ test_that("convert_to_dsfr works", {
1313
open = FALSE
1414
)
1515

16-
file.copy(
17-
from = file.path(mydir, "R", "app_ui.R"),
18-
to = file.path(mydir, "R", "module_ui.R")
19-
)
16+
file.copy(from = file.path(mydir, "R", "app_ui.R"),
17+
to = file.path(mydir, "R", "module_ui.R"))
2018

2119
#' @description Retourne une erreur si le numero de version est NULL
2220
expect_error(convert_to_dsfr(path = file.path(mydir, "R"), version = NULL))
@@ -34,4 +32,5 @@ test_that("convert_to_dsfr works", {
3432
#' @description les composants dsfr n ayant pas d equivalent en shiny ne sont pas remplaces, par exemple header_dsfr
3533

3634
expect_false(any(stringr::str_detect(app_ui, pattern = "header_dsfr\\(")))
35+
3736
})

tests/testthat/test-parse_html.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
test_that("tester parse_html", {
44
html_code <- '<button id="test" type="button" class="btn btn-default action-button">test</button>'

tests/testthat/test-parse_to_xml.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
test_that("parse_to_xml works", {
44
expect_true(inherits(parse_to_xml, "function"))
@@ -21,4 +21,5 @@ test_that("parse_to_xml works", {
2121

2222
#' @description tester la classe du resultat de la fonction sur un shiny.tag
2323
expect_s3_class(test2, "XMLInternalElementNode")
24+
2425
})

tests/testthat/test-render_node.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand
22

33
test_that("render_node works", {
44
expect_true(inherits(render_node, "function"))

0 commit comments

Comments
 (0)