gattuso
gattuso

Reputation: 99

Include reactive text in a R markdown shiny documents

I am a little lost and am unable to add a reactive test (te) in the shiny output of an R markdown document. A minimal example based on an R studio example is paste below.

Many thanks in advance! Jean-Pierre

---
title: "Untitled"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{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)
)

renderText({te})

renderPlot({
startTime <- Sys.time()
  # additional code goes here
endTime <- Sys.time() +1
te <- reactive(startTime - endTime)
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")
})
```

Upvotes: 0

Views: 443

Answers (1)

Yang
Yang

Reputation: 191

I think you should use te <<- reactive(startTime - endTime) to define te outside the renderPlot, use renderText({te()}) instead of renderText({te}) because it is a reactive expression, and finally put renderText({te()}) to the end after it's definition.

Upvotes: 1

Related Questions