You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny结合fullPage库:如何识别当前激活标签页以更新dataframe

解决方案

要检测当前处于哪个标签页并在进入PAGE 1时执行操作,可利用fullPage库与Shiny集成时自动生成的输入对象:

  • input$section:返回当前section的索引(从1开始,对应Home→1、Page1→2、Page2→3)
  • input$menu:返回当前选中的menu项ID(对应设置的"home"、"page1"、"page2")

以下是修改后的完整代码,实现进入PAGE 1时打印消息的功能:

library(shiny)
library(shinyjs)
library(fullPage)

ui = fullPage::pagePiling(
  sections.color = c("white", "pink", "green"),
  opts = options,
  menu = c(
    "Home" = "home",
    "Page 1" = "page1",
    "Page 2" = "page2"
  ),
  
  # HOME
  fullPage::pageSectionImage(
    center = T,
    img = "image.jpg",
    menu = "home",
    div(class = "header", "This is the title of home page"),
    div(class = "footer", "Developped by ...")
  ),
  
  # PAGE 1
  fullPage::pageSection(center = TRUE,
                        menu = "page1",
                        h1("Page 1")),
  
  # PAGE 2
  fullPage::pageSection(center = TRUE,
                        menu = "page2",
                        h1("Page 2"))
  
)

server <- function(input, output, session ) {
  # 监听menu选中事件,当选中page1时打印消息
  observeEvent(input$menu, {
    if(input$menu == "page1"){
      cat("当前处于PAGE 1\n")
      # 此处可添加更新dataframe的代码
    }
  })
  
  # 或监听section切换事件,进入第2个section(PAGE1)时打印消息
  observeEvent(input$section, {
    if(input$section == 2){
      cat("当前处于PAGE 1\n")
      # 此处可添加更新dataframe的代码
    }
  })
}

shinyApp(ui, server)

关键说明

  • 两种监听方式任选其一即可:input$menu直接对应你设置的标签页ID,input$section依据section顺序判断
  • 将打印消息的代码替换为你需要的dataframe更新逻辑即可实现需求

内容的提问来源于stack exchange,提问作者nimliug

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:48:23