2017-12-30 30 views
0

私はプリントとプロットという2つの出力を持っています。実行ボタンが押された(作業中)後に印刷を実行し、印刷が完了したらプロットパートが実行されます。別の仕上げの後に光沢のある反応を起こしてください

この理由は、印刷部分が数分かかるため、その出力は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) 
関連する問題