Issues using CSS style sheet when rendering Quarto document from Shinyapp. #6105
-
Description@cderv provided some initial help and I was able to get my shiny app working so that it renders the report (both when hosted locally and on shinyapps.io. My last issue is getting Quarto to use my external CSS style sheet. Any help would be appreciated! The App:library(shiny)
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
library(gt)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(qbr)
library(tidyverse)
library(quarto)
ui <- fluidPage(
titlePanel("My App"),
sidebarLayout(
sidebarPanel(width = 3,
textInput(inputId = "user.string", label = "Input string:"),
br(),
downloadButton(outputId = "report", label = "Generate Report:"),
br()
),
mainPanel(
)
)
)
server <- function(input, output) {
output$report <- downloadHandler(
filename = "final_report.html",
content = function(file) {
temp_qmd <- file.path(tempdir(), "checklist.qmd")
file.copy("checklist.qmd", temp_qmd, overwrite = TRUE)
temp_css <- file.path(tempdir(), "markdown-style.css")
file.copy("markdown-style.css", temp_css, overwrite = TRUE)
quarto::quarto_render(
temp_qmd,
output_format = "html",
output_file = "rendered_report.html",
execute_params = list(userkey = input$user.string)
)
file.copy("rendered_report.html", file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
#>
#> Listening on http://127.0.0.1:8241 Created on 2023-07-04 with reprex v2.0.2 The Quarto Doc ("checklist.qmd"):
The CSS sheet ("markdown-style.css"):@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,300;0,500;1,300;1,500&display=swap');
body {
font-family: 'Roboto', sans-serif;
font-weight: 300;
font-size: 14pt;
}
h1 {
text-align: center;
} Updates:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Could format your Quarto document using more backticks around it? You can share a Quarto document using the following syntax, i.e., using more backticks than you have in your document (usually four ````qmd
---
title: "Reproducible Quarto Document"
format: html
---
This is a reproducible Quarto document using `format: html`.
It is written in Markdown and contains embedded R code.
When you run the code, it will produce a plot.
```{r}
plot(cars)
```
The end.
```` |
Beta Was this translation helpful? Give feedback.
-
I think this is because you are using a However, you need to take care of how and where your files are rendered, accordingly to how It is best to do everything in the same folder really. And then move files yourself. See how I modify your server part server <- function(input, output) {
output$report <- downloadHandler(
filename = "final_report.html",
content = function(file) {
# create a temp dir for your app
dir.create(temp_dir <- tempfile("dir-shinyapps-"))
on.exit(unlink(temp_dir, recursive = TRUE), add = TRUE)
# Copy resources there
file.copy("checklist.qmd", temp_dir, overwrite = TRUE)
file.copy("markdown-style.css", temp_dir, overwrite = TRUE)
# Run Quarto in that directory with resource embedding and
# and output to the temp file
xfun::in_dir(temp_dir, {
quarto::quarto_render(
"checklist.qmd",
output_format = "html",
output_file = "rendered_report.html",
execute_params = list(user.string = input$user.string)
)
file.copy("rendered_report.html", file)
})
}
)
} Key parts are :
Please study this to understand the logic on how path works with rendering. Hope it helps and serve as example to build upon. |
Beta Was this translation helpful? Give feedback.
I think this is because you are using a
downloadHandler()
so this will offer the user to download the HTML.However it does not offer to download a folder, so the CSS will not be downloaded, and the the HTML report will be missing the resource file. Using
embed-resource: true
could help with that.However, you need to take care of how and where your files are rendered, accordingly to how
quarto_render()
is working - You are running the function from a R working dir that is different than where your file really are, and currently output the file in another directory.It is best to do everything in the same folder really. And then move files yourself.
See how I modify your server part