2016-11-11 3 views
0

ユーザーが入力値を変更するたびに複数のリアクティブ値を更新する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

@ジオバニーこれを見てください。助けてくれてありがとう – sajjad6al

関連する問題