I am trying to calculate and plot % yield of some data based on user definable inputs. I am using rmarkdown and shiny to do this. I keep getting stuck when passing a reactive subset through ddply to count the number of rows in the subset.."invalid (null) left side of assignment".
Here is an example data set:
---
title: "Yield3"
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}
library(plyr)
library(ggplot2)
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)
# reactive subset of data based on user input of sliders
pass <- reactive({subset(df, Measurement > ML() & Measurement < MU())})
# Count number of rows in complete data set
ac <- ddply(df, c("Batch", "ID"), function(x) nrow(x))
colnames(ac) <- c("Batch", "ID", "Total")
# Count number of row in passed data set (reactive because inputs are reactive)
bc <- reactive({ddply(pass(), c("Batch", "ID"), function(x) nrow(x))})
colnames(bc()) <- c("Batch", "ID", "Pass")
# Calculate yield by dividing passed by total rows (also reactive)
bc()$Yield <- (bc()$Pass / ac$Total) * 100
# Plot yield by against ID number grouped by batch
renderPlot({ggplot(bc(), aes(ID, Yield, colour=Batch)) + geom_point()})
I have read I think all of the other questions based on reactive subsetting in shiny. This one I think is the closest (R Shiny reactive subset data - ERROR object of type 'closure' is not subsettable) but I still cant put 2 and 2 together and its driving me crazy. Also I have read this (Error in <my code> : target of assignment expands to non-language object) which suggests i am assigning a value to a variable that doesnt exist but I cant see it. Please could someone point out my glaring error or even perhaps a more elegant way to calculate yield. Many thanks