2016-10-26 7 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 : 그것은 단지 UI로 입력을 업데이트

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에 서버.

어떤 아이디어가 있습니까?

답변

2

speciesChoices <<- setNames(input$species$code_name, input$species$public_name)과 같은 것을 사용하십시오. 전역 변수를 변경하려면 수퍼 할당 연산자 <<-이 필요하며 u12에 설정된 입력을 참조하려면 input$_id_을 사용해야합니다.

사이드 노트 : 여기에 최종 게임이 무엇인지 잘 모르겠지만 실제로는 관찰자를 사용하고 반응이 없도록하고 싶습니다. 이들의 차이에 대한 아이디어는 Joe Cheng's slides from useR2016을 참조하십시오.