2017-08-03 52 views
1

我想知道reactiveValue和全局变量之间有什么区别。全局变量和无效值之间的差别

我无法找到一个正确的答案:/和我在下面的脚本有一个问题:

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

reactiveValue是您可以从观察者或观察事件更新的东西。任何依赖于该值的反应性表达式都将失效,并在必要时进行更新。

全局变量是一个全局定义的变量,即所有正在闪亮的应用程序中的用户共享该变量。最简单的例子就是一个大的数据集。

请注意,您的'全局'变量不是全局变量。您将其定义为:

global <- list() 

服务器内部。因此,它对于您的应用中的一位用户来说是独一无二的,而不是共享的。即,如果它将是

global <- runif(1) 

'全局'中的数字对于多个用户将是不同的。如果您希望该值相同,则应将其初始化为高于服务器定义。还要注意这条线:

global <- creerCube(input) 

不会修改您的'全局'变量,因为它超出了范围。它会在您的观察者中创建一个变量“全局”,并在函数结束时放弃它。什么是可能是最好是设置全局的reactiveValue:

global <- reactiveVal() 

,然后对其进行更新:

global(creerCube(input)) 

我希望这有助于。

相关问题