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

[FR] shiny server context code echoing #2524

Open
Karel-Kroeze opened this issue Oct 23, 2023 · 1 comment
Open

[FR] shiny server context code echoing #2524

Karel-Kroeze opened this issue Oct 23, 2023 · 1 comment

Comments

@Karel-Kroeze
Copy link

I extensively use .rmd/.qmd documents to create reports, but also to write tutorials for workshops and lectures. Echoing code is a standard feature that helps me do this.

The problem I encounter when writing tutorials for shiny workshops, is that I cannot find a (good1) way to echo the server-side code. I would like some way to easily include server-side code for inline shiny widgets in a rendered markdown document (obviously using the shiny runtime).

The best case scenario would be an embedded version of the code view as used in shiny's 'showcase' mode.
Realistically, just echoing server context chunks as a plain code block would already be a huge quality of live upgrade for me.

I've searched the rmarkdown and shiny documentation without any results, and a broader search only found vague answers explaining that this is not possible "because the server code runs in a different context"2. I suspect that answer misunderstood the question, because I don't understand why a server context code block could not be echoed during rendering, just as any other code block? I understand the outputs (and reactive updates for the showcase) are not available during render, but the code itself is?

In addition, given that a shiny document requires a backing shiny server anyway, why could 'live' content not be injected at runtime, just as regular inline shiny widgets are? Given the motivation, time, and resources, it should be theoretically possible to render and embed a special version of shiny's showcase mode for inline server-side code, no?

If there are intricacies of the render pipeline that make this impossible, could you (point me to resources that) explain this more in depth?

Footnotes

  1. I've resorted to including a duplicate of the server code in a plain code block, but that's ugly and inconvenient.

  2. I can't seem to find the stack overflow question where this was referenced anymore, apologies.

@gadenbuie
Copy link
Member

You should be able to echo the server code by setting echo = TRUE. Here's an example using the new Shiny Rmd template:

---
title: "Issue 2524"
output: html_document
runtime: shiny
---

## Inputs and Outputs

You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change.  This demonstrates how a standard R plot can be made interactive by wrapping it in the Shiny `renderPlot` function. The `selectInput` and `sliderInput` functions create the input widgets used to drive the plot.

```{r eruptions, echo = FALSE}
inputPanel(
  selectInput("n_breaks", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20),
  sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)
)
```

```{r eruptions-server, echo = TRUE, context = "server"}
renderPlot({
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")
  
  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  lines(dens, col = "blue")
})
```

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants