Reactive subset in ddply for rmarkdown shiny

Pete900

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

GPierre

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()})

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Shiny Reactive 사용

분류에서Dev

Reactive Shiny Application

분류에서Dev

Rmarkdown 및 Shiny 입력

분류에서Dev

Rmarkdown 및 Shiny 입력

분류에서Dev

Shiny Reactive SQL : WHERE 절

분류에서Dev

Shiny를 사용하여 RMarkdown의 로컬 html 파일에 연결

분류에서Dev

R Shiny Reactive 입력 파일 (조건 포함)

분류에서Dev

RStudio 및 Shiny : Reactive를 사용하여 reactValues 정의

분류에서Dev

passing strings as arguments in ddply

분류에서Dev

Passing a function argument to ddply

분류에서Dev

yaml :: yaml.load (..., eval.expr = TRUE) 오류 : Rmarkdown에서 shiny를 사용할 때

분류에서Dev

ggplot 플롯을 렌더링 할 때 RMarkdown Shiny가 충돌합니다.

분류에서Dev

ddply 및 열 추가

분류에서Dev

ddply 속도 향상

분류에서Dev

ksmooth 함수에 ddply

분류에서Dev

shinyapps.io에서 호스팅 될 때 shiny Rmarkdown selectInput이 작동하지 않음-옵션에 <a0> 포함

분류에서Dev

rmarkdown Shiny 앱 내에서 handsontable에 포함 된 로컬 오디오를 재생할 수 없습니다.

분류에서Dev

plyr :: ddply를 dplyr로 변환

분류에서Dev

열 이름 ddply 설정

분류에서Dev

R : ddply가있는 for 루프

분류에서Dev

반복, ddply 및 개수

분류에서Dev

ddply 함수가있는 if 문

분류에서Dev

R ddply 행 요약 통계

분류에서Dev

Reactive 기반의 필터링 된 가시 데이터로 제출 후 Shiny Datatable을 업데이트하는 방법은 무엇입니까?

분류에서Dev

R 스프레드 ddply fivenum 결과

분류에서Dev

ddply 및 간격 혼합시 오류

분류에서Dev

ddply 비례 카운트 요약

분류에서Dev

그룹 별 ddply 다중 분위수

분류에서Dev

使用不同的变量类时,ddply的输出奇怪