Output of Shiny app is not rendered in Quarto

Viewed 105

I am trying to include a simple Shiny app in a Quarto website. The expected output is a string with the score and risk, based on some simple conditions. Instead, the textOutput is not rendered. No error message is returned.

_quarto.yml:

project:
  type: website

website:
  title: "name"
  sidebar:
    style: "docked"
    search: true
    contents:
      - index.qmd
      - section: "Chapter"
        contents:
          - app.qmd

format:
  html:
    theme: cosmo
    css: styles.css
    toc: true
  
bibliography: references.bib

editor: visual

index.qmd

# Introduction {.unnumbered}

"A towel, \[The Hitchhiker's Guide to the Galaxy\] says, ..."

app.qmd

---
title: app
format: html
server: shiny
---


```{r}
#| panel: fill
checkboxInput("v1", label = "condition 1", value = F)
checkboxInput("v2", label = "condition 2", value = F)  
checkboxInput("v3", label = "condition 3", value = F)
checkboxInput("v4", label = "condition 4", value = F)
textOutput("out")
```

```{r}
#| context: server
output$out <- renderText({
  risk_tbl <- data.frame(score = c(0:4), 
             risk = c("0.6%", "3.6%", "9.8%", "34.8%", "46.2%"))
  score <- sum(c(input$v1, input$v2, input$v3, input$v4))
  risk <- risk_tbl[risk_tbl$score == score,]$risk
  paste("The score is", score, "with a risk of", risk, sep = " ")
})
```

Output after rendering to html:

1 Answers

You are not getting the output because you are trying to render the shiny app on a website. While the shiny app needs to run the code of the server part to compute the output and to print the output in the textOutput, but the website is static and can not run any code and will not respond to any kind of user input.

So when you are trying to render the shiny app on the website, you will only see the static part of your app (the HTML components and CSS styling) but not any interactive part of the shiny app.

Quoting from this answer on the Github discussion

The Quarto website is ultimately a static website - it won't be responding to user input or running any R code. You can embed a shiny app into a qmd that you render in the rstudio IDE and it'll work just fine because the shiny app is running in a session on your computer. Website hosting services like quarto-pub, netlify, and gh-pages will only be serving up the html pages of the site, not running the code necessary to operate the shiny app in response to user input.

One workaround is to put an iframe into your website that points that page of the website to your shiny app as hosted on a shiny server like shinyapps.io

Related