2017-02-12 3 views
3

다양한 입력으로 반짝이는 앱을 개발 중입니다. 일반적으로, 사용자가 입력 값을 받아들이도록 버튼을 누를 수있는 설정이 있습니다. 입력 값을 설정하면 설정해야 할 객체가 만들어집니다. 애니메이션은 사용자가 움직이는 sliderInput 객체에서 재생 버튼을 누를 때 실행되어야합니다. 생성 된 객체는 반복 될 때마다 업데이트됩니다.R 반짝이 : reactiveValues ​​개체의 값을 변경하는 방법

하나의 문제는 객체 중 하나가 reactiveValues ​​목록이라는 것입니다. 사용자가 설정을 변경하고 "수락"을 클릭하고 실행할 때마다 전체 개체 집합을 다시 만들 수 있습니다 (이전 값 지우기). 대신, reactiveValues ​​목록의 오브젝트는 겹쳐 쓰지 않지만 다음 설정으로 매번 증가됩니다. 따라서 각 객체 슬롯에는 실제로 여러 개의 객체가 있습니다.

최대 반복을 값으로 설정하고 수용을 누른 다음 값을 변경하고 다시 적용한 다음 재생을 클릭 해보십시오. 당신은 그것이 reactiveValues ​​객체의 다른 길이를 출력 할 것을 보게 될 것입니다.

rm()과 같은 몇 가지 시도를 시도하여 reactiveValues를 삭제하려고했습니다. 또한 나는 해결책을 여기 (shiny: How to update a reactiveValues object?) 시도했지만 그것은 작동하지 않았다.

library(shiny) 

iter_test <- shinyApp(

ui=shinyUI(fluidPage(
    titlePanel("title panel"), 

    sidebarLayout(position = "left", 
          sidebarPanel("Simulation parameters",         
           sliderInput("iter","Progress of simulation",value=1, min=1, max=30, round=TRUE, step=1, 
           animate=animationOptions(interval=1, loop=FALSE)), 
           actionButton('run',"Accept settings, press play above"), 
           sliderInput('max_iter','Maximum number of iterations',value=20, min=1, max=30, round=TRUE, step=1) 
          ), 
       mainPanel( plotOutput("plots")) 
       )#end of layout 
) 
)#end of UI definition 
, 

server=shinyServer(function(input, output, session) 
{ 

    observeEvent(input$run, { 
     #only set up simulation objects if accept is clicked 

    updateSliderInput(session, "iter", label="Progress of simulation", value=1, min=1, max=input$max_iter, step=1) 
    #try(rm(params)) 
    #set up objects needed for simulation 
    params <- reactiveValues(tmp=rep(1, input$max_iter)) 

    #when the play button is pressed, the following loop should trigger.  

    observeEvent(input$iter, { 
      print(length(params$tmp)) 

    }) 
}) 
} 
) 
) 
+0

나는 당신이 함께 일할 수있는 무언가를 가지고 있다고 생각하지만, 당신이 이것을 어디로 가는지 정말로 알지 못하기 때문에 완전히 확신 할 수는 없다. –

답변

2

나는 당신이 원하는 무슨이 당신을 얻을 생각 :

library(shiny) 

iter_test <- shinyApp(

    ui=shinyUI(fluidPage(
    titlePanel("title panel"), 

    sidebarLayout(position = "left", 
        sidebarPanel("Simulation parameters",         
           sliderInput("iter","Progress of simulation",value=1, min=1, max=30, round=TRUE, step=1, 
              animate=animationOptions(interval=1, loop=FALSE)), 
           actionButton('run',"Accept settings, press play above"), 
           sliderInput('max_iter','Maximum number of iterations',value=20, min=1, max=30, round=TRUE, step=1) 
       ), 
        mainPanel( plotOutput("plots")) 
    )#end of layout 
)),#end of UI definition 
    server=shinyServer(function(input, output, session) 
    { 
    params <- reactiveValues(tmp=NULL) 

    observeEvent(input$run, { 
     req(input$run)   
     updateSliderInput(session, "iter", label="Progress of simulation", value=1, min=1, max=input$max_iter, step=1) 
     #try(rm(params)) 
     #set up objects needed for simulation 
     params$tmp =rep(1, input$max_iter) 
    })   
    #when the play button is pressed, the following loop should trigger.  
    observeEvent(input$iter, { 
     req(input$iter) 
     print(length(params$tmp)) 
    }) 
    }) 
) 

이 항복 :

enter image description here

내가 20으로 설정 위, 그것은을 실행 한 후 6으로 설정 , 그리고 다시 그것을 달렸다. 이 20, 6

내가 만든 변경을 번갈아 것 전에, 더 이상 값을 반복하는 것을 볼 수 있었다 :

  • observeEvent의 중첩을 제거
  • 루프 외부 param$tmp 초기화 전화.

나는 종류의 당신이 input$run 경우에 여기에 eventReactive를 사용한다고 생각합니다,하지만 난 당신이 함께 어디로 가는지 정말 확실하지 않다, 그래서 나는 그것을 할 수 있습니다.

+0

안녕하세요, 마이크 감사합니다. 당신이 맞다면, observeEvent 내부에있는 params를 가짐으로써, 내가 클릭했을 때마다 실행 중이라는 것을 알았습니다. 이는 예상했던 것과 다릅니다. 나는 그 물체가 매번 덮어 쓸 것으로 예상했다. 질문에 대답하기 위해 첫 번째 observeEvent를 eventReactive로 바꾸려고 시도했지만 params에 값을 설정하지 않았기 때문에 작동하지 않습니다. observeEvent는 반응 값에 대한 반응으로 많은 동작을 수행하는 데 사용되는 반면, eventReactive는 계산을 수행하고 값을 반환한다고 가정합니다. –

+0

또한 두 번째 observeEvent의 시작 부분에 req (input $ run)를 추가하는 것이 문제를 해결한다고 생각합니다. 아무 것도 초기화하지 않은 경우 (inpu $ run) 결과 플롯이나 출력을 반복 실행하지 않기 때문에 문제가 해결됩니다. –

+0

오케이. 이게 받아 들일만한 대답인가요? –

관련 문제