This is my first trial with golem and I would like to generate an R-Markdown report as in Shiny
I use PC R-Studio 1.41106. When running it and click on 'Generate Report' button I got this error message.
run_app()
Loading required package: shiny
Warning: package ‘shiny’ was built under R version 4.1.2
Listening on http://127.0.0.1:20926
Warning in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="www/report.Rmd": The system cannot find the path specified
Warning: Error in abs_path: The file 'www/report.Rmd' does not exist.
[No stack trace available]
Could somebody what is the correct way to do this?
Below are my app_ui.R, app_server.R and the module mod_markdown.R (my report.Rmd file is in inst/app/www/report.Rmd).
Best.
Ha
app_UI.R
The application User-Interface
@param request Internal parameter for {shiny}. DO NOT REMOVE. @import shiny @noRd
app_ui <- function(request) {
tagList(
# Leave this function for adding external resources
golem_add_external_resources(),
# Your application UI logic
mod_rmarkdown_ui("rmarkdown_1")
)
}
Add external Resources to the Application
This function is internally used to add external resources inside the Shiny application.
@import shiny @importFrom golem add_resource_path activate_js favicon bundle_resources @noRd
golem_add_external_resources <- function() {
add_resource_path(
"www",
app_sys("app/www")
)
tags$head(
favicon(),
bundle_resources(
path = app_sys("app/www"),
app_title = "Sandbox"
)
# Add here other external resources
# for example, you can add shinyalert::useShinyalert()
)
}
############
############ app_server.R
############
The application server-side
@param input,output,session Internal parameters for {shiny}. DO NOT REMOVE. @import shiny @noRd
app_server <- function(input, output, session) {
Your application server logic
mod_rmarkdown_server("rmarkdown_1")
}
##########
########## Module mod_rmarkdown_ui.R
##########
rmarkdown UI Function
@description A shiny Module.
@param id,input,output,session Internal parameters for {shiny}.
@noRd
@importFrom shiny NS tagList
mod_rmarkdown_ui <- function(id){
ns <- NS(id)
tagList(
includeMarkdown(
system.file("app/www/report.Rmd",
package="Sandbox")
),
fluidPage(
h1("Sandbox"),
sliderInput(ns("slider"), "Slider", 1, 100, 50),
downloadButton(ns("report"), "Generate report")
) )
}
rmarkdown Server Functions
@noRd
mod_rmarkdown_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "ReportTemplate.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
#tempReport <- file.path(tempdir(), "report.Rmd")
#file.copy("ReportTemplate.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(n = input$slider)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render("www/report.Rmd", output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
})
}
To be copied in the UI
mod_rmarkdown_ui("rmarkdown_1")
To be copied in the server
mod_rmarkdown_server("rmarkdown_1")
Created on 2022-07-04 by the reprex package (v2.0.1)