Per G. Østerlie
Per G. Østerlie

Reputation: 13

Display text and data R markdown shiny

Yesterday I tried to write my first interactive markdown document with R studio and finally it worked ok, but I can not figure out how to access the data inside a render plot(). It is probably very easy, but I can not find the right examples. May be some kind soul will help me?

I am trying to illustrate the central limit theorem with this code

---
title: "Eksperimentering med utvalg"
author: "Per G. Østerlie"
date: "15 3 2017"
output: html_document
runtime: shiny
---

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


# CLT


```{r}
fisk_masse =sample(1:1000,500,replace=TRUE)

```
```{r histogram, echo=FALSE}
hist(fisk_masse, 
     main="Masse i populasjon", 
     xlab="Masse", 
     ylab="Antall",
     border="darkgray", 
     col="lightgray",
     xlim=c(0,1000),
     las=1, 
     breaks=10)
```

```{r, echo=FALSE}
paste("Gjennomsnitt: ",mean(fisk_masse))
paste("Standardavvik: ",sd(fisk_masse))
```

# Sample

50 fish, choose number of samples

```{r, echo=FALSE}
inputPanel(
  sliderInput("n_utvalg", label = "Antall utvalg",
              min = 50, max = 800, value = 100, step = 50)
)
renderPlot({
xbar = rep(0,input$n_utvalg)
for (i in 1:input$n_utvalg){xbar[i]=mean(sample(fisk_masse,50,replace=FALSE))}
hist(xbar, 
     main="Gjennomsnittsmasse i stikkprovene", 
     xlab="Masse", 
     ylab="Antall",
     border="darkgray", 
     col="lightgray",
     #xlim=c(0,1000),
     las=1, 
     breaks=10)

m <-mean(xbar)
std<-sqrt(var(xbar))

})

Now I would like to display the variables m (or mean(xbar) and std. I would like to have them displayed under the histogram. All my attempts end up with a message that xbar is not known (which make sense). Can anyone direct me to an answer?

Per

Upvotes: 1

Views: 1076

Answers (1)

Robert Hawlik
Robert Hawlik

Reputation: 472

In Shiny, you will have to render text similarly to how you render a plot when the input is dynamic. So additionally to renderPlot() you will also have to add a renderText() part to your last chunk.

I recommend computing xbar in a so called reactive conductor. With the help of the reactive conductor you only have to compute xbar once for the plot and the text output.

You can replace your last chunk with this working example:

```{r, echo=FALSE}

    inputPanel(
      sliderInput("n_utvalg", label = "Antall utvalg",
                  min = 50, max = 800, value = 100, step = 50)
    ) 
  computeXbar <- reactive({
    xbar = rep(0,input$n_utvalg)
    for (i in 1:input$n_utvalg){xbar[i]=mean(sample(fisk_masse,50,replace=FALSE))}
    xbar
  })

  renderPlot({
    xbar <- computeXbar()
    hist(xbar,
         main="Gjennomsnittsmasse i stikkprovene",
         xlab="Masse",
         ylab="Antall",
         border="darkgray",
         col="lightgray",
         #xlim=c(0,1000),
         las=1,
         breaks=10)

    })

  renderText({
    xbar <- computeXbar()
    paste(
      "The mean is: ", mean(xbar), " and the standard deviation is ", sqrt(var(xbar))
    )
  })

```

You can find more information about Shiny reactivity in this overview article: https://shiny.rstudio.com/articles/reactivity-overview.html

Upvotes: 2

Related Questions