martinkabe martinkabe - 2 months ago 25
R Question

Loop through reactive data.frame in r markdown document

I have the following r markdown document:

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


The last loop doesn't compute difference between the both reactive data frames. Value, as a difference is always 0.00. I have no idea, what I'm doing wrong. Please, help me, I've stucked on this for a long time.

GyD GyD
Answer

The id-s of your 2 selectInputs are the same. I renamed them to Experiment1 and Experiment2 and now it should be fine:

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