1

I am trying to combine the answers to the following two questions:

Reactive subset in ddply for rmarkdown shiny

Maintain data frame rows after subet

In the first question I was shown how to properly use reactive to subset in shiny / rmarkdown. I the second I was shown how to use dplry to summarize my data to calculate a % yield. Now I am trying to use dplry with a reactive function so that my % yield can be affected by user inputs. I am almost there but get an error of "unused argument" then a list of numbers. Here is an example:

---
title: "Yield5"
author: "P Downs"
date: "Tuesday, May 26, 2015"
output: html_document
runtime: shiny
---

# Create user input for reactive subsetting
```{r echo=FALSE}
sliderInput("Meas_L", label = "Measure lower bound:",
            min=2, max=9, value=3, step=0.1)

sliderInput("Meas_U", label = "Measure upper bound:",
            min=2, max=9, value=8, step=0.1)

# create reactive variables for use in subsetting below

ML <- reactive({input$Meas_L})
MU <- reactive({input$Meas_U})

```

# Create example data frame. Measurement is grouped by batch and ID number
```{r echo=FALSE, message=FALSE}

library(plyr)
library(ggplot2)
library(dplyr)


set.seed(10)
Measurement <- rnorm(1000, 5, 2)
ID <- rep(c(1:100), each=10)
Batch <- rep(c(1:10), each=100)

df <- data.frame(Batch, ID, Measurement)

df$ID <- factor(df$ID)
df$Batch <- factor(df$Batch)

# function used to count number of "passed" data based on user input from sliders i.e. how many data points are between ML and MU

countFunc <- reactive({ function(x) sum( (x > ML()) & (x < MU()) )})

# user dplyr to produce summary of count for: total data, passed data, then calculate % yield 

totals <- reactive({
  df %>% group_by(Batch, ID) %>%
  summarize(total = length(Measurement), x = countFunc(Measurement)) %>%
  mutate(Yield = (x/total)*100) %>%
  as.data.frame()
  })

# Plot yield by against ID number grouped by batch

renderPlot({ggplot(totals(), aes(ID, Yield, colour=Batch)) + geom_point() +
              scale_y_continuous(limits=c(0,100))})

I cant see why I have an unused argument in the function? I eventually want to extend the function to more the one variable but I will save that for another day!

Community
  • 1
  • 1
Pete900
  • 2,016
  • 1
  • 21
  • 44

1 Answers1

2

A reactive is not a function and you cannot pass arguments to a reactive. Your function countFunc should be a function, not a reactive. Then you call the function with the appropriate (reactive) values.

countFunc <- function(x, ml, mu) sum( (x > ml) & (x < mu) )

totals <- reactive({
  df %>% group_by(Batch, ID) %>%
  summarize(total = length(Measurement), x = countFunc(Measurement, ML(), MU())) %>%
  mutate(Yield = (x/total)*100) %>%
  as.data.frame()
  })
shadow
  • 21,823
  • 4
  • 63
  • 77
  • Absolutely fantastic, thank you! And now I can see clearly how to extend the function for more variables, I just need to define them in the function(x, y, z...) bit then call them in the countFunc. – Pete900 May 27 '15 at 09:47