Reactive subset in ddply for rmarkdown shiny

旧城冷巷雨未停 提交于 2020-01-25 00:34:27

问题


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


回答1:


First, you are trying to modify a reactive object outside the reactive expression. I would suggest to define column names inside the expression.

Second, I don't think that modifying bc()$Yield is an authorized operation. So I would try do generate Yield also inside a reactive expression.

Below is an edited piece of your code. It generates an output without errors. You will probably have to tweak it a little bit more. (I think bcand bc2 could be merged).

# Count number of row in passed data set (reactive because inputs are reactive)
bc <- reactive({
  a<-ddply(pass(), c("Batch", "ID"), function(x) nrow(x))
  colnames(a) <- c("Batch", "ID", "Pass")
  return(a)
  })

# Calculate yield by dividing passed by total rows (also reactive)
bc2 <- reactive({
  a<-(bc()$Pass / ac$Total) * 100
  a<-cbind(a,bc())
  colnames(a)<- c("Yield","Batch", "ID", "Pass")
  return(a)
 })

# Plot yield by against ID number grouped by batch
renderPlot({ggplot(bc2(), aes(ID, Yield)) + geom_point()})


来源:https://stackoverflow.com/questions/30455402/reactive-subset-in-ddply-for-rmarkdown-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!