更改withProgress()生成的消息框的样式和位置

时间:2016-01-27 12:20:25

标签: r shiny message

withProgress()函数可以生成一个消息框,指示闪亮的应用程序正在运行。但是消息是在浏览器的右上方,文本大小很小,这使得消息不那么引人注目。

所以我想知道有什么方法可以改变这个盒子的风格和位置,这样消息可以更具表现力。

以下是我的代码的一部分:

output$plot <- renderPlot({ 
 if (input$button){

 withProgress(
              distributionPolygon(data(),unit="years",colors=c("black","gray30","gray50","gray70","blue3","dodgerblue3","steelblue1","lightskyblue1","red4","indianred3","orange","seagreen4"),main=title(),tpsMax=input$months),
              message = 'Loading...',
              detail = ''
              )
      }
   })

3 个答案:

答案 0 :(得分:1)

更新

这是一个更新版本,因此您可以参考此示例,该示例来自here

server <- function(input, output) {
  output$plot <- renderPlot({
    input$goPlot # Re-run when button is clicked

    # Create 0-row data frame which will be used to store data
    dat <- data.frame(x = numeric(0), y = numeric(0))

    withProgress(message = 'Making plot', value = 0, {
      # Number of times we'll go through the loop
      n <- 10

      for (i in 1:n) {
        # Each time through the loop, add another row of data. This is
        # a stand-in for a long-running computation.
        dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))

        # Increment the progress bar, and update the detail text.
        incProgress(1/n, detail = paste("Doing part", i))

        # Pause for 0.1 seconds to simulate a long computation.
        Sys.sleep(0.1)
      }
    })

    plot(dat$x, dat$y)
  })
}

ui <- shinyUI(basicPage(
  plotOutput('plot', width = "300px", height = "300px"),
  actionButton('goPlot', 'Go plot')
))

shinyApp(ui = ui, server = server)

原帖

这也与您之前的问题有关。因此,您可能希望更改进度条的css样式。如果您想要一个更高级的css进度条,请随意玩游戏并进行更多研究。

注意:在计算过程中我也禁用了按钮,因此用户不会多次单击该按钮,强制计算重复。您可以进一步查看shinyjs以获取更多信息。正如您在计算过程中可以看到的那样,按钮被禁用。它将在完成后备份

rm(list = ls())
library(shiny)
library(shinyIncubator)
library(shinyjs)

server <- function(input, output, session) {
  observe({
    if(input$aButton==0) return(NULL)
    withProgress(session, min=1, max=15, expr={
      disable("aButton")
      for(i in 1:20) {
        setProgress(message = 'Finished...',detail = paste0('Number ',i, ':20'))
        Sys.sleep(0.1)
      }
      Sys.sleep(1.5)
    })
    enable("aButton")
  })
}

ui <- fluidPage(
  tags$head(tags$style(".shiny-progress {top: 50% !important;left: 50% !important;margin-top: -100px !important;margin-left: -250px !important; color: blue;font-size: 20px;font-style: italic;}")),
  sidebarPanel(actionButton("aButton", "Let's go!"), width=2),
  useShinyjs(),
  mainPanel(progressInit())
)

shinyApp(ui = ui, server = server)

enter image description here

答案 1 :(得分:0)

尝试修改CSS样式shiny-notification。您可以查看this post

答案 2 :(得分:0)

简单方法: 您可以在ui中包含HTML函数标记$ head()以覆盖进度条。由于它是通知,因此可以使用.shiny-notification调用。最简单的方法是将此行放在ui中的某个位置并调整位置(以及其他与CSS相关的参数see here

ui <- fluidPage(
  titlePanel(),

  tags$head(tags$style(".shiny-notification {position: fixed; top: 60% ;left: 50%)),

  sidebarPanel()
  mainPanel()
         )