【问题标题】:R Shiny - Sequential text outputR Shiny - 顺序文本输出
【发布时间】:2018-01-05 00:52:56
【问题描述】:

我想在处理代码之前先打印一个文本,然后在代码执行后打印一个确认。

这是我的代码(app.R 的 UI 和服务器): 所有未指定的变量都在 app.R 文件中启动。

ui <- fluidPage(
  # Application title
  titlePanel(h1("Order2Ship", align="center")),
  # Sidebar iputs
  sidebarLayout(
    sidebarPanel( #LONG LIST OF INPUTS
    ),
    # On Screen output
    mainPanel(
      textOutput("START"),
      textOutput("Confirm")
    )
  )
)
# Underlining code for output (computes the whole each time an input is changed)
server <- function(input, output) {
  observeEvent(input$Do, { # ignores all return values
    output$START <- renderText({"Starting Analysis"})
    O2S( #LONG LIST OF PARAMETERS, FROM INPUTS
    )
    output$Confirm <- renderText({"Analysis Done"})
  })
}
# Run the application
shinyApp(ui = ui, server = server)

我不需要函数O2S 的任何返回,它基本上将一个文件作为输入并生成一个解决方案文件。但是,在函数运行后,两个文本输出同时显示。我想不明白。我刚开始接触 Shiny,很抱歉提出这么幼稚的问题。

【问题讨论】:

  • 那是因为启动和确认的renderText只有在函数运行后才从服务器传递到ui。
  • 我明白,但我该如何解决?
  • 也许我遗漏了一些东西,但我在运行您的应用程序时看到的只是 Order2Ship。没有别的...
  • 如果在 observeEvent 函数之前使用第一个 renderText 会怎样?这样做将按以下顺序运行您的代码:在mainPanel 中显示Starting Analysis 文本;运行代码输入文件并生成解决方案文件;最后显示Analysis Done
  • @mlavoie 所需的数据和一些代码被省略以使其简短。创建一个O2S函数,以及一个do的动作按钮,你可以复制行为。

标签: r shiny


【解决方案1】:

您好,您可以使用 shinyjs 来创建类似这样的链事件

library(shinyjs)
library(shiny)

ui <- fluidPage(
  # Application title
  titlePanel(h1("Order2Ship", align="center")),
  # Sidebar iputs
  sidebarLayout(
    sidebarPanel( #LONG LIST OF INPUTS
      actionButton(inputId = "Do",
                   label = "Start")
    ),
    # On Screen output
    mainPanel(
      textOutput("START"),
      textOutput("Confirm"),
      useShinyjs()
    )
  )
)
# Underlining code for output (computes the whole each time an input is changed)
server <- function(input, output) {
  startText <- eventReactive({input$Do},{
    runjs("Shiny.onInputChange('analysisStarted',Date.now())")
    "Starting Analysis"
  },
  ignoreInit = TRUE)
  output$START <- renderText({startText()})
  observeEvent(input$analysisStarted, { # ignores all return values
    temp <- NULL
    for(i in seq(50000)){
      temp <- c(temp,i)
    }
    runjs("Shiny.onInputChange('analysisFinished',true)")
  },
  ignoreInit = FALSE)
  confirmText <- eventReactive({input$analysisFinished},{
    "Analysis Done"
  },
  ignoreInit = FALSE)
  output$Confirm <- renderText({confirmText()})
}
# Run the application
shinyApp(ui = ui, server = server)

希望这会有所帮助!

【讨论】:

  • 我明天试试这个然后回复你。谢谢。
  • 成功了。谢谢。你能解释一下它是如何工作的吗?据我所知,runjs 正在创建某种标志,但为什么我们在Shiny.onInputChange 函数中有一次Date.now() 和另一次true?另外,为什么true 而不是TRUE?那是因为它是用 JavaScript 编写的吗?
猜你喜欢
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2018-03-16
相关资源
最近更新 更多