Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7 previewer@main #33

Merged
merged 44 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
721df25
previewer
Polkas May 6, 2022
e81a592
card name docs and tests
Polkas May 9, 2022
c980c3d
merge to main
Polkas May 9, 2022
a462c7d
samll update
Polkas May 9, 2022
a9217fa
Merge branch 'main' into 7_previewer@main
Polkas May 9, 2022
ab5a4d9
fix app
Polkas May 9, 2022
04197e1
fix app
Polkas May 9, 2022
ba2d9e6
fix app
Polkas May 9, 2022
aa78920
improve app
Polkas May 9, 2022
6b64bbe
clean check
Polkas May 9, 2022
1227d1e
tests
Polkas May 9, 2022
2d525d2
styler
Polkas May 9, 2022
de19bb7
vignette
Polkas May 9, 2022
d966679
vignette
Polkas May 9, 2022
c892904
Apply suggestions from code review
Polkas May 9, 2022
e4b617e
vignette
Polkas May 9, 2022
bcaee21
vignette
Polkas May 9, 2022
6af1d66
styler
Polkas May 9, 2022
afb7afe
covr 1
Polkas May 10, 2022
2318701
tests
Polkas May 10, 2022
bb57b91
tests
Polkas May 10, 2022
ebea05c
styler
Polkas May 10, 2022
7dfd31e
Apply suggestions from code review
Polkas May 10, 2022
d8c496e
verify fa
Polkas May 10, 2022
b318998
notification and icon size
Polkas May 10, 2022
bd3fb58
times not remove
Polkas May 10, 2022
5accb53
rm LICENSE md
Polkas May 10, 2022
c238112
rm js
Polkas May 10, 2022
0572035
Nik
Polkas May 10, 2022
96db6af
Merge branch 'main' into 7_previewer@main
Polkas May 10, 2022
42cb87e
Nik 2
Polkas May 10, 2022
55de522
Dawid, rm template for teal module
Polkas May 10, 2022
a1f13c6
rm old test
Polkas May 10, 2022
f0f9ed7
simplify reactivity
Polkas May 10, 2022
72030fc
docs
Polkas May 10, 2022
4defe79
add reset to previewer
Polkas May 11, 2022
a4c3a43
more ui funcs
Polkas May 11, 2022
5fa19a8
js code disable
Polkas May 11, 2022
cee3361
styler
Polkas May 11, 2022
e674ece
rm console.log
Polkas May 11, 2022
49aa25b
example
Polkas May 11, 2022
0a2f695
example
Polkas May 11, 2022
00f8f37
docs
Polkas May 12, 2022
8894421
tests
Polkas May 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ BugReports: https://github.com/insightsengineering/teal.reporter/issues
Imports:
checkmate,
grid,
knitr,
R6,
rmarkdown,
shiny,
shinyWidgets,
yaml,
zip
Suggests:
ggplot2,
knitr,
lattice,
png,
rmarkdown,
rtables,
testthat
VignetteBuilder:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export(add_card_button_ui)
export(as_yaml_auto)
export(download_report_button_srv)
export(download_report_button_ui)
export(reporter_previewer_srv)
export(reporter_previewer_ui)
export(reset_report_button_srv)
export(reset_report_button_ui)
export(rmd_output_arguments)
Expand Down
7 changes: 1 addition & 6 deletions R/DownloadModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ download_report_button_ui <- function(id) {
#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`.
#' @param id `character`
#' @param reporter `Reporter` instance.
#' @param notification `logical` whether to add a shiny notification about the download process. Default `TRUE`.
#' @param rmd_output `character` vector with `rmarkdown` output types,
#' by default all possible `c("pdf_document", "html_document", "powerpoint_presentation", "word_document")`.
#' @param rmd_yaml_args `named list` vector with `Rmd` `yaml` header fields and their default values.
Expand All @@ -35,7 +34,6 @@ download_report_button_ui <- function(id) {
#' @export
download_report_button_srv <- function(id,
reporter,
notification = TRUE,
rmd_output = c(
"html_document", "pdf_document",
"powerpoint_presentation", "word_document"
Expand All @@ -45,7 +43,6 @@ download_report_button_srv <- function(id,
date = as.character(Sys.Date()), output = "html_document"
)) {
checkmate::assert_class(reporter, "Reporter")
checkmate::assert_flag(notification)
checkmate::assert_subset(rmd_output, c(
"html_document", "pdf_document",
"powerpoint_presentation", "word_document"
Expand Down Expand Up @@ -122,9 +119,7 @@ download_report_button_srv <- function(id,
paste("report_", format(Sys.time(), "%y%m%d%H%M%S"), ".zip", sep = "")
},
content = function(file) {
if (notification) {
shiny::showNotification(sprintf("Rendering and Downloading a document."))
}
shiny::showNotification("Rendering and Downloading the document.")
input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]])
names(input_list) <- names(rmd_yaml_args)
report_render_and_compress(reporter, input_list, file)
Expand Down
250 changes: 250 additions & 0 deletions R/Previewer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
#' Reporter Previewer User Interface
#' @description reporter previewer user interface to visualize and manipulate the already added report Cards
#' @param id `character`
#' @param rmd_output `character` vector with `rmarkdown` output types,
#' by default all possible `c("pdf_document", "html_document", "powerpoint_presentation", "word_document")`.
#' @param rmd_yaml_args `named list` vector with `Rmd` `yaml` header fields and their default values.
#' Default `list(author = "NEST", title = "Report", date = Sys.Date(), output = "html_document")`.
#' Please update only values at this moment.
#' @export
reporter_previewer_ui <- function(id, rmd_output = c(
"html_document", "pdf_document",
"powerpoint_presentation", "word_document"
),
rmd_yaml_args = list(
author = "NEST", title = "Report",
date = as.character(Sys.Date()), output = "html_document"
)) {
ns <- shiny::NS(id)
encoding <- shiny::tagList(
shiny::tags$h3("Download the Report"),
shiny::tags$hr(),
shiny::textInput(ns("author"), label = "Author:", value = rmd_yaml_args$author),
shiny::textInput(ns("title"), label = "Title:", value = rmd_yaml_args$title),
shiny::dateInput(ns("date"), "Date:", value = rmd_yaml_args$date),
shiny::tags$div(
shinyWidgets::pickerInput(
inputId = ns("output"),
label = "Choose a document type: ",
choices = rmd_output,
selected = rmd_yaml_args$output
)
),
shiny::tags$a(
id = ns("download_data_prev"),
class = "btn btn-primary shiny-download-link",
href = "",
target = "_blank",
download = NA,
shiny::icon("download"),
"Download Report"
),
teal.reporter::reset_report_button_ui(ns("resetButtonPreviewer"))
)

shiny::fluidRow(
add_previewer_js(ns),
add_previewer_css(),
shiny::tags$div(
shiny::tags$div(
class = "col-md-3",
shiny::tags$div(class = "well", encoding)
),
shiny::tags$div(
class = "col-md-9",
shiny::uiOutput(ns("pcards"))
)
)
)
}

#' Reporter Previewer Server
#' @description server supporting the functionalities of the reporter previewer
#' @param id `character`
#' @param reporter `Reporter` instance
#' @param rmd_yaml_args `named list` vector with `Rmd` `yaml` header fields and their default values.
#' Default `list(author = "NEST", title = "Report", date = Sys.Date(), output = "html_document")`.
#' Please update only values at this moment.
#' @export
reporter_previewer_srv <- function(id, reporter, rmd_yaml_args = list(
author = "NEST", title = "Report",
date = as.character(Sys.Date()), output = "html_document"
)) {
checkmate::assert_class(reporter, "Reporter")
shiny::moduleServer(
id,
function(input, output, session) {
ns <- session$ns

teal.reporter::reset_report_button_srv("resetButtonPreviewer", reporter)

output$pcards <- shiny::renderUI({
reporter$get_reactive_add_card()
input$card_remove_id
input$card_down_id
input$card_up_id

cards <- reporter$get_cards()
cards_names <- names(cards)

if (length(cards)) {
shiny::tags$div(
class = "panel-group", id = "accordion",
lapply(seq_along(cards), function(ic) {
shiny::tags$div(
id = paste0("panel_card_", ic),
class = "panel panel-default",
shiny::tags$div(
class = "panel-heading", style = "overflow:auto;",
shiny::tags$h4(
class = "panel-title",
shiny::tags$span(
shiny::tags$span(
class = "preview_card_control",
nav_previewer_icon(name = "card_remove_id", icon_name = "times", idx = ic, size = 1),
nav_previewer_icon(name = "card_up_id", icon_name = "arrow-up", idx = ic, size = 1),
nav_previewer_icon(name = "card_down_id", icon_name = "arrow-down", idx = ic, size = 1)
),
shiny::tags$a(
class = "accordion-toggle",
style = "display: block;padding: 10px 15px;margin: -10px -15px;",
`data-toggle` = "collapse", `data-parent` = "#accordion", href = paste0("#collapse", ic),
shiny::tags$h4(paste0("Card ", ic, ": ", cards[[ic]]$get_name()), shiny::icon("caret-down"))
)
)
)
),
shiny::tags$div(
id = paste0("collapse", ic), class = "panel-collapse collapse out",
shiny::tags$div(
class = "panel-body",
shiny::tags$div(
id = paste0("card", ic),
lapply(
cards[[ic]]$get_content(),
function(b) {
resolve_block_to_html(b, cards_names[ic])
}
)
)
)
)
)
})
)
} else {
shiny::tags$p(style = "color:red;", shiny::tags$strong("No Cards added"))
}
})
Polkas marked this conversation as resolved.
Show resolved Hide resolved

shiny::observeEvent(input$card_remove_id, {
reporter$remove_cards(input$card_remove_id)
})

shiny::observeEvent(input$card_up_id, {
if (input$card_up_id > 1) {
reporter$swap_cards(
as.integer(input$card_up_id),
as.integer(input$card_up_id - 1)
)
}
})

shiny::observeEvent(input$card_down_id, {
if (input$card_down_id < length(reporter$get_cards())) {
reporter$swap_cards(
as.integer(input$card_down_id),
as.integer(input$card_down_id + 1)
)
}
})

output$download_data_prev <- shiny::downloadHandler(
filename = function() {
paste("report_", format(Sys.time(), "%y%m%d%H%M%S"), ".zip", sep = "")
},
content = function(file) {
shiny::showNotification("Rendering and Downloading the document.")
input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]])
names(input_list) <- names(rmd_yaml_args)
report_render_and_compress(reporter, input_list, file)
},
contentType = "application/zip"
)
}
)
}

resolve_block_to_html <- function(b, name) {
Polkas marked this conversation as resolved.
Show resolved Hide resolved
block_class <- class(b)[1]
b_content <- b$get_content()
switch(block_class,
TextBlock = {
switch(b$get_style(),
header1 = shiny::tags$h1(b_content),
header2 = shiny::tags$h2(b_content),
header3 = shiny::tags$h3(b_content),
header4 = shiny::tags$h4(b_content),
verbatim = shiny::tags$pre(b_content),
b_content
)
},
PictureBlock = shiny::tags$img(src = knitr::image_uri(b_content)),
TableBlock = {
b_table <- readRDS(b_content)
shiny::tags$pre(
paste(utils::capture.output(print(b_table)), collapse = "\n")
)
},
NewpageBlock = shiny::tags$br(),
""
)
}

add_previewer_css <- function() {
shiny::tags$head(shiny::tags$style("
span.preview_card_control i:hover {
color: blue;
}
"))
}

add_previewer_js <- function(ns) {
shiny::tags$head(shiny::tags$script(
sprintf('
$(document).ready(function(event) {
$("body").on("click", "span.card_remove_id", function() {
var val = $(this).data("cardid");
let msg_confirm = "Do you really want to remove the card " + val + " from the Report?";
var answer = confirm(msg_confirm);
if (answer) {
Shiny.setInputValue("%s", val, {priority: "event"});
$("#panel_card_" + val).remove();
}
});

$("body").on("click", "span.card_up_id", function() {
var val = $(this).data("cardid");
Shiny.setInputValue("%s", val, {priority: "event"});
});

$("body").on("click", "span.card_down_id", function() {
var val = $(this).data("cardid");
Shiny.setInputValue("%s", val, {priority: "event"});
Polkas marked this conversation as resolved.
Show resolved Hide resolved
});
})
', ns("card_remove_id"), ns("card_up_id"), ns("card_down_id"))
))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like idea of not creating observe for each remove/swap button!

}

nav_previewer_icon <- function(name, icon_name, idx, size = 1L) {
checkmate::assert_string(name)
checkmate::assert_string(icon_name)
checkmate::assert_int(size)

shiny::tags$span(
class = name, `data-cardid` = idx,
style = "float:right;margin-left:10px;margin-right:10px;margin-top:10px;color:#337ab7;",
shiny::icon(icon_name, sprintf("fa-%sx", size))
)
}
21 changes: 20 additions & 1 deletion R/ReportCard.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,31 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
#'
get_chr_converters = function() {
private$chr_converters
},
#' @description get the Card name
#'
#' @return `character` a Card name
#' @examples
#' ReportCard$new()$set_name("NAME")$get_name()
get_name = function() {
private$name
},
#' @description set the Card name
#'
#' @param name `character` a Card name
#' @return invisibly self
#' @examples
#' ReportCard$new()$set_name("NAME")$get_name()
set_name = function(name) {
checkmate::assert_string(name)
private$name <- name
invisible(self)
}
),
private = list(
content = list(),
chr_converters = list(),

name = character(0),
# @description The copy constructor.
#
# @param name the name of the field
Expand Down
Loading