有没有一种有效的方法可以将文本反应性地导入 R 闪亮模式对话框?

Is there an efficient way to reactively import text into R shiny modal dialog?

R Shiny 中的我的应用程序使用模态对话框为用户提供冗长的解释。正如您在下面的 MWE 中看到的,有很多文本。在此处未显示的完整应用程序中,还有许多其他文本解释导致 UI 和服务器部分的代码非常混乱,难以阅读。除了在这个 MWE 中是如何完成的,有没有一种方法可以在用户单击操作按钮时反应性地导入文本(例如,直接从保存在同一目录中的 Word 文件导入)?

如果以上不可能,有没有办法像这样模块化冗长的文本,将其移动到另一个部分(如函数声明部分),这样它就不会弄乱 UI 或服务器部分?

看过一些R包,把对话框和相关功能做得很漂亮。当 运行 这个 MWE 时,我不在寻找比那更美的东西。我只需要一种有效划分文本的方法。

library(shiny)

ui <- fluidPage(style = "margin-top:20px;margin-left:10px;margin-right:10px",
                
      # --- Creates header row at top of page
    fluidRow
      (column(12, 
              actionButton("explain1",
                           strong("Gettyburg Address"),
                           icon = icon("info-circle"),
                           style="color: #fff;background-color:#337ab7;font-size:19px;
                                  position:fixed;width: 95vw;")
      ) # closes column
  ), # closes fluid row
)  # fluid page

server <- function(input, output) {
  observeEvent(input$explain1, {
    showModal(modalDialog(
      title = "Background and text of the Gettysburg Address",
      tags$ul(
        tags$li("Gettysburg Address is a speech that Abraham Lincoln delivered at 
                the dedication of the Soldiers' National Cemetery in Gettysburg, PA."), 
        tags$li("On the afternoon of November 19, 1863."),
        tags$li("Four and a half months after the Union armies defeated those of the 
                Confederacy at the Battle of Gettysburg."),
        tags$li("It is one of the best-known speeches in American history.")),
      tags$p(strong("Text of speech:")),
      
      "Four score and seven years ago our fathers brought forth upon this continent, 
      a new nation, conceived in Liberty, and dedicated to the proposition that all men 
      are created equal.",tags$p(""),
      "Now we are engaged in a great civil war, testing whether that nation, or any 
      nation so conceived and so dedicated, can long endure. We are met on a great 
      battle-field of that war. We have come to dedicate a portion of that field, as 
      a final resting place for those who here gave their lives that that nation might 
      live. It is altogether fitting and proper that we should do this.",tags$p(""),
      "",tags$p(""),
      "But, in a larger sense, we can not dedicate—we can not consecrate—we can not 
      hallow—this ground. The brave men, living and dead, who struggled here, have 
      consecrated it, far above our poor power to add or detract. The world will little 
      note, nor long remember what we say here, but it can never forget what they did here. 
      It is for us the living, rather, to be dedicated here to the unfinished work which 
      they who fought here have thus far so nobly advanced. It is rather for us to be here 
      dedicated to the great task remaining before us—that from these honored dead we take 
      increased devotion to that cause for which they gave the last full measure of devotion
      — that we here highly resolve that these dead shall not have died in vain—that this 
      nation, under God, shall have a new birth of freedom—and that government of the 
      people, by the people, for the people, shall not perish from the earth."
    )) # closes showModal
  })   # closes observeEvent
} # closes server

shinyApp(ui = ui, server = server)

为了响应 Roman 的建议,我将文本移到了一个单独的 Global.R 文件中。它现在不在我的应用程序文件中,并且更清晰,更容易理解。对于卡在同一个地方的任何其他人,下面是上面 MWE 的完整工作代码。我都保存在同一个目录下。

App.R 文件:

library(shiny)
source("./Global.R")

ui <- fluidPage(style = "margin-top:20px",
  fluidRow
    (column(12, 
          actionButton("explain1",
          strong("Gettyburg Address"),
          icon = icon("info-circle"),
          style="font-size:19px;
                 position:fixed")
     ) # closes column
  ), # closes fluid row
)  # fluid page

server <- function(input, output) {
  AbeLincoln(input,"explain1")
} # closes server

shinyApp(ui = ui, server = server)

Global.R 文件(多于 MWE 所需的文本,但我喜欢那个演讲):

AbeLincoln <- function(input,value){

observeEvent(input[[value]], {  
    showModal(modalDialog(  
      
      title = "Background and text of the Gettysburg Address",
      
      tags$ul(
        
        tags$li("Gettysburg Address is a speech that Abraham Lincoln delivered at 
                 the dedication of the Soldiers' National Cemetery in Gettysburg, PA."), 
        tags$li("On the afternoon of November 19, 1863."),
        tags$li("Four and a half months after the Union armies defeated those of the 
                 Confederacy at the Battle of Gettysburg."),
        tags$li("It is one of the best-known speeches in American history.")),
      
      tags$p(strong("Text of speech:")),
      
        "Four score and seven years ago our fathers brought forth upon this continent, 
        a new nation, conceived in Liberty, and dedicated to the proposition that all men 
        are created equal.",tags$p(""),
        "Now we are engaged in a great civil war, testing whether that nation, or any 
        nation so conceived and so dedicated, can long endure. We are met on a great 
        battle-field of that war. We have come to dedicate a portion of that field, as 
        a final resting place for those who here gave their lives that that nation might 
        live. It is altogether fitting and proper that we should do this.",tags$p(""),
      
        "",tags$p(""),
      
        "But, in a larger sense, we can not dedicate—we can not consecrate—we can not 
        hallow—this ground. The brave men, living and dead, who struggled here, have 
        consecrated it, far above our poor power to add or detract. The world will little 
        note, nor long remember what we say here, but it can never forget what they did here. 
        It is for us the living, rather, to be dedicated here to the unfinished work which 
        they who fought here have thus far so nobly advanced. It is rather for us to be here 
        dedicated to the great task remaining before us—that from these honored dead we take 
        increased devotion to that cause for which they gave the last full measure of devotion
        — that we here highly resolve that these dead shall not have died in vain—that this 
        nation, under God, shall have a new birth of freedom—and that government of the 
        people, by the people, for the people, shall not perish from the earth."
      
      ) # close modal dialog
    ) # close show modal 
  }) # close observe event
} # close function