2016-10-23 4 views
0

나는이 버튼을 클릭하면 시작 시간과 종료 시간을 절약하기 위해 반짝이는 앱을 만들었습니다. 나는 Start, EndDownload이 앱은 내가 StartEnd 버튼을 클릭 한 다음 Download 버튼을 클릭하면 .csv 버튼에 파일을 저장하는 시간을 절약하기위한 것입니다. 그러나 다운로드 버튼 클릭 시간을 절약하고 있습니다. 다른 시간을 제대로 구하도록 도와주세요.반짝 반짝 빛나는 액션 버튼 클릭시 절약 시간

library(shiny) 

shinyUI(fluidPage(
    titlePanel("Header"), 
    sidebarLayout(
    sidebarPanel(
     actionButton("start", "Start"), 
     tags$br(), 
     actionButton("end", "End"), 
     tags$br(), 
     downloadButton("downloadData", "Download") 
    ), 

    mainPanel(

    ) 
) 
)) 

shinyServer(function(input, output) { 

    startTime <- eventReactive(input$start,{ 
    Sys.time() 
    }) 

    endTime <- eventReactive(input$end,{ 
    Sys.time() 
    }) 


    data <- reactive({data.frame(start = startTime(), 
           end = endTime())}) 


    output$downloadData <- downloadHandler(
    filename = function() { 
     "download.csv" 
    }, 
    content = function(file) { 
     write.csv(data(), file, row.names = F) 
    } 
) 

}) 

답변

0

글로벌 변수를 사용하면 효과가 있습니다. 아래 예제 (세션 별 전역 변수 사용을 위해 편집 됨).

library(shiny) 

ui <- shinyUI(fluidPage(
    titlePanel("Header"), 
    sidebarLayout(
    sidebarPanel(
     actionButton("start", "Start"), 
     tags$br(), 
     actionButton("end", "End"), 
     tags$br(), 
     downloadButton("downloadData", "Download") 
    ), 

    mainPanel(

    ) 
) 
)) 

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

    starttime <- NULL 
    endtime <- NULL 

    observeEvent(input$start, { 
    starttime <<- Sys.time() 
    }) 

    observeEvent(input$end, { 
    endtime <<- Sys.time() 
    }) 

    output$downloadData <- downloadHandler(
    filename = function() { 
     "download.csv" 
    }, 
    content = function(file) { 
     data <- data.frame(start=starttime, end=endtime) 
     write.csv(data, file, row.names = F) 
    } 
) 

}) 

shinyApp(ui = ui, server = server) 
+0

shinyapp의 전역 변수를 사용하지 않는 것이 좋지 않습니까? 이 shinyapp을 여러 사용자가 사용하려고하고이 전역 변수가 다른 세션에서 공유되기 때문에이 응용 프로그램의 의도 된 응용 프로그램에 영향을 미치지 않습니다. – SBista

+0

@SBista 네, 세션 당 전역 변수를 원하면 서버 기능의 범위 내에 있어야합니다. 내 대답을 편집했습니다. 전역 변수는 그 자체로 나쁜 습관이 아닙니다. –

관련 문제