2828# ' The file must be accessible from the Shiny app environment.
2929# ' @param allow_download (`logical`) whether to allow downloading of the R Markdown file.
3030# ' Defaults to `TRUE`.
31+ # ' @param extra_transform (`list`) of [teal::teal_transform_module()] that will be added in the module's UI.
32+ # ' This can be used to create interactive inputs that modify the parameters in R Markdown rendering.
3133# '
3234# ' @inherit shared_params return
3335# '
6163# ' shinyApp(app$ui, app$server)
6264# ' }
6365# '
66+ # ' @examples
67+ # ' static_decorator <- teal_transform_module(
68+ # ' label = "N Rows selector",
69+ # ' ui = function(id) {
70+ # ' ns <- NS(id)
71+ # ' tags$div(
72+ # ' numericInput(ns("n_rows"), "Show n rows", value = 5, min = 0, max = 200, step = 5)
73+ # ' )
74+ # ' },
75+ # ' server = function(id, data) {
76+ # ' moduleServer(id, function(input, output, session) {
77+ # ' reactive({
78+ # ' req(data())
79+ # ' within(data(), {
80+ # ' rmd_data$n_rows <- n_rows_value
81+ # ' }, n_rows_value = input$n_rows)
82+ # ' })
83+ # ' })
84+ # ' }
85+ # ' )
86+ # '
87+ # ' app <- init(
88+ # ' data = data,
89+ # ' modules = modules(
90+ # ' tm_rmarkdown(
91+ # ' label = "RMarkdown Module",
92+ # ' rmd_file = "test.Rmd",
93+ # ' allow_download = FALSE,
94+ # ' decorators = list(static_decorator)
95+ # ' )
96+ # ' )
97+ # ' ) |> shiny::runApp()
6498# ' @export
6599# '
66100tm_rmarkdown <- function (label = " RMarkdown Module" ,
@@ -69,7 +103,8 @@ tm_rmarkdown <- function(label = "RMarkdown Module",
69103 allow_download = TRUE ,
70104 pre_output = NULL ,
71105 post_output = NULL ,
72- transformators = list ()) {
106+ transformators = list (),
107+ extra_transform = list ()) {
73108 message(" Initializing tm_rmarkdown" )
74109
75110 # Start of assertions
@@ -89,7 +124,7 @@ tm_rmarkdown <- function(label = "RMarkdown Module",
89124 ans <- module(
90125 label = label ,
91126 server = srv_rmarkdown ,
92- server_args = list (rmd_file = rmd_file , allow_download = allow_download ),
127+ server_args = list (rmd_file = rmd_file , allow_download = allow_download , extra_transform = extra_transform ),
93128 ui = ui_rmarkdown ,
94129 ui_args = args ,
95130 transformators = transformators ,
@@ -100,7 +135,7 @@ tm_rmarkdown <- function(label = "RMarkdown Module",
100135}
101136
102137# UI function for the rmarkdown module
103- ui_rmarkdown <- function (id , rmd_file , allow_download , ... ) {
138+ ui_rmarkdown <- function (id , rmd_file , allow_download , extra_transform , ... ) {
104139 args <- list (... )
105140 ns <- NS(id )
106141
@@ -117,8 +152,8 @@ ui_rmarkdown <- function(id, rmd_file, allow_download, ...) {
117152 sprintf(" Download '%s'" , basename(rmd_file )),
118153 class = " btn-primary btn-sm"
119154 )
120- }
121-
155+ },
156+ ui_transform_teal_data(ns( " extra_transform " ), transformators = extra_transform )
122157 ),
123158 tags $ hr(),
124159 uiOutput(ns(" rmd_output" ))
@@ -130,7 +165,7 @@ ui_rmarkdown <- function(id, rmd_file, allow_download, ...) {
130165}
131166
132167# Server function for the rmarkdown module
133- srv_rmarkdown <- function (id , data , rmd_file , allow_download ) {
168+ srv_rmarkdown <- function (id , data , rmd_file , allow_download , decorators ) {
134169 checkmate :: assert_class(data , " reactive" )
135170 checkmate :: assert_class(isolate(data()), " teal_data" )
136171 moduleServer(id , function (input , output , session ) {
@@ -142,7 +177,7 @@ srv_rmarkdown <- function(id, data, rmd_file, allow_download) {
142177 )
143178 }
144179
145- q_r <- reactive({
180+ pre_decorated_q_r <- reactive({
146181 data_q <- req(data())
147182 teal.reporter :: teal_card(data_q ) <- c(
148183 teal.reporter :: teal_card(data_q ),
@@ -157,9 +192,14 @@ srv_rmarkdown <- function(id, data, rmd_file, allow_download) {
157192 )
158193 })
159194
195+ q_r <- data_with_output_decorated <- teal :: srv_transform_teal_data(
196+ " extra_transform" ,
197+ data = pre_decorated_q_r ,
198+ transformators = extra_transform
199+ )
200+
160201 rendered_path_r <- reactive({
161202 datasets <- req(q_r()) # Ensure data is available
162-
163203 temp_dir <- tempdir()
164204 temp_rmd <- tempfile(tmpdir = temp_dir , fileext = " .Rmd" )
165205 temp_html <- tempfile(tmpdir = temp_dir , fileext = " .md" )
@@ -197,22 +237,25 @@ srv_rmarkdown <- function(id, data, rmd_file, allow_download) {
197237 output $ rmd_output <- renderUI(rendered_html_r())
198238
199239 reactive({
200- out_data <- eval_code(
201- q_r(),
202- paste(
203- sep = " \n " ,
204- sprintf(" ## R Markdown contents are generated from file, please download it from the module UI." ),
205- sprintf(" # rmarkdown::render(%s, params = rmd_data)" , shQuote(basename(rmd_file ), type = " cmd" ))
206- )
207- )
240+ out_data <- q_r()
208241
209- out_data @ verified <- FALSE
242+ if (allow_download ) {
243+ out_data <- eval_code(
244+ q_r(),
245+ paste(
246+ sep = " \n " ,
247+ sprintf(" ## R Markdown contents are generated from file, please download it from the module UI." ),
248+ sprintf(" # rmarkdown::render(%s, params = rmd_data)" , shQuote(basename(rmd_file ), type = " cmd" ))
249+ )
250+ )
251+ out_data @ verified <- FALSE # manual change verified status as code is being injected
252+ }
210253
211254 teal.reporter :: teal_card(out_data ) <- c(
212255 teal.reporter :: teal_card(out_data ),
213256 rendered_html_r()
214257 )
215258 out_data
216- })
217- })
259+ })
260+ })
218261}
0 commit comments