循环遍历r markdown文档中的反应性data.frame

时间:2016-09-29 11:48:34

标签: r shiny r-markdown

我有以下r降价文件:

---
title: ""
author: ""
date: ""
output: html_document
runtime: shiny
---

```{r include=FALSE, cache=FALSE}
library(knitr)
library(rmarkdown)
library(plyr)
library(dplyr)
library(plotly)
```

```{r Compute, echo=FALSE}
# load data frame:
abTestingData <- data.frame("Experiment"=c("original", "original","v1","v1"),
                        "Day"=c("D1","D1","D1","D1"),
                        "N"=c(24965,24965,23962,23962),
                        "n"=c(428,195,387,465),
                        "MonitoredParam"=c("par1","par2","par3","par4"))

abTestingData$Experiment <- as.character((abTestingData$Experiment))
# Compute proportions
abTestingData$Proportion <- abTestingData$n/abTestingData$N
noExperiments <- 2

# Input 1
selectInput("Experiment","Choose the first experiment",
        choices = unique(abTestingData$Experiment),
        selected = unique(abTestingData$Experiment)[1])
abTestingData_SelectedExperiment <-     reactive(as.data.frame(subset(abTestingData, Experiment == input$Experiment)))
firstExperiment <- reactive(abTestingData_SelectedExperiment())

# Input 2
selectInput("Experiment","Choose the second experiment",
        choices = unique(abTestingData$Experiment),
        selected = unique(abTestingData$Experiment)[2])
abTestingData2_SelectedExperiment <- reactive(as.data.frame(subset(abTestingData, Experiment == input$Experiment)))
secondExperiment <- reactive(abTestingData2_SelectedExperiment())

dayGroup <- table(abTestingData$Day)[[1]]/noExperiments

data <- reactive({

noRows <- dim(firstExperiment())[1]

data3 <- data.frame("Day"=c(rep(NA, noRows)),
                "Value"=c(rep(NA, noRows)),
                "CriticalRange"=c(rep(NA, noRows)),
                "Significant"=c(rep(NA, noRows)),
                "BubbleText"=c(rep(NA, noRows)),
                "BubbleSymbol"=c(rep(NA, noRows)))


# And critical part - loop through reactive data frames
for(i in 1:dim(firstExperiment())[1]){
  data3$Value[i] <- abs(firstExperiment()$Proportion[i] -  secondExperiment()$Proportion[i])
}

return(data3)

})

renderTable({
  head(data(), 24)
})
```

最后一个循环不计算两个反应数据帧之间的差异。价值,因为差异总是0.00。我不知道,我做错了什么。请帮助我,我已经坚持了很长时间。

1 个答案:

答案 0 :(得分:0)

你的2 selectInput的id-s是相同的。我将它们重命名为Experiment1Experiment2,现在应该没问题了:

# Input 1
selectInput("Experiment1","Choose the first experiment",
            choices = unique(abTestingData$Experiment),
            selected = unique(abTestingData$Experiment)[1])
abTestingData_SelectedExperiment <-     reactive(as.data.frame(subset(abTestingData, Experiment == input$Experiment1)))
firstExperiment <- reactive(abTestingData_SelectedExperiment())

# Input 2
selectInput("Experiment2","Choose the second experiment",
            choices = unique(abTestingData$Experiment),
            selected = unique(abTestingData$Experiment)[2])
abTestingData2_SelectedExperiment <- reactive(as.data.frame(subset(abTestingData, Experiment == input$Experiment2)))
secondExperiment <- reactive(abTestingData2_SelectedExperiment())
相关问题