2016-11-11 63 views
0

如何设置for循环,每当用户更改输入值时更新多个反应值?For循环计算多个无功变量

例如程序应该读取一个原始文件。然后根据原始文件的长度多次运行for循环,每次更新基于if语句的计数器。 每次用户更新输入值时,计数器都应相应更新。

下面我试着用简化的格式来翻译我想要的东西。

谢谢你们

my_data <- read.csv("whatever_raw_file.csv") 
counter_A <- 0 
counter_B <- 0 

for (i in 1:length(my_data)) { 
    if (my_data[i] > user_input_value) { 
    counter_A <- reactive({ 
     counter_A <- counter_A + 1 
     counter_A 
     }) 
    } 
    else { 
    counter_B <- reactive({ 
     counter_B <- counter_B + 1 
     counter_B 
    }) 
    } 
} 

回答

0

可以使用reactiveValues代替。

喜欢的东西:

my_data <- read.csv("whatever_raw_file.csv") 
counter <- reactiveValues(A = 0, B = 0) 

# inside some reactive expression 
for (i in 1:length(my_data)) { 
    if (my_data[i] > user_input_value) { 
    counter$A <- isolate(counter$A) + 1 
    } else { 
    counter$B <- isolate(counter$B) + 1 
    } 
} 
0

所以这是我的基本应用程序,它改写了我的一部分,它仍然似乎并不奏效。

library(shiny) 
library(shinythemes) 

ui <- fluidPage(theme = shinytheme("sandstone"), 
navbarPage(title = "Project Methylation", 
    tabPanel(title = "Main", 
    wellPanel(
    column(4, 
     sliderInput("lowMethRangeS", "Low methylation range", min=0, max=1, value=c(0,0.4), step=0.01)), 
    column(4, 
     sliderInput("medMethRangeS", "Medium methylation range", min=0, max=1, value=c(0.4,0.6), step=0.01)), 
    column(4, 
     sliderInput("highMethRangeS", "High methylation range", min = 0, max = 1, value = c(0.6,1), step=0.01)))), 
    fluidRow(
    verbatimTextOutput("test"), 
    textOutput("test2") 
     ) 
     ) 
    ) 
    ) 

server <- function(input, output, session) { 

#This chunk of the code will connect the three sliders together to update automatically 
    observe({ 
    val1S <- input$lowMethRangeS 
    val2S <- input$medMethRangeS 
    updateSliderInput(session, "medMethRangeS", value =  c(val1S[2],val2S[2]), min = 0, max = 1, step = 0.01) 
    updateSliderInput(session, "highMethRangeS", value = c(val2S[2],1), min = 0, max = 1, step = 0.01) 
    }) 

    observe({ 
    val1P <- input$lowMethRangeP 
    val2P <- input$medMethRangeP 
    updateSliderInput(session, "medMethRangeP", value = c(val1P[2],val2P[2]), min = 0, max = 1, step = 0.01) 
    updateSliderInput(session, "highMethRangeP", value = c(val2P[2],1), min = 0, max = 1, step = 0.01) 
    }) 

#The next line is just to read the file 
    rawFileCoding <- read.csv(file = "coding.csv", header = TRUE, sep = ",") 

#This is the part I have problem with!!! 
    counters <- reactiveValues(
    CBLcounter = 0, 
    CPLcounter = 0, 
    CCLcounter = 0, 
    CNLcounter = 0 
) 

    for(i in 1:length(rawFileCoding$BEN)){ 
    if(rawFileCoding$BEN[i]>=input$lowMethRangeS[1] && rawFileCoding$BEN[i] <= input$lowMethRangeS[2]){ 
     counters$CBLcounter <- isolate(counters$CBLcounter) + 1 
     counters$CPLcounter <- isolate(counters$CPLcounter) + 1 
     counters$CCLcounter <- isolate(counters$CCLcounter) + 1 
     counters$CNLcounter <- isolate(counters$CNLcounter) + 1 
    } 
    } 

#I just added these two ways to just make sure the loop has worked 
    output$test <- renderPrint ({counters$CBLcounter}) 
    output$test2 <- renderText ({counters$CBLcounter}) 

} 
shinyApp(ui = ui, server = server) 
+0

@Geovany请看看这个。提前谢谢你的帮助 – sajjad6al