Shiny :: withProgress showNotification

Published onesixx on

http://shiny.rstudio.com/articles/progress.html
https://gallery.shinyapps.io/085-progress/

Progress indicators

 single-file app

 withProgress()  로딩중 가장 쉽고 기본적인 방법.

긴 계산과정을 포함하고 있는  reactive(), observer(), render함수() 내에 withProgress() 추가하기 

library(shiny)

ui <- fluidPage(
    plotOutput("plot")
)

server <- function(input, output, session) {
    output$plot <- renderPlot({
        # progress <- Progress$new(session, min=1, max=15)
        #     on.exit(progress$close())
        # progress$set(message = 'Calculation in progress', detail='This may take a while...')
        # for (i in 1:15) {
        #   progress$set(value = i)
        #   Sys.sleep(0.25)
        # }
        
        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)
    })
}

shinyApp(ui, server)

output$APlot <- renderPlot({
                   if(!is.null(env4$data)){
                       withProgress(message = 'A Plotting.....',{

showNotification

https://shiny.rstudio.com/articles/notifications.html
https://shiny.rstudio.com/reference/shiny/1.0.5/showNotification.html

ex1)

https://gallery.shinyapps.io/116-notifications/
Customizing showNotification
shinyApp(
  ui = fluidPage(
    textInput("txt", "Content", "Text of message"),
    radioButtons("duration", "Seconds before fading out",
      choices = c("2", "5", "10", "Never"),
      inline = TRUE
    ),
    radioButtons("type", "Type",
      choices = c("default", "message", "warning", "error"),
      inline = TRUE
    ),
    checkboxInput("close", "Close button?", TRUE),
    actionButton("show", "Show"),
    actionButton("remove", "Remove most recent")
  ),
  server = function(input, output) {
    id <- NULL

    observeEvent(input$show, {
      if (input$duration == "Never")
        duration <- NA
      else 
        duration <- as.numeric(input$duration)

      type <- input$type
      if (is.null(type)) type <- NULL

      id <<- showNotification(
        input$txt,
        duration = duration, 
        closeButton = input$close,
        type = type
      )
    })

    observeEvent(input$remove, {
      removeNotification(id)
    })
  }
)

ex2)

ui

이미지 삽입시 경로는 (./www/loading.gif)

tags$head( tags$style( HTML("#shiny-notification-admintab i{margin-right: 6px;}" ) ) )

server

env$adminMsg <- NULL

  observeEvent(input$startMonitor, {
    withBusyIndicatorServer("startMonitor", {

      env$adminMsg <<- showNotification(
                          ui=tags$div( #tags$img(src = "./loading.gif"),
                                tags$b(shiny::span(icon("spinner fa-spin")), tags$span(style="color:red","Monitoring is underway!")),tags$br(),
                                tags$p(tags$i("if you want to stop, click Stop button"))),
                          duration=NULL, closeButton=FALSE, type='message', id='admintab'
      )

    })
  })
  
  observeEvent(input$stop, {
    withBusyIndicatorServer("stop", {

      removeNotification(id =env$adminMsg)

    })
  })

action 옵션 예제

 action = a(href = "javascript:location.reload();", "Reload page")

html 로 뿌려질때 

<div id="shiny-notification-panel">
	<div id="shiny-notification-admintab" class="shiny-notification shiny-notification-message">
		<div class="shiny-notification-content">
			<div class="shiny-notification-content-text">
				<div>
					<b>
						<span> <i class="fa fa-spinner fa-spin"></i></span>
						<span style="color:red">Monitoring is underway!</span>
					</b>
					<br>
					<p>
						<i>if you want to stop, click Stop button</i>
					</p>
				</div>
			</div>
		<div class="shiny-notification-content-action"></div>
		</div>
	</div>
</div>

R
Categories: Shiny

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.