首页 > 解决方案 > 如何将功能从 Shiny App 移动到 Global.R 文件?

问题描述

我正在开发一个闪亮的应用程序,它在 modalDialog 函数中使用冗长的文本解释。用户单击一个操作按钮,解释会在对话框中弹出。我想将这些长文本函数从 App 文件(包含 UI 和服务器)中移出到单独的 Global.R 文件中,以清理代码,使其更具可读性。为什么我的以下尝试不起作用?我确信解决方案很简单。它在其他几篇 Stack Exchange 帖子中得到了解决,但提出的解决方案似乎在这种情况下不起作用。

下面是我目前正在创建一个单独的 Global.R 的 MWE(两个文件都保存在同一个目录中):

App.R 文件(它在 Global.R 文件中调用的函数称为“AbeLincoln”):

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 文件包含响应操作按钮的观察事件和所有文本:

AbeLincoln <- function(x){

observeEvent(x, {  
    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

这是应用程序应该如何工作的。我在下面显示图像,而不是在没有 Global.R 的情况下发布上面的组合代码。

首先提示操作按钮:

在此处输入图像描述

单击操作按钮后,您将获得以下内容:

在此处输入图像描述

标签: rshiny

解决方案


observeEvent函数假定您传入一个未引用的值。它试图观察x而不是input$explain1。为了使其更通用,您应该将 传递input给您的函数。

将您的包装器更改为类似

AbeLincoln <- function(input, value){
  observeEvent(input[[value]], {  
    showModal(modalDialog(  ...) )
  }
}

然后你的服务器将是

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

或者,您可以弄乱observeEvent参数并在 AbeLincoln 函数上使用非标准评估。所以你可以做

AbeLincoln <- function(x){
  x <- substitute(x)
  observeEvent(x, 
    event.quoted=TRUE, event.env=parent.frame(), {  
    showModal(modalDialog(  ...) )
  }
}

或者,或者,

AbeLincoln <- function(...) {
  observeEvent(..., event.env=parent.frame(), {  
    showModal(modalDialog(  ...) )
  }
}

而且您的服务器功能仍然可以

server <- function(input, output) {
  AbeLincoln(input$explain1)
}

我猜这真的取决于你发现什么更直观。


推荐阅读