2017-12-30 7 views
0

두 개의 출력물 인 인쇄물과 플롯이 있습니다. 실행 버튼을 누른 후 (작업 중) 프린트를 실행하고 프린트가 완료되면 플롯 파트가 실행됩니다.다른 마무리 후 반짝 반짝 빛납니다.

이유는 인쇄 부분이 몇 분이 걸리는 계산을 수행하기 때문에 그 출력이 plot 명령으로 이동해야하기 때문입니다.

간단한 예 :

library(shiny) 

ui <- fluidPage(


    sidebarLayout(
    sidebarPanel(
     actionButton('run','Run') 
    ), 

    mainPanel(
     verbatimTextOutput("Descriptive"), 
     plotOutput("plotData",width = "700px", height = "500px") 
    ) 
) 
) 

server <- function(input, output) { 

    output$Descriptive <- renderPrint({ 

    if(input$run>0){ 

     return(isolate({ 
     cat('Number of rows:', nrow(mtcars)) 
     mpg2 <<- mtcars$mpg+3 
     cyl2 <<- mtcars$cyl+3 
     })) 
    }else{return(invisible())} 
    }) 


    #### RUN AFTER DESCRIPTIVE COMPLETES #### 
    output$plotData <- renderPlot({ 
    plot(mpg2,cyl2) 
    }) 


} 

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

이 출력 후 사용할 수 있습니다 반응 식으로 계산을 돌립니다. 계산이 변경 될 때마다 계산에 의존하는 출력이 자동으로 업데이트됩니다. https://shiny.rstudio.com/articles/reactivity-overview.html을 한 눈에 볼 수 있습니다. –

답변

1

나는 reactiveValues ​​등의 변수를 저장하고 그들에 플롯 의존하게 당신을 건의 할 것입니다. 이를 통해 현재 전역 할당을 피할 수 있으며 플롯 업데이트를 변수의 변경에 종속되게 할 수 있습니다.

은 다음과 같이 수 :

global <- reactiveValues(mpg2 = mtcars$mpg, cyl2 = mtcars$cyl, txt = "") 

    observe({ 
    if(input$run > 0){ 
     Sys.sleep(5) # simulate minutes of calculating 
     global$txt <- paste('Number of rows:', nrow(mtcars)) 
     global$mpg2 <- mtcars$mpg + 3 
     global$cyl2 <- mtcars$cyl + 3 
    } 
    }) 

앱은 다음과 같이 보일 것이다 :

library(shiny) 

ui <- fluidPage(


    sidebarLayout(
    sidebarPanel(
     actionButton('run','Run') 
    ), 

    mainPanel(
     verbatimTextOutput("Descriptive"), 
     plotOutput("plotData",width = "700px", height = "500px") 
    ) 
) 
) 

server <- function(input, output) { 
    global <- reactiveValues(mpg2 = mtcars$mpg, cyl2 = mtcars$cyl, txt = "") 

    observe({ 
    if(input$run > 0){ 
     Sys.sleep(5) # simulate minutes of calculating 
     global$txt <- paste('Number of rows:', nrow(mtcars)) 
     global$mpg2 <- mtcars$mpg + 3 
     global$cyl2 <- mtcars$cyl + 3 
    } 
    }) 



    output$Descriptive <- renderPrint({ 
    if(nchar(global$txt)) return(global$txt) 
    }) 


    #### RUN AFTER DESCRIPTIVE COMPLETES #### 
    output$plotData <- renderPlot({ 
    plot(global$mpg2, global$cyl2) 
    }) 


} 

shinyApp(ui = ui, server = server) 
관련 문제