将闪亮的小部件显示与特定的导航栏 tabPanel() 选择连接起来

2024-03-19

我有一个闪亮的仪表板,其中有一个导航栏页面,其中包含两个 tabPanel"Summary" and "Available Funds". Then "Available Funds"由一个tabsetPanel()有两个选项卡面板"Plot" and "Plot2". When "Plot"单击后,右侧边栏中会显示闪亮的小部件。除了第一次加载应用程序并单击"Available funds"。发生这种情况是因为我没有点击"Plot"但我想知道如何连接导航栏 tabPanel"Available funds"以及小部件显示。

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      golem::activate_js(),
      navbarPage("Navbar!",
                 tabPanel("Summary"

                 ),
                 tabPanel("Available funds",
                          tabsetPanel(
                            id="tabA",
                            type = "tabs",
                            tabPanel("Plot"),
                            tabPanel("Plot2"))
                 )), 
      tags$script(
        '$("a[data-toggle=\'tab\']").click(function(){
          Shiny.setInputValue("tabactive", $(this).data("value"))
        })'
      )
    ),
    rightsidebar = rightSidebar(
      background = "dark",
      rightSidebarTabContent(
        id = 1,
        title = "Tab 1",
        icon = "desktop",
        active = TRUE,
        uiOutput("sl")

      )

    ),
    title = "Right Sidebar"
  )),
  server = function(input, output) {
    output$sl <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs",
        "Number of observations:",
        min = 0, max = 1000, value = 500
      )
    })

    observeEvent( input$tabactive , {
      if (input$tabactive == "Plot"){
        golem::invoke_js("showid", "sl")
      } else {
        golem::invoke_js("hideid", "sl")
      }
    })
  }
)

具有更多选项卡的编辑版本

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      golem::activate_js(),
      navbarPage("Navbar!",
                 id = "tabactive",
                 tabPanel("Summary",
                          tabsetPanel(
                            id="tabB",
                            type = "tabs",
                            tabPanel("Plot3"),
                            tabPanel("Plot4"))),
                 tabPanel("Available funds",
                          tabsetPanel(
                            id="tabA",
                            type = "tabs",
                            tabPanel("Plot"),
                            tabPanel("Plot2"))
                 ))
    ),
    rightsidebar = rightSidebar(
      background = "dark",
      rightSidebarTabContent(
        id = 1,
        title = "Tab 1",
        icon = "desktop",
        active = TRUE,
        uiOutput("sl")
      )
    ),
    title = "Right Sidebar"
  )),
  server = function(input, output) {
    output$sl <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs",
        "Number of observations:",
        min = 0, max = 1000, value = 500
      )
    })
    output$sl2 <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs2",
        "Number of observations:",
        min = 0, max = 100, value = 50
      )
    })
    output$sl3 <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs3",
        "Number of observations:",
        min = 0, max = 100, value = 50
      )
    })
    output$sl4 <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs4",
        "Number of observations:",
        min = 0, max = 100, value = 50
      )
    })
    observe({
      if (input$tabactive == "Available funds" && input$tabA == "Plot"){
        golem::invoke_js("showid", "sl")
      } else {
        golem::invoke_js("hideid", "sl")
      }
    })
    observe({
      if (input$tabactive == "Available funds" && input$tabA == "Plot2"){
        golem::invoke_js("showid", "sl2")
      } else {
        golem::invoke_js("hideid", "sl2")
      }
    })
    observe({
      if (input$tabactive == "Summary" && input$tabB == "Plot3"){
        golem::invoke_js("showid", "sl3")
      } else {
        golem::invoke_js("hideid", "sl3")
      }
    })
    observe({
      if (input$tabactive == "Summary" && input$tabB == "Plot4"){
        golem::invoke_js("showid", "sl4")
      } else {
        golem::invoke_js("hideid", "sl4")
      }
    })
  }
)

请检查以下内容(我只是修改了您的 if 条件):

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      golem::activate_js(),
      navbarPage("Navbar!",
                 id = "tabactive",
                 tabPanel("Summary"),
                 tabPanel("Available funds",
                          tabsetPanel(
                            id="tabA",
                            type = "tabs",
                            tabPanel("Plot"),
                            tabPanel("Plot2"))
                 ))
    ),
    rightsidebar = rightSidebar(
      background = "dark",
      rightSidebarTabContent(
        id = 1,
        title = "Tab 1",
        icon = "desktop",
        active = TRUE,
        uiOutput("sl")
      )
    ),
    title = "Right Sidebar"
  )),
  server = function(input, output) {
    output$sl <- renderUI({
      sliderInput(
        "obs",
        "Number of observations:",
        min = 0, max = 1000, value = 500
      )
    })

    observe({
      if (input$tabactive == "Available funds" && input$tabA == "Plot"){
        golem::invoke_js("showid", "sl")
      } else {
        golem::invoke_js("hideid", "sl")
      }
    })
  }
)

顺便提一句。你不需要那个 JS 代码来获取input$tabactive since navbarPage还有一个id争论。


另一种方法是包裹uiOutput("sl") in conditionalPanel像这样:

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      golem::activate_js(),
      navbarPage("Navbar!",
                 id = "tabactive",
                 tabPanel("Summary"),
                 tabPanel("Available funds",
                          tabsetPanel(
                            id="tabA",
                            type = "tabs",
                            tabPanel("Plot"),
                            tabPanel("Plot2"))
                 ))
    ),
    rightsidebar = rightSidebar(
      background = "dark",
      rightSidebarTabContent(
        id = 1,
        title = "Tab 1",
        icon = "desktop",
        active = TRUE,
        conditionalPanel(condition = "input.tabactive == 'Available funds' && input.tabA == 'Plot'", {uiOutput("sl")})
      )
    ),
    title = "Right Sidebar"
  )),
  server = function(input, output) {
    output$sl <- renderUI({
      sliderInput(
        "obs",
        "Number of observations:",
        min = 0, max = 1000, value = 500
      )
    })
  }
)

OP编辑后:

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      golem::activate_js(),
      navbarPage("Navbar!",
                 id = "tabactive",
                 tabPanel("Summary",
                          tabsetPanel(
                            id="tabB",
                            type = "tabs",
                            tabPanel("Plot3"),
                            tabPanel("Plot4"))),
                 tabPanel("Available funds",
                          tabsetPanel(
                            id="tabA",
                            type = "tabs",
                            tabPanel("Plot"),
                            tabPanel("Plot2"))
                 ))
    ),
    rightsidebar = rightSidebar(
      background = "dark",
      rightSidebarTabContent(
        id = 1,
        title = "Tab 1",
        icon = "desktop",
        active = TRUE,
        uiOutput("sl"),
        uiOutput("sl2"),
        uiOutput("sl3"),
        uiOutput("sl4")
      )
    ),
    title = "Right Sidebar"
  )),
  server = function(input, output) {
    output$sl <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs",
        "Number of observations:",
        min = 0, max = 1000, value = 500
      )
    })
    output$sl2 <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs2",
        "Number of observations:",
        min = 0, max = 100, value = 50
      )
    })
    output$sl3 <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs3",
        "Number of observations:",
        min = 0, max = 100, value = 50
      )
    })
    output$sl4 <- renderUI({
      req(input$tabactive)
      sliderInput(
        "obs4",
        "Number of observations:",
        min = 0, max = 100, value = 50
      )
    })
    observe({
      if (input$tabactive == "Available funds" && input$tabA == "Plot"){
        golem::invoke_js("showid", "sl")
      } else {
        golem::invoke_js("hideid", "sl")
      }
    })
    observe({
      if (input$tabactive == "Available funds" && input$tabA == "Plot2"){
        golem::invoke_js("showid", "sl2")
      } else {
        golem::invoke_js("hideid", "sl2")
      }
    })
    observe({
      if (input$tabactive == "Summary" && input$tabB == "Plot3"){
        golem::invoke_js("showid", "sl3")
      } else {
        golem::invoke_js("hideid", "sl3")
      }
    })
    observe({
      if (input$tabactive == "Summary" && input$tabB == "Plot4"){
        golem::invoke_js("showid", "sl4")
      } else {
        golem::invoke_js("hideid", "sl4")
      }
    })
  }
)

这里有一个UI-唯一的解决方案:

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      golem::activate_js(),
      navbarPage("Navbar!",
                 id = "tabactive",
                 tabPanel("Summary",
                          tabsetPanel(
                            id="tabB",
                            type = "tabs",
                            tabPanel("Plot3"),
                            tabPanel("Plot4"))),
                 tabPanel("Available funds",
                          tabsetPanel(
                            id="tabA",
                            type = "tabs",
                            tabPanel("Plot1"),
                            tabPanel("Plot2"))
                 ))
    ),
    rightsidebar = rightSidebar(
      background = "dark",
      rightSidebarTabContent(
        id = 1,
        title = "Tab 1",
        icon = "desktop",
        active = TRUE,
        conditionalPanel(condition = "input.tabactive == 'Available funds' && input.tabA == 'Plot1'", {
          sliderInput(
            "obs1",
            "Number of observations 1:",
            min = 0, max = 1000, value = 500
          )}),
        conditionalPanel(condition = "input.tabactive == 'Available funds' && input.tabA == 'Plot2'", {
          sliderInput(
            "obs2",
            "Number of observations 2:",
            min = 0, max = 100, value = 50
          )}),
        conditionalPanel(condition = "input.tabactive == 'Summary' && input.tabB == 'Plot3'", {
          sliderInput(
            "obs3",
            "Number of observations 3:",
            min = 0, max = 100, value = 50
          )}),
        conditionalPanel(condition = "input.tabactive == 'Summary' && input.tabB == 'Plot4'", {
          sliderInput(
            "obs4",
            "Number of observations 4:",
            min = 0, max = 100, value = 50
          )})
      )
    ),
    title = "Right Sidebar"
  )),
  server = function(input, output) {}
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将闪亮的小部件显示与特定的导航栏 tabPanel() 选择连接起来 的相关文章

  • R - 在浏览器中获取帮助而不是内置的 R 帮助程序

    我见过 R help 的两种不同行为 当你输入 density例如 帮助已在您的默认浏览器中打开 帮助在内置 R 帮助程序中打开 R 中的窗口 我目前有第二种行为 但我想在浏览器中打开帮助 我可以轻松地在这两种行为之间切换吗 无需重新安装
  • 有没有办法在 RStudio 中调试 RScript 调用?

    假设我从命令行运行 R 脚本 如下所示 Rscript prog R x y z 我想检查某一行的代码 目前 我无法在 RStudio 中以交互方式调试它 因为我不知道如何传递参数 由于它设计为从命令行运行 因此如何通过命令行 RStudi
  • R - 正则表达式错误(PCRE 版本)

    我正在尝试使用koRpus在 R 中在运行 RHEL6 的 Linux 服务器上进行词形还原 上周 当我安装了 MRO Microsoft R Open 3 2 3 时 下面的代码效果很好 library koRpus lw c danci
  • 从网络源获取 R 中的数据作为数据框

    我正在尝试使用 RCurl 包将一些空气污染背景数据作为 data frame 直接加载到 R 中 该网站有 3 个下拉框 用于在下载 csv 文件之前选择选项 如下图所示 我试图从下拉框中选择 3 个值 并使用 下载 CSV 按钮将数据作
  • 如何调整ggplot直方图的时间刻度轴

    我正在使用一个数据框 其中一列包含POSIXct日期时间值 我正在尝试使用绘制这些时间戳的直方图ggplot2但我有两个问题 我不知道如何设置 binwidthgeom histogram 我想将每个垃圾箱设置为一天或一周 我尝试提供 di
  • 使用底格里斯河从纬度/经度获取人口普查区

    我有相对较多的坐标 我想获取其人口普查区 除了 FIPS 代码 我知道我可以使用以下命令查找各个纬度 经度对call geolocator latlon 已完成here https stackoverflow com questions 5
  • R-了解 akima::interp 结果中的 NA 值

    我有以下数据框 ref dat k Intensity Slope 1 0 021467214 33 16 2 0 012444759 33 8 3 0 006079156 33 4 4 0 003792025 33 2 5 0 02276
  • 将值替换为其各自列的名称

    我有一个数据框 Code 401k CVS 101A true 231N true FD54 true 99JB 85F4 true 我试图用相应的列名称 例如 401k 替换 true 字符值 这是我想要的输出 Code 401k CVS
  • 从 R 中的 HTTPS 连接逐行读取

    当创建连接时open r 它允许逐行读取 这对于批量处理大数据流非常有用 例如这个脚本 https gist github com jeroenooms d33a24958d99bb969ac0通过一次读取 100 行来解析相当大的 gzi
  • 仅保留百分比的尾随零

    给出以下示例 library pander tableAbs lt Titanic 1 1 tablePct lt round prop table tableAbs 100 2 table lt cbind tableAbs tableP
  • 使用 R 进行项目组织 [重复]

    这个问题在这里已经有答案了 可能的重复 统计分析和报告撰写的工作流程 https stackoverflow com questions 1429907 workflow for statistical analysis and repor
  • R:表格格式

    我有一个包含以下列的 Excel 文件 Column1 Column2 Column3 ab bb 0 5 ab bc 0 1 ab cd 0 7 ab dd 0 8 ac bb 0 2 ac bg 0 8 ac ee 0 8 ac dd
  • 为什么这些数字不相等?

    下面的代码显然是错误的 有什么问题 i lt 0 1 i lt i 0 05 i 1 0 15 if i 0 15 cat i equals 0 15 else cat i does not equal 0 15 i does not eq
  • 使用outer代替expand.grid

    我正在寻找尽可能快的速度并留在基地做该做的事expand grid做 我用过outer为过去类似的目的创建一个向量 像这样的东西 v lt outer letters LETTERS paste0 unlist v lower tri v
  • 有没有一种简单的方法来判断存储在一个列表中的许多数据帧是否包含相同的列?

    我有一个包含许多数据框的列表 df1 lt data frame A 1 5 B 2 6 C LETTERS 1 5 df2 lt data frame A 1 5 B 2 6 C LETTERS 1 5 df3 lt data frame
  • 带 R 的多彩标题

    我想添加颜色某些词在我的图表标题中 我已经能够在这里找到一些先例 http blog revolutionanalytics com 2009 01 multicolor text in r html 具体来说 我希望用撇号括起来的文本 在
  • 使用 dplyr::filter 的整洁方式是什么?

    使用下面的函数调用foo c b 输出以内联方式显示 正确的写作方式是什么df gt filter x gt x 我已经包含了一个使用的示例mutate以整洁的风格与之对比filter foo lt function variables x
  • 使用predictNLS围绕R中的拟合值创建置信区间?

    我想使用 R 中 propogate 包中的 PredictNLS 围绕一大组拟合值构建置信区间 作为示例 我将使用它们在函数描述中引用的数据集 https rdrr io github anspiess propagate man pre
  • R 中的 Websocket

    我设法在 R 中建立到 Mtgox websocket 的连接 规格如下 url https socketio mtgox com mtgox Currency USD https socketio mtgox com mtgox Curr
  • GGPLOT2:如何在 ggplot() 脚本中绘制特定选择

    这是一个名为的大型数据集的峰值P 其中有 10 个优惠 CS 有不同的商店 SHP 具有多个数值 数据集列出了按周排序的它们 WK 2 tm 52 它创建一个大文件 仅前 6 行出现峰值 WK MND CS SHP RevCY RevLY

随机推荐