使用rmarkdown的闪亮应用程序的dplry处于被动功能

使用rmarkdown的闪亮应用程序的dplry处于被动功能,r,shiny,dplyr,R,Shiny,Dplyr,我试图将以下两个问题的答案结合起来: 在第一个问题中,我被演示了如何在Shining/rmarkdown中正确使用reactive to subset。第二天,我被演示了如何使用dplry总结我的数据,以计算%的收益率。现在我正在尝试使用dplry和反应函数,以便我的%产量可以受到用户输入的影响。我几乎到了,但是得到了一个“未使用的参数”错误,然后是一个数字列表。以下是一个例子: --- title: "Yield5" author: "P Downs" date: "Tuesday, Ma

我试图将以下两个问题的答案结合起来:

在第一个问题中,我被演示了如何在Shining/rmarkdown中正确使用reactive to subset。第二天,我被演示了如何使用dplry总结我的数据,以计算%的收益率。现在我正在尝试使用dplry和反应函数,以便我的%产量可以受到用户输入的影响。我几乎到了,但是得到了一个“未使用的参数”错误,然后是一个数字列表。以下是一个例子:

---
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))})
---
标题:“产量5”
作者:“P Downs”
日期:“2015年5月26日,星期二”
输出:html\u文档
运行时间:闪亮
---
#为反应式子集创建用户输入
```{r echo=FALSE}
滑块输入(“测量”,label=“测量下限:”,
最小值=2,最大值=9,值=3,步长=0.1)
滑块输入(“Meas_”,label=“测量上限:”,
最小值=2,最大值=9,值=8,步长=0.1)
#创建反应性变量,以便在下面的子集中使用

MLA
reactive
不是函数,不能将参数传递给
reactive
。您的函数
countFunc
应该是
函数
,而不是
反应式
。然后使用适当的(反应)值调用函数

countFunc ml)和(x%
汇总(总计=长度(测量值),x=计数函数(测量值,ML(),MU())%>%
突变(产量=(x/总数)*100)%>%
as.data.frame()
})

太棒了,谢谢!现在我可以清楚地看到如何扩展更多变量的函数,我只需要在函数(x,y,z…)位中定义它们,然后在countFunc中调用它们。
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()
  })