[英]Loop through reactive data.frame in r markdown document
我有以下r markdown文件:
---
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。 我不知道我在做什么錯。 請幫助我,我已經堅持了很長時間。
您的2個selectInput
的id是相同的。 我將它們重命名為Experiment1
和Experiment2
,現在應該可以了:
# 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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.