如何解决将Shiny App函数迁移到Global.R文件后调用失效的问题
问题根因
observeEvent要求第一个参数为响应式表达式,你当前的写法是在server中直接传入input$explain1的瞬时静态值,不是可监听的响应式对象,因此触发器无法正常工作。
解决方案
这里提供两种可直接运行的修改方案,你可以根据自己的使用习惯选择:
方案1:最小改动适配原有逻辑
仅调整2处代码即可复用你现有的封装结构:
- 修改
App.R的server调用逻辑,将输入值包裹为响应式对象传入函数:
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") ) ), ) server <- function(input, output) { # 改动点:将input值包裹为reactive响应式对象传入 AbeLincoln(reactive(input$explain1)) } shinyApp(ui = ui, server = server)
- 修改
Global.R中observeEvent的监听对象,读取响应式对象的值:
AbeLincoln <- function(x){ # 改动点:监听x()而非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." ) ) }) }
方案2:更符合Shiny规范的封装(推荐)
建议仅把静态的弹窗内容抽离到Global.R,响应式监听逻辑保留在server作用域中,避免后续多弹窗复用的时候出现作用域冲突:
- 修改
Global.R仅返回弹窗内容:
AbeLincolnModal <- function(){ 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." ) }
- 修改
App.R的server逻辑,将监听逻辑放在原生server作用域:
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") ) ), ) server <- function(input, output) { observeEvent(input$explain1, { showModal(AbeLincolnModal()) }) } shinyApp(ui = ui, server = server)
运行效果
两种方案均可实现你要的效果:
- 初始页面:

- 点击按钮后弹窗:

内容的提问来源于stack exchange,提问作者Curious Jorge - user9788072
相关产品推荐
相关产品推荐

