2013-06-26 64 views
36

我使用Shiny GUI R軟件包。我正在尋找一種方式來顯示消息,如「加載...」在actionButton被按下後。該函數需要幾分鐘才能執行,所以我需要以某種方式通知用戶該按鈕實際上觸發了一些事件。現在server.R代碼如下所示:R閃亮:功能運行時顯示「loading ...」消息

DATA <- reactive({ 
    if(input$DownloadButton>0) { 
    RunDownload() 
    } else { 
    NULL 
    } 
}) 

output$Download <- renderText({ 
    if(NROW(DATA())>0) { 
    paste0(Sys.time(),": ",NROW(DATA()), " items downloaded") 
    } else { 
    '' 
    } 
}) 

actionButton()是從互聯網上下載數據的功能。 input$DownloadButton是actionButton。因此,在按下按鈕之後,用戶等待幾分鐘,然後纔看到一條消息,說明下載已成功。我想在actionButton按下後顯示一條消息「Loading ...」,然後在執行結束時顯示另一條消息,如paste0(Sys.time(),": ",NROW(DATA()), " items downloaded")

+0

爲了簡化事情,我認爲你需要一個進度條,而在R中有很多方法可以將它添加到函數中。我們可以有一個'RunDownload'版本來看看如何添加進度條嗎? – dickoa

+0

我不需要進度條,它可以稱爲二進制進度條。我需要顯示2條消息:一條在函數啓動,一條在函數結束。我想我忘了在消息體中指定我正在使用Shiny包,它不僅僅是R代碼。現在就解決這個問題。 – user1603038

回答

28

我已經在使用比以前發佈的更簡單,更可靠的方法。

tags$style(type="text/css", " 
      #loadmessage { 
      position: fixed; 
      top: 0px; 
      left: 0px; 
      width: 100%; 
      padding: 5px 0px 5px 0px; 
      text-align: center; 
      font-weight: bold; 
      font-size: 100%; 
      color: #000000; 
      background-color: #CCFF66; 
      z-index: 105; 
      } 
    ") 

conditionalPanel(condition="$('html').hasClass('shiny-busy')", 
       tags$div("Loading...",id="loadmessage") 
) 

例A組合:

runApp(list(
    ui = pageWithSidebar(
     headerPanel("Test"), 
     sidebarPanel(
      tags$head(tags$style(type="text/css", " 
      #loadmessage { 
       position: fixed; 
       top: 0px; 
       left: 0px; 
       width: 100%; 
       padding: 5px 0px 5px 0px; 
       text-align: center; 
       font-weight: bold; 
       font-size: 100%; 
       color: #000000; 
       background-color: #CCFF66; 
       z-index: 105; 
      } 
      ")), 
      numericInput('n', 'Number of obs', 100), 
      conditionalPanel(condition="$('html').hasClass('shiny-busy')", 
          tags$div("Loading...",id="loadmessage")) 
     ), 
     mainPanel(plotOutput('plot')) 
), 
    server = function(input, output) { 
    output$plot <- renderPlot({ Sys.sleep(2); hist(runif(input$n)) }) 
    } 
)) 

標籤$頭()不是必需的,但它是一個很好的做法,讓所有的風格頭部標籤內。

+0

對於需要一段時間加載的頁面內的'renderDataTable'表,可以做些類似的事情嗎? – 719016

+0

喜歡這個解決方案,但是我很難用'shinydashboard'工作。任何想法爲什麼?在開發人員控制檯中,我可以看到觸發的條件,但儘管在'conditionalPanel'周圍嘗試了許多不同的位置和包裝,但我無法將消息實際顯示出來。 – ClaytonJY

+0

如果任何人有同樣的問題,我做了這個錯誤解決方案,以獲得更好看,更自然的使用進度條,也適用於'shinydashboard':https://github.com/rstudio/shiny/issues/609 – ClaytonJY

4

我加入以下代碼sidebarPanel()解決了這個問題:

HTML('<script type="text/javascript"> 
     $(document).ready(function() { 
      $("#DownloadButton").click(function() { 
      $("#Download").text("Loading..."); 
      }); 
     }); 
     </script> 
') 
2

我發現了一個解決方案,爲我工作得很好。我正在使用Bootstrap模式。當函數的執行開始時顯示,並在結束時再次隱藏。

modalBusy < - 功能(ID,標題,...){

msgHandler = singleton(tags$head(tags$script('Shiny.addCustomMessageHandler("jsCode", 
              function(message) { 
               console.log(message) 
               eval(message.code); 
              });' 
              ) 
           ) 
        ) 

label_id = paste(id, "label", sep='-') 
modal_tag <- div(id=id, 
       class="modal hide fade", 
       "aria-hidden"=FALSE, 
       "aria-labelledby"=label_id, 
       "role"="dialog", 
       "tabindex"="-1", 
       "data-keyboard"=FALSE, 
       "data-backdrop"="static") 
header_tag <- div(class="modal-header", 
       h3(id=label_id, title)) 
body_tag <- div(class="modal-body", 
       Row(...)) 
footer_tag <- div(class="modal-footer") 
modal_tag <- tagAppendChildren(modal_tag, header_tag, body_tag, footer_tag) 
tagList(msgHandler, modal_tag) 
} 

顯示和隱藏它使用的功能

showModal <- function(id,session) { 
    session$sendCustomMessage(type="jsCode", 
          list(code= paste("$('#",id,"').modal('show')" 
              ,sep=""))) 
} 

hideModal <- function(id,session) { 
    session$sendCustomMessage(type="jsCode", 
          list(code= paste("$('#",id,"').modal('hide')" 
              ,sep=""))) 
} 

調用之前的ShowModal功能你的函數調用和hideModal函數!

希望這會有所幫助。

Seb

+0

我無法得到這個工作。 @Seb請你分享一個實際的例子。謝謝 – guna

3

您可以使用ShinyJS:https://github.com/daattali/shinyjs

當按下actionButton,就可以輕鬆切換文本組件顯示「載入中...」,當計算完成後,您可以然後切換這個組件隱藏。