R Shiny:从模块内更改选项卡

2024-01-11

我有一个带有多个选项卡的闪亮应用程序,我希望选项卡内有操作按钮,允许用户切换选项卡。我找到了以下页面:https://www.titanwolf.org/Network/q/e6b187b8-6cad-4ece-ad16-7ec73ed2f758/y https://www.titanwolf.org/Network/q/e6b187b8-6cad-4ece-ad16-7ec73ed2f758/y 如何从模块内部在闪亮的选项卡面板之间切换? https://stackoverflow.com/questions/62135166/how-to-switch-between-shiny-tab-panels-from-inside-a-module,这似乎表明问题是范围/命名空间错误,但它们没有完全解释正在发生的事情,而且我没有足够的声誉点来评论其他要求澄清的 stackoverflow 帖子。

这是我的示例代码:

modtab1_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 1',
           value = NS(id, 'tab.1'),
           
           h4('This is the first tab'),
           actionButton(NS(id, 'nexttab'), 'Next Tab')
           
          )
}

modtab1_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$nexttab, {
                   updateTabsetPanel(session = session, inputId = NS(id, 'tabs'), selected = NS(id, 'tab.2'))
                   print('button clicked')
                 })
               })
}

modtab2_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 2',
           value = NS(id, 'tab.2'),
           
           h4('This is the second tab'),
          )
}


ui <- fluidPage(
  tabsetPanel(
    id = 'tabs',
    modtab1_ui('tab1'),
    modtab2_ui('tab1')
  )
)

server <- function(input, output, session) {
  modtab1_server('tab1')
}

shinyApp(ui = ui, server = server)

编辑以解决新问题

modtab1_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 1',
           value = NS(id, 'tab.1'),

           h4('This is the first tab'),
           actionButton(NS(id, 'nexttab'), 'Next Tab'),

           textInput(NS(id,'userid'), 'User ID'),
           textOutput(NS(id, 'useridout'))
          )
}

modtab1_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$nexttab, {
                   updateTabsetPanel(session = session, inputId =  'tabs', selected = NS('tab2', 'tab.2'))
                   print('button clicked'),
                 })
                 
                 output$useridout <- renderText(input$userid)
               })
}

modtab2_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 2',
           value = NS(id, 'tab.2'),

           h4('This is the second tab'),
           actionButton(NS(id, 'firsttab'), 'First Tab'),

           textInput(NS(id, 'userid'), 'User ID'),
           textOutput(NS(id, 'useridout'))
          )
}

modtab2_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$firsttab, {
                   updateTabsetPanel(session = session, inputId =  'tabs', selected = NS('tab1', 'tab.1'))
                   print('button clicked'),
                 })
                 
                 output$useridout <- renderText(input$userid)
               })
}


ui <- fluidPage(
  tabsetPanel(
    id = 'tab1-tabs',
    modtab1_ui('tab1'),
    modtab2_ui('tab2')
  )
)

server <- function(input, output, session) {
  modtab1_server('tab1')
  modtab2_server('tab2')
}

shinyApp(ui = ui, server = server)

再次编辑我在一个新问题中问了这个问题,得到了回答:闪亮的模块:在具有不同命名空间的模块内切换选项卡 https://stackoverflow.com/questions/69831550/shiny-modules-switch-tabs-from-within-modules-that-have-different-namespaces


我想这就是您正在寻找的。做了两个相当小的改变!一、在modtab1_server函数中,我改变了ns(id, 'tabs')只是'tabs'。我认为由于 inputId 位于模块内,因此它已经添加了 id,在本例中这意味着它添加了 tab1。使用您现有的代码,我认为 tabsetPanel 的 id 是“tab1-tab1-tabs”,因此通过删除ns(id)它应该将 inputId 称为“tab1-tabs”。第二个更改是使 tabsetPanel id 为“tab1-tabs”,以封装模块将“tab1”添加到 updateTabsetPanel 的 inputId 的方式。

modtab1_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 1',
           value = NS(id, 'tab.1'),
           
           h4('This is the first tab'),
           actionButton(NS(id, 'nexttab'), 'Next Tab')
           
  )
}

modtab1_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$nexttab, {
                   updateTabsetPanel(session = session, inputId = 'tabs', selected = NS(id, 'tab.2'))
                   print('button clicked')
                 })
               })
}

modtab2_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 2',
           value = NS(id, 'tab.2'),
           
           h4('This is the second tab'),
  )
}


ui <- fluidPage(
  tabsetPanel(
    id = 'tab1-tabs',
    modtab1_ui('tab1'),
    modtab2_ui('tab1')
  )
)

server <- function(input, output, session) {
  modtab1_server('tab1')
}

shinyApp(ui = ui, server = server)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R Shiny:从模块内更改选项卡 的相关文章

随机推荐

  • Python range 函数如何在实际参数之前有一个默认参数?

    我正在编写一个函数 它接受一个可选列表并将其扩展到指定的长度 而不是写成foo n list None 我想知道如何模拟 Python 范围函数的行为 其工作原理如下 gt gt gt range 10 0 1 2 3 4 5 6 7 8
  • 批量上传图片到SSRS

    我们用了优秀的RSBuild http rsbuild codeplex com用于将报告批量上传到 SQL Server Reporting Services SSRS 但我找不到批量上传图像的方法 使用报告管理器一次上传一张图像是愚蠢的
  • (异步() => { })();这是什么?

    async function test async gt var a await this test1 var b await this test2 a var c await this test3 b this doThis a b c
  • 用于根据选择插入交叉引用的宏

    我目前在一家使用固定内部风格文档的公司工作 这包括我们的 Word 模板中内置的多级编号标题 IE 标题 1 1 1 标题 2 1 1 1 标题 3 etc 我们当前任务的很大一部分涉及添加对文档中其他部分的交叉引用 当文档有数百页且每页大
  • 使用 Eloquent (Laravel) 在分组之前进行排序

    我有一个包含以下列的 消息 表 CREATE TABLE messages id int 11 NOT NULL AUTO INCREMENT fromId int 11 NOT NULL toId int 11 NOT NULL mess
  • Java 函数在并行 MATLAB 中的非线性性能

    最近 我在我的项目中实现了并行化MATLAB http en wikipedia org wiki MATLAB计划 很大程度上取决于中提供的建议MATLAB 中 xlsread 速度慢 https stackoverflow com qu
  • 需要运行 .exe,然后在约 10 秒后终止它并移至批处理文件中的下一个命令

    我正在尝试创建一个运行 exe 的批处理文件 但在大约 10 秒后 在完成之前 杀死所述 exe 然后继续执行文件中的下一个命令 任何帮助将不胜感激 你可以使用这个 start program exe ping 127 0 0 1 n 10
  • Android Intent.ACTION_VIEW 基本身份验证

    如何将 HTTP 基本身份验证信息传递给Intent ACTION VIEW 这就是我激发意图的地方 public class OutageListFragment extends ListFragment implements Loade
  • 如何更改反向引用的大小写?

    我正在尝试修改 PowerShell 中的反向引用 但运气不佳 这是我的例子 456 Jane Doe replace d 3 2 ToUpper 1 如果我运行它 我会得到这个 简 无名氏 456 456 但我真的很期待这个 简 多伊 4
  • 将字符串(逐个字符)插入到更大的字符串中

    我试图通过用新字符串 toInsert 替换 原始 字符串中的子字符串来插入字符串 start参数是我要替换的子字符串的起始位置 注意 这是一个更大程序的一部分 但我只是想让这个功能发挥作用 include
  • 如何强制 PyCharm 检查requirements.txt 文件并建议更新?

    如何强制 PyCharm 检查requirements txt 文件并建议更新 我似乎无法控制这种情况何时发生 谢谢 我尝试打开任何 py 文件 检查包要求文件字段 设置 工具 Python 集成工具 没有任何东西触发 Windows 上的
  • 自定义表达式中的 Spotfire IF 语句 [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 If Len case SPEC gt 0 case SPEC case type 当我尝试上面的公式时 我收到错误 gt 作为无
  • 符号查找错误(linux - c++)

    我正在研究多代理系统 例如Robocup soccerrSim2d在 Gnu linux 上 我的 distb 是 Ubuntu 11 10 内核 3 2 gcc 4 6 我安装了librcsc http sourceforge jp pr
  • Spring中依赖资源发生变化时重新加载类

    您知道 Eclipse 如何在更新 Tomcat 所依赖的资源文件 例如 spring 上下文文件 时自动重新加载在 Tomcat 中运行的类 这样您就不必重新启动 Tomcat 了 如何使类依赖于资源文件 以便 Eclipse 在资源文件
  • 比较 2 个 Dictionary 实例

    我想比较两个的内容Dictionary
  • 从github下载最新代码到Android Studio

    How to sync我当地的安卓工作室项目上有最新的代码github 我想下载所有最新的更改但我不希望我的本地更改被覆盖 如果存在冲突 它应该要求我解决这些特定文件 有办法实现这一点吗 我想下载所有最新的更改 但我不想要我的本地 更改被覆
  • `forSome { val `? 的示例

    Scala 语言规范指定的语法存在主义类型 https scala lang org files archive spec 2 13 03 types html existential types as Type InfixType Exi
  • Blazor 格式化数字的输入类型

    有人知道如何将数字类型格式化为 100 10 吗 目前 如果该值为 100 10 它将显示为 100 1 尝试了 bind value format with C2 0 0 我是 Blazor 的新手 所以如果有人能指出我正确的方向 我将不
  • 为什么这个流不返回任何元素?

    我尝试将以下代码编写为流 AbstractDevice myDevice null for AbstractDevice device session getWorkplace getDevices if device getPluginc
  • R Shiny:从模块内更改选项卡

    我有一个带有多个选项卡的闪亮应用程序 我希望选项卡内有操作按钮 允许用户切换选项卡 我找到了以下页面 https www titanwolf org Network q e6b187b8 6cad 4ece ad16 7ec73ed2f75