在闪亮应用程序中启动功能时创建notiftication

时间:2018-04-02 09:18:11

标签: r shiny

我有以下R Shiny Application:

library(shiny)


shinyApp(
  ui = dashboardPage(
    dashboardHeader(
      title = "Tweetminer",
      titleWidth = 350
    ),
    dashboardSidebar(
      width = 350,
      sidebarMenu(
        menuItem("Menu Item")
      )
    ),
    dashboardBody(
      fluidRow(
        tabBox(
          tabPanel("Select tweets",
                   numericInput("tweet_amount", "Amount of tweets:", 10, min = 1, max = 100),
                   div(style="display:inline-block",numericInput("tweet_long", "Longitude:", 10, min = 1, max = 100)),
                   div(style="display:inline-block",numericInput("tweet_lat", "Latitude:", 10, min = 1, max = 100)),
                   #selectInput("tweet_name", "Account name", choices = c("@realDonaldTrump","@GorgeNails"), width = NULL, placeholder = NULL),
                   selectInput("tweet_name", "Account name", choices = c("@realDonaldTrump","@Yankees"), selected = NULL, multiple = FALSE,
                               selectize = TRUE, width = NULL, size = NULL),
                   actionButton("get_tweets", "Fetch the tweets"),
                   hidden(
                     div(id='text_div',
                         verbatimTextOutput("text")
                     )
                   )
                  )
        )
      )
    )
  ),
  server = function(input, output) {

  test_list <- c(1,2,3)
  run_function <- reactive({
    for(i in test_list){
      Sys.sleep(2)
    }
  })


  observeEvent(input$get_tweets, {
    run_function()
    toggle('text_div')
    output$text <- renderText({"You're now connecting to the API"})
  })
  }
)

此应用程序应允许用户输入参数,之后将删除推文。但是 - 因为我需要一些时间来寻找告知用户的方法,这需要一段时间。

使用此功能:

 observeEvent(input$get_tweets, {
    run_function()
    toggle('text_div')
    output$text <- renderText({"You're now connecting to the API"})
  })

我通知用户该函数(此示例中的run_function(),一个模型但仅用于复制目的)已准备就绪。但是,在您启动该功能并完成该功能后,我正在寻找通知。

因此,在您第一次按下它之后应该说 - 启动功能并在功能准备好之后 - > “你.. api”。

关于我应该改变什么以使其发挥作用的任何想法?

1 个答案:

答案 0 :(得分:1)

您可以使用withProgress功能,请查看以下示例:

output$plot <- renderPlot({
    withProgress(message = 'Calculation in progress', detail = 'This may take a while...', value = 0, {
      for (i in 1:15) {
        incProgress(1/15)
        Sys.sleep(0.25)
      }
    })
    plot(cars)
  })

More info

enter image description here

编辑:新示例:

library(shiny)

ui <- fluidPage(
    actionButton(inputId = 'DoSomething', label = 'Do something'),
    verbatimTextOutput('t1')
)

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

    output$t1 <- renderPrint('Doing nothing...')

    run_function <- reactive({

        withProgress(message = "Starting...", max = 60, value = 0, {

            for (i in 1:25) {
                incProgress(1)
                Sys.sleep(0.25)
            }

            setProgress(message = "Connected..")

            for (i in 1:25) {
                incProgress(1)
                Sys.sleep(0.25)
            }

            setProgress(message = "Doing Stuffs...")

            for (i in 1:10) {
                incProgress(1)
                Sys.sleep(0.25)
            }

            setProgress(message = "Disconecting...")
        })

        return('Finish')
    })

    observeEvent(input$DoSomething, {

        output$t1 <- renderPrint(run_function())

    })
}
shinyApp(ui, server)