Skip to content

Commit

Permalink
Merge branch 'main' of github.com:spyrales/shinygouv
Browse files Browse the repository at this point in the history
  • Loading branch information
jengelaere committed Aug 23, 2023
2 parents 05388b1 + 4010c30 commit fe7f7f9
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 61 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Imports:
magrittr (>= 2.0.3),
purrr (>= 0.3.4),
shiny (>= 1.7.1),
stringr (>= 1.4.0),
tools,
utils,
XML (>= 3.99.0.10)
Expand All @@ -41,6 +40,7 @@ Suggests:
knitr (>= 1.39),
readxl (>= 1.4.0),
rmarkdown (>= 2.14),
stringr (>= 1.4.0),
testthat (>= 3.0.0),
tibble (>= 3.1.7),
withr (>= 2.5.0)
Expand Down
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,5 @@ importFrom(shiny,tagList)
importFrom(shiny,updateCheckboxInput)
importFrom(shiny,updateNumericInput)
importFrom(shiny,updateSelectInput)
importFrom(stringr,str_detect)
importFrom(stringr,str_pad)
importFrom(stringr,str_replace_all)
importFrom(stringr,str_which)
importFrom(utils,file_test)
importFrom(utils,read.csv2)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# shinygouv 1.0.2

## chore

* suppression de la dépendance à `stringr`

## fix

* Correction de la classe du `fluidPage_dsfr()`
Expand Down
15 changes: 8 additions & 7 deletions R/convert_file_to_dsfr.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#' @param tab_corresp la table de correspondance comportant 2 colonnes (composant_shiny, composant_dsfr)
#' @importFrom glue glue
#' @importFrom purrr reduce2
#' @importFrom stringr str_replace_all
#' @importFrom utils file_test
#'
#' @return un fichier
Expand All @@ -28,12 +27,14 @@ convert_file_to_dsfr <- function(file, tab_corresp) {
if (isTRUE(grep("navbarPage", file_read) > 0)) {
message(glue::glue("Attention le {file} contient un 'navbarPage()', la version dsfr n\u00e9cessite un header, voir \\?navbarPage_dsfr"))
}

file_convert <- purrr::reduce2(
paste0(tab_corresp$composant_shiny, "\\("),
paste0(tab_corresp$composant_dsfr, "\\("),
.init = file_read,
stringr::str_replace_all)

file_convert <- purrr::reduce2(paste0(tab_corresp$composant_shiny, "\\("),
paste0(tab_corresp$composant_dsfr, "\\("),
.init = file_read,
function(string, pattern, replacement) {
gsub(pattern, replacement, string)
})

writeLines(file_convert, con = file)

}
Expand Down
3 changes: 1 addition & 2 deletions R/convert_to_dsfr.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#' @importFrom purrr map
#' @importFrom glue glue
#' @importFrom utils read.csv2
#' @importFrom stringr str_which
#' @return des fichiers modifies
#'
#' @export
Expand Down Expand Up @@ -65,7 +64,7 @@ convert_to_dsfr <- function(path = "R/", version = get_dsfr_version()) {
# ajout de la dependance a app_ui.R si il existe
if (file.exists(file.path(path, "app_ui.R"))) {
file_read <- readLines(file.path(path, "app_ui.R"))
local <- stringr::str_which(string = file_read, pattern = "@import shiny")
local <- grep(pattern = "@import shiny", x = file_read)
for (i in local) {
file_read <- append(file_read, "#' @import shinygouv", after = i)
}
Expand Down
2 changes: 1 addition & 1 deletion R/get_dsfr_version.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' get_dsfr_version()
#' get_dsfr_version(with_v = TRUE)
get_dsfr_version <- function(with_v = FALSE) {
res <- utils::packageDescription(pkg = "shinygouv")[["VersionDsfr"]]
res <- utils::packageDescription(pkg = 'shinygouv')[["VersionDsfr"]]
if (isTRUE(with_v)) {
res <- paste0("v", res)
}
Expand Down
3 changes: 2 additions & 1 deletion R/parse_html.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

#' Traduit du html en resultat shiny
#'
Expand All @@ -12,6 +12,7 @@
#' html_code <- '<button id="test" type="button" class="btn btn-default action-button">test</button>'
#' parse_html(html_code)
parse_html <- function(html_code, zone = "/html/body/*") {

xml <- parse_to_xml(html_code, zone) %>% render_node(prefix = TRUE)

eval(parse(text = xml))
Expand Down
2 changes: 1 addition & 1 deletion R/parse_to_xml.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

#' Traduit du code html de htmltools pour du code xml
#'
Expand Down
29 changes: 6 additions & 23 deletions R/render_node.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

#' render_node
#'
Expand All @@ -7,9 +7,7 @@
#' @param prefix si TRUE alors le resultat sera prefixe par tags
#'
#' @importFrom XML xmlName xmlValue xmlChildren
#' @importFrom stringr str_pad
#' @importFrom purrr partial
#'
#' @noRd
render_node <- function(node, indent = 0, prefix = FALSE) {
if (xmlName(node) == "text") {
Expand All @@ -24,43 +22,28 @@ render_node <- function(node, indent = 0, prefix = FALSE) {
xmlName(node)
}

my_render <- partial(
render_node,
indent = newIndent,
prefix = prefix
)
my_render <- partial(render_node, indent = newIndent,
prefix = prefix)
newIndent <- indent + length(tagName) + 1
xmlChildren(node) %>%
Keep(my_render, .) %>%
append(make_attrs(node), .) %>%
paste(collapse = str_pad(
",\n",
width = newIndent,
side = c("right")
)) %>%
paste(collapse = sprintf(paste0("%-",newIndent,"s"), ",\n")) %>%
trimws(which = c("left")) %>%
paste0(tagName, "(", ., ")")
}
}

#' @noRd
#' @importFrom XML xmlAttrs
#' @importFrom stringr str_detect
make_attrs <- function(node) {
attrs <- xmlAttrs(node)
names(attrs) %>% Map(function(name) {
val <- attrs[[name]]
if (str_detect(string = name, pattern = "-") || str_detect(
string = name,
pattern = "for"
)) {
if (grepl(pattern = "-", x = name) || grepl(pattern = "for", x = name)) {
name <- paste0("`", name, "`")
}
paste0(name, " = ", if (val == "") {
"NA"
} else {
paste0("\"", val, "\"")
})
paste0(name, " = ", if (val == "") {"NA"} else { paste0("\"", val, "\"")})
}, .)
}

Expand Down
2 changes: 1 addition & 1 deletion R/update_inline.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

#' @noRd
update_inline <- function(inputId, inline, session) {
Expand Down
17 changes: 8 additions & 9 deletions dev/tools/flat_convert_to_dsfr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pkgload::load_all(export_all = FALSE)
#' @param tab_corresp la table de correspondance comportant 2 colonnes (composant_shiny, composant_dsfr)
#' @importFrom glue glue
#' @importFrom purrr reduce2
#' @importFrom stringr str_replace_all
#' @importFrom utils file_test
#'
#' @return un fichier
Expand All @@ -43,12 +42,13 @@ convert_file_to_dsfr <- function(file, tab_corresp) {
if (isTRUE(grep("navbarPage", file_read) > 0)) {
message(glue::glue("Attention le {file} contient un 'navbarPage()', la version dsfr n\u00e9cessite un header, voir \\?navbarPage_dsfr"))
}
file_convert <- purrr::reduce2(
paste0(tab_corresp$composant_shiny, "\\("),
paste0(tab_corresp$composant_dsfr, "\\("),
.init = file_read,
stringr::str_replace_all)
file_convert <- purrr::reduce2(paste0(tab_corresp$composant_shiny, "\\("),
paste0(tab_corresp$composant_dsfr, "\\("),
.init = file_read,
function(string, pattern, replacement) {
gsub(pattern, replacement, string)
})
writeLines(file_convert, con = file)
}
Expand Down Expand Up @@ -154,7 +154,6 @@ convert_to_dsfr(path = "le_chemin_de_mon_application")
#' @importFrom purrr map
#' @importFrom glue glue
#' @importFrom utils read.csv2
#' @importFrom stringr str_which
#' @return des fichiers modifies
#'
#' @export
Expand Down Expand Up @@ -190,7 +189,7 @@ convert_to_dsfr <- function(path = "R/", version = get_dsfr_version()) {
# ajout de la dependance a app_ui.R si il existe
if (file.exists(file.path(path, "app_ui.R"))) {
file_read <- readLines(file.path(path, "app_ui.R"))
local <- stringr::str_which(string = file_read, pattern = "@import shiny")
local <- grep(pattern = "@import shiny", x = file_read)
for (i in local) {
file_read <- append(file_read, "#' @import shinygouv", after = i)
}
Expand Down
10 changes: 3 additions & 7 deletions dev/tools/flat_tools.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ test_that("parse_to_xml works", {
#' @param prefix si TRUE alors le resultat sera prefixe par tags
#'
#' @importFrom XML xmlName xmlValue xmlChildren
#' @importFrom stringr str_pad
#' @importFrom purrr partial
#'
#' @noRd
render_node <- function(node, indent = 0, prefix = FALSE) {
if (xmlName(node) == "text") {
txt <- xmlValue(node)
Expand All @@ -100,22 +99,19 @@ render_node <- function(node, indent = 0, prefix = FALSE) {
xmlChildren(node) %>%
Keep(my_render, .) %>%
append(make_attrs(node), .) %>%
paste(collapse = str_pad(",\n", width = newIndent,
side = c("right"))) %>%
paste(collapse = sprintf(paste0("%-",newIndent,"s"), ",\n")) %>%
trimws(which = c("left")) %>%
paste0(tagName, "(", ., ")")
}
}
#' @noRd
#' @importFrom XML xmlAttrs
#' @importFrom stringr str_detect
make_attrs <- function(node) {
attrs <- xmlAttrs(node)
names(attrs) %>% Map(function(name) {
val <- attrs[[name]]
if (str_detect(string = name, pattern = "-") || str_detect(string = name,
pattern = "for")) {
if (grepl(pattern = "-", x = name) || grepl(pattern = "for", x = name)) {
name <- paste0("`", name, "`")
}
paste0(name, " = ", if (val == "") {"NA"} else { paste0("\"", val, "\"")})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-parse_html.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

test_that("tester parse_html", {
html_code <- '<button id="test" type="button" class="btn btn-default action-button">test</button>'
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-parse_to_xml.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

test_that("parse_to_xml works", {
expect_true(inherits(parse_to_xml, "function"))
Expand All @@ -21,4 +21,5 @@ test_that("parse_to_xml works", {

#' @description tester la classe du resultat de la fonction sur un shiny.tag
expect_s3_class(test2, "XMLInternalElementNode")

})
2 changes: 1 addition & 1 deletion tests/testthat/test-render_node.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WARNING - Generated by {fusen} from dev/tools/flat_tools.Rmd: do not edit by hand
# WARNING - Generated by {fusen} from /dev/tools/flat_tools.Rmd: do not edit by hand

test_that("render_node works", {
expect_true(inherits(render_node, "function"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ knitr::opts_chunk$set(
library(shinygouv)
```


# Note sur les orientations initiales de développement : présentation des options

### Comparaison entre les différentes possibilités pour répondre au besoin d'utilisation du framework [DSFR](https://gouvfr.atlassian.net/wiki/spaces/DB/overview?homepageId=145359476)
Expand Down Expand Up @@ -47,6 +48,7 @@ DT::datatable(
Utiliser les fonctionnalités de {bslib} pour utiliser le framework DSFR.



Avantages :

- Simple d'utilisation, une fonction ou deux pour appliquer le bon thème bootstrap qui reprend les règles CSS de DSFR
Expand Down Expand Up @@ -264,6 +266,7 @@ Avantages :

> Néanmoins, le temps gagné sur les inputs basiques pourrait permettre d'implementer de nouveaux inputs si nécéssaire

- Implémentation plus simple pour les composants compatibles.
- Pas besoin de connaissances avancées en JS pour les composants compatibles.

Expand All @@ -272,6 +275,8 @@ Inconvénients :
- Lié au package {shiny} et aux inputs implémentés
- Si l'input n'existe pas dans {shiny}, alors il faudra recoder la partie JS (c'est vrai pour les options A et B)
- Maintenance : suivre {shiny} et DSFR. Autrement dit, si la classe de l'input {shiny} devient autre chose que `action-button` notre code ne marchera plus (peu probable...).
- Les librairies bootstrap qui gravitent autour de {shiny} ne sont pas compatibles DSFR. Il faudra donc les remplacer par les librairies maison ou des implémentation de lib JS. Un exemple : shinyWidget


Les changements sont énumérés:

Expand All @@ -296,15 +301,16 @@ DT::datatable(
)
```


Les éléments tels que `navbarPage()`, `tabPanel()` etc sont possibles dans la limite de ce que propose le DSFR.
Ils nécessitent plus ou moins une demi-journée par composant (codé avec des htmlTemplates).



## Notre recommandation

Nous préconisons l'**option D**. Pourquoi ?


Cette approche permet de mettre en avant le travail réalisé sur le framework DSFR avec une implémentation possible de l'intégralité des fonctionnalités sans la contrainte et la lourdeur de connaitre le JS.
Elle permet aussi de faciliter la maintenance du package par les futures mainteneurs.
Bien que cette option ne réponde pas directement à l'objectif : `éviter d'avoir des fonctions spécifiques pour ne pas géner le passage d'un template à un autre`, on peut tout à fait imaginer une fonction qui parcourt le code pour remplacer les appels à {shiny} avec les fonctions {DSFR}.

0 comments on commit fe7f7f9

Please sign in to comment.