2017-08-03 7 views
1

reactalValueとグローバル変数の違いは何ですか?グローバル変数とリアクティブ値の差

私は、正しい答えを見つけることができません:/と私は私の次のスクリプトでは、問題を持っている:

shinyServer(function(input, output, session) { 
    global <- list() 
    observe({ 
    updateSelectInput(session, "choixDim", choices = param[name == input$choixCube, dim][[1]]) 
    updateSelectInput(session, "choixMes", choices = param[name == input$choixCube, mes][[1]]) 
    }) 

    output$ajoutColonneUi <- renderUI({ 
    tagList(
     if(input$ajoutColonne != "Aucun"){ 
     textInput("nomCol", "Nom de la colonne créée") 
     }, 
     switch(input$ajoutColonne, 
      "Ratio de deux colonnes" = tagList(
       selectInput("col1", label = "Colonne 1", choices = input$choixMes), 
       selectInput("col2", label = "Colonne 2", choices = input$choixMes) 
      ), 
      "Indice base 100" = selectInput("col", label = "Colonne", choices = input$choixMes), 
      "Evolution" = selectInput("col", label = "Colonne", choices = input$choixMes) 
    ) 
    ) 
    }) 

    observeEvent(input$chargerCube,{ 
    debutChargement() 
    global <- creerCube(input) 
    global <- ajouterColonne(global, input) 
    finChargement() 

    if(!is.null(global)){ 
     cat('Cube chargé avec succés ! \n') 
     output$handlerExport <- downloadHandler(
     filename = function(){ 
      paste0("cube_generated_with_shiny_app",Sys.Date(),".csv") 
     }, 
     content = function(file){ 
      fwrite(global$cube, file, row.names = FALSE) 
     } 
    ) 
     output$boutons <- renderUI({ 
     tagList(
      downloadButton("handlerExport", label = "Exporter le cube"), 
      actionButton("butValider", "Rafraichir la table/le graphique") 
     ) 
     }) 
    } 
    }) 

    observeEvent(input$butValider,{ 
    output$pivotTable <- renderRpivotTable({ 
     cat('test') 
     rpivotTable(data = global$cube, aggregatorName = "Sum", vals = global$mes[1], cols = global$temp) 
    }) 
    }) 

}) 

グローバルが更新されていない私は、これらのデータからrpivotTableを表示したいとき...

+0

反応変数は有用ですが、グローバル変数の値を更新する必要がある場合は、反応式やサブ関数内で '< - 'の代わりに '< - '演算子を使用してください。 – Geovany

答えて

4

reactValueは、オブザーバーまたはobserveEventから更新できるものです。その値に依存する反応式が無効になり、必要に応じて更新されます。

グローバル変数はグローバルに定義された変数です。つまり、あなたの輝くAppのプロセスにいるすべてのユーザーがその変数を共有します。これの最も簡単な例は、大きなデータセットです。

グローバル変数はグローバルではありません。サーバー内

global <- list() 

あなたはとして定義します。そのため、アプリ内の1人のユーザーに固有のものであり、共有されていません。すなわち、それが

global <- runif(1) 

の場合、複数のユーザーの場合は「global」の数字が異なります。値を同じにするには、サーバー定義の上に値を初期化する必要があります。

global <- creerCube(input) 

は、範囲外であるため、この変数は変更されません。オブザーバ内に変数「グローバル」を作成し、関数が終了するとその変数を破棄します。

global <- reactiveVal() 

として、それを更新します:何が最善であることはreactiveValueとしてグローバルに設定することで、私はこのことができます願ってい

global(creerCube(input)) 

関連する問題