2016-10-26 3 views
0

オブザーバーを使用してグローバル変数を更新するにはどうすればよいですか?光沢のあるオブザーバ - オブザーバを使ってグローバル変数を更新する方法は?

私は、起動時に空の種があります。

speciesChoices <- c() 

をしかし、私は何かが、それはなるように、UIに変更されたときにそれに値を追加したい:

speciesChoices <- c(
    'PM2.5' = 'particles' 
) 

が、それは可能ですか?

私のコード今のところ...

ui.r:

speciesOptions <- selectInput(
    inputId = "species", 
    label = "Species:", 
    choices = c(
     # 'PM2.5' = 'particles', 
     # 'Humidity %' = 'humidity' 
    ) 
) 

server.r:

speciesChoices <- c() # global variable 

# Define server logic required to draw a histogram 
shinyServer(function(input, output, session) { 

    observe({ 
     key <- input$streams1 

     stream <- fromJSON(paste(server, "/output/stream?public_key=", key, sep=""),flatten=TRUE) 
     species <- as.data.frame(stream$species) 

     # Set choices of title and code for select input. 
     speciesChoices <- setNames(species$code_name, species$public_name) 

     updateSelectInput(session, "species", choices = speciesChoices) 
    }) 

}) 

それだけでspeciesChoices UIの入力を更新しなくサーバー。

答えて

2

speciesChoices <<- setNames(input$species$code_name, input$species$public_name)のようなものを使用してください。大域変数を変更するには、超割り当て演算子<<-が必要です。また、uiに設定された入力を参照するには、input$_id_を使用する必要があります。

サイドノート:あなたのエンドゲームがここにあるのかどうかはわかりませんが、実際にはオブザーバーを使用していて反応しないようにしてください。これらの違いについては、Joe Cheng's slides from useR2016を参照してください。

関連する問題