闪亮的tabsetPanel多个图,但在模型运行时包括等待消息/图

时间:2016-04-03 19:06:15

标签: r plot shiny

我正在使用dismo::maxent函数创建绘图,但问题是它们可能需要很长时间(几分钟)。我想有两个标签,一个带有地图和可能性的图,另一个带有变量重要性。这是有效的,但我想在模型运行时在两个选项卡上都有等待消息/绘图/屏幕。这是2个函数调用,没有循环,所以我需要一个占位符消息。每次按下提交按钮后都需要显示,但最初不会显示。

这是server.R

data <- read.csv("CFR_Vaccine_Map_Corrected.csv")
load("./bioclim_10m.Rdata")

shinyServer(
  function(input, output) {
    observe({
      if (!(is.null(input$checkGroup))) {
        print(input$years)
        print(input$checkGroup)
        year.range <- input$years[1]:input$years[2]
        filtered.cases <- which(data$Outbreak %in% input$checkGroup & 
                                  data$Year %in% year.range)
        maxent.model <- maxent(bioStack, data[filtered.cases,5:4])
        print("model comp")
        maxent.map <- predict(maxent.model, bioStack)
        print("plotting map")
        output$map <- renderPlot({
          plot(maxent.map, xlab="Longitude", ylab="Latitude")
        })
        output$significance <- renderPlot({
          plot(maxent.model)
        })
    }
  })
  }
)

这是ui.R

shinyUI(fluidPage(
  titlePanel("Vaccine-Preventable Outbreaks"),

  sidebarLayout(
    sidebarPanel(
      helpText("Create maximum entropy maps with information from 
               Council on Foreign Relations data on vaccine-preventable 
               outbreaks."),

      checkboxGroupInput("checkGroup", h3("Category"),
                         choices = list("Polio" = "Polio", 
                             "Whooping Cough" = "Whooping Cough", 
                             "Measles" = "Measles",
                             "Mumps" = "Mumps",
                             "Rubella" = "Rubella",
                             "Polio" = "Polio",
                             "Violence" = "Violence")),
      sliderInput("years", "Years of interest:",
              min = 2007, max = 2016, value = c(2007, 2016), sep=""),
      submitButton("Submit")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Map", plotOutput("map")), 
        tabPanel("Variable Significance", plotOutput("significance")) 
      )
    )
  )
))

1 个答案:

答案 0 :(得分:1)

两种可能的解决方案:

  1. 使用Shiny中的withProgress功能:

    observeEvent(input$button, { 
        withProgress(message = "calculation in progress", detail = "This may take a while", value = 0, { 
            incProgress(1) 
            # You code that takes a long time.
        }) 
    })
    
  2. 使用此答案中标识的Javascript

    R shiny: display "loading..." message while function is running