2014-11-28 2 views
5

저는 R Studio의 Shiny를 사용하여 애니메이션 그래프 프로젝트를 구성 중입니다. 현재 "가!" 버튼을 누르면 애니메이션이 시작됩니다. "재설정"버튼을 사용하여 변수를 다시 초기화하고 애니메이션을 다시 실행하고 싶습니다. 그러나 Shiny가 코드 내에서 입력 $ 버튼 값을 변경할 수 없기 때문에이 작업을 수행하는 방법에 대해 고민하고 있습니다. 실제 프로젝트는 아래의 샘플 블록과 형태가 비슷하지만 훨씬 더 복잡합니다. 애니메이션은 전달되는 정보에 필수적입니다. 프로젝트가 완료되면 Shiny 서버에 배포 할 예정이므로 사용자가 링크를 다시 열지 않고도 여러 선택 항목을 사용하여 애니메이션을 다시 실행할 수 있기를 바랍니다.Shiny R Studio에서 애니메이션 재설정

# ui.R 
library(shiny) 

shinyUI(fluidPage(

    # Application title 
    headerPanel("Cost Explorer"), 


    sidebarPanel(
    actionButton("goButton", "Go!"), 
    actionButton("reset", "Reset"), 

    sliderInput("myvar", label=h6("Variability of cost"), 
       min=0, max=50, value=10) 
), 

    mainPanel(

    plotOutput(outputId="tsplot") 

) 
)) 

    # server.R 
library(shiny) 

shinyServer(function(input, output, session) { 

    # initialize reactive values 
    ts <- reactiveValues(cost=rep(NA,100), year=(2010:2109), counter=1) 

    output$tsplot <- renderPlot({ 
    plot(ts$year, ts$cost, xlim=c(2010,2110), ylim=c(-200,200), xlab="Year", 
     ylab="Cost (US Dollars)", type="l", main="Forecasted Cost Time series") 
    }) 

    observe({ 

    isolate({ 


     if (ts$counter==1){ 
      ts$cost[ts$counter]=50 #initial cost 
     } 
     if (ts$counter > 1){ 
      ts$cost[ts$counter]=ts$cost[ts$counter-1]+rnorm(1,0,input$myvar) 
     } 
     ts$counter=ts$counter+1 

    }) 

    if (((isolate(ts$counter) < 100)) & (input$goButton > 0)){ 
     invalidateLater(200, session) 
    } 
    if (input$reset > 0){ 
     # How do I add reset functionality? 

    } 
    }) 
}) 

답변

3

앱에 기초하여, 다른 observe을 추가하고 글로벌 할당 연산자 <<-를 사용하여 1로 카운터를 재설정 빨랐다. 또한 그것은 음모를 변경하여 인덱스 된 변수를 표시합니다. 사람들이 가지고있는 비슷한 문제를보십시오, here. NB : 일부 앱에서는 사용자가 시작 버튼을 두 번 누르면 일시 중지 버튼이 생깁니다. 버튼을 클릭 할 때마다 버튼 색인이 두 개씩 나눌 수 있는지 확인하여이를 수행 할 수 있습니다.

난 너의 메모리를 다 써 버릴 수도 있으므로 (응용 프로그램을 더보고 있었고, 작업 관리자를 통해 메모리 프로파일을 보았을 때 참조되지 않은 옵저버를 가비지 수집하고 있는지 확인하십시오). 이 예제 here을 살펴보면 n 분 후에 클라이언트가 로그 오프되는 세션 당 로그 오프 기능을 설정할 수 있습니다.

rm(list = ls()) 
library(shiny) 

ui <- (fluidPage(
    # Application title 
    headerPanel("Cost Explorer"), 

    sidebarPanel(
    actionButton("goButton", "Go!"), 
    actionButton("reset", "Reset"), 
    sliderInput("myvar", label=h6("Variability of cost"),min=0, max=50, value=10) 
), 
    mainPanel(plotOutput(outputId="tsplot")) 
)) 

server <- (function(input, output, session) { 

    # initialize reactive values 
    ts <- reactiveValues(cost=rep(NA,100), year=(2010:2109), counter=1) 

    output$tsplot <- renderPlot({ 
    plot(ts$year[1:ts$counter], ts$cost[1:ts$counter], xlim=c(2010,2110), ylim=c(-200,200), xlab="Year", 
     ylab="Cost (US Dollars)", type="l", main="Forecasted Cost Time series") 
    }) 

    observe({ 
    isolate({ 
     if (ts$counter==1){ 
     ts$cost[ts$counter]=50 #initial cost 
     } 
     if (ts$counter > 1){ 
     ts$cost[ts$counter]=ts$cost[ts$counter-1]+rnorm(1,0,input$myvar) 
     } 
     ts$counter=ts$counter+1  
    }) 
    if (((isolate(ts$counter) < 100)) & (input$goButton > 0)){ 
     invalidateLater(200, session) 
    } 

    }) 

    observe({ 
    if (input$reset > 0){ 
     ts$counter <<- 1 
    } 
    }) 
}) 

runApp(list(ui = ui, server = server))