【问题标题】:Shiny tabsetPanel multiple plots but include wait message/plot while model running闪亮的 tabsetPanel 多个绘图,但在模型运行时包括等待消息/绘图
【发布时间】:2016-07-23 05:44:33
【问题描述】:

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

这是服务器。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")) 
      )
    )
  )
))

【问题讨论】:

    标签: r plot shiny


    【解决方案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

    【讨论】:

    • 我刚刚使用了withProgress 的修改版本,它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2019-05-20
    • 2015-06-08
    • 2018-01-25
    • 2022-10-04
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多