ddply 中用于 rmarkdown 闪亮的反应性子集 [英] Reactive subset in ddply for rmarkdown shiny

查看:27
本文介绍了ddply 中用于 rmarkdown 闪亮的反应性子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户可定义的输入来计算和绘制某些数据的产量百分比.我正在使用 rmarkdown 和闪亮来做到这一点.在通过 ddply 传递反应性子集以计算子集中的行数时,我一直陷入困境..分配的左侧无效(空)".

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".

这是一个示例数据集:

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

我已经阅读了我认为所有其他基于闪亮的反应性子集的问题.我认为这是最接近的(R Shiny 反应性子集数据 - 'closure' 类型的 ERROR 对象不可子集) 但我仍然无法将 2 和 2 放在一起,这让我发疯.我也读过这个(Error in<我的代码>:赋值目标扩展到非语言对象),这表明我正在为一个不存在但我看不到的变量赋值.请有人指出我的明显错误,甚至可能是一种更优雅的计算产量的方法.非常感谢

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

推荐答案

首先,您试图在反应式表达式之外修改反应式对象.我建议在表达式中定义列名.

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

其次,我认为修改bc()$Yield 不是授权操作.所以我会尝试在反应式表达式中生成 Yield.

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.

下面是一段经过编辑的代码.它生成一个没有错误的输出.您可能需要稍微调整一下.(我认为 bcbc2 可以合并).

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

这篇关于ddply 中用于 rmarkdown 闪亮的反应性子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆