r - navbarPage、Shiny R 中不同 tabPanel 上的相同输入

标签 r shiny navbar shiny-server tabpanel

我有一个带有 ui 的 Shiny 应用程序,它由一个带有不同 tabPanel 的 navbarPage 组成。 在那些里面,我希望每个选项卡都有相同的按钮。所以每个选项卡都有相同的按钮,链接到相同的输入。

例如

ui <- navbarPage( theme = shinytheme("sandstone"),
              tabPanel(title="number one",
                       fluidRow(column(2, wellPanel(

                         sliderInput("one", "slider_one", value =0, min = 0, max=500, step=10),
                         sliderInput("two", "slider_two", value =250, min = 0, max=1000, step=10),
                       )),
                       column(8,
                              dataTableOutput('totals')
                              ))),

                tabPanel(title="number two" ,
                         fluidRow(column(2, wellPanel(
                           sliderInput("one", "slider_one", value =0, min = 0, max=500, step=10),
                           sliderInput("two", "slider_two", value =250, min = 0, max=1000, step=10)
                           )),
                           column(8,
                                         dataTableOutput('totals_2')),
                       )))

在代码的服务器部分,我有一些依赖于插入的输入的 react 值和一些依赖于 react 值的输出(具有不同的名称)。

是否可以只为 react 值编写一次代码,例如仅针对第一个 tabPanel 上的输入(因为它们对于两个面板都是相同的),或者我应该为每个 tabPanel 本身定义 react 值(在这种情况下,我还应该重命名输入,以便它们在每个选项卡面板)?

例如

server <- function(input, output) {

react <- reactive({some_function(input$one, input$two)})

output$totals<-renderDataTable({react()
  })

output$totals_2<-renderDataTable({react()
  })

}

所以它应该写成下面的内容。

ui <- navbarPage( theme = shinytheme("sandstone"),
              tabPanel(title="number one",
                       fluidRow(column(2, wellPanel(

                         sliderInput("one", "slider_one", value =0, min = 0, max=500, step=10),
                         sliderInput("two", "slider_two", value =250, min = 0, max=1000, step=10),
                       )),
                       column(8,
                              dataTableOutput('totals')
                              ))),

                tabPanel(title="number two" ,
                         fluidRow(column(2, wellPanel(
                           sliderInput("one1", "slider_one", value =0, min = 0, max=500, step=10),
                           sliderInput("two2", "slider_two", value =250, min = 0, max=1000, step=10)
                           )),
                           column(8,
                                         dataTableOutput('totals_2')),
                       )))
    server <- function(input, output) {

react <- reactive({some_function(input$one, input$two)})

react2 <- reactive({some_function(input$one1, input$two2)})

output$totals<-renderDataTable({react()
  })

output$totals_2<-renderDataTable({react2()
  })

}

我认为这会写很多东西,尤其是当您有很多参数时。

最佳答案

我想出了一种方法,可以通过循环创建 n 个选项卡。

library(shiny)
library(tidyverse)
library(shinythemes)

##modify n_ui to change de number of tabs

n_ui <- 10

ui <- fluidPage(
    uiOutput('repeated_tabs') ##uiOutput can eventually be extended to be reactive on users input to control the n of tabs from inside the app.
    ) 



server <- function(input, output, session) {
  ##create unique index for ids in each tab
  number_panel <- as.character(seq(1, n_ui))
  
    output$repeated_tabs <- renderUI({
    #map will create n tabPanel`s as specified in n_ui at the start of the script
     tabs <- map(number_panel, ~
                     tabPanel(title = paste('Tab', .x), 
                              fluidRow(
                                column(4,
                                       wellPanel(
                                                 sliderInput(paste0('one', .x), "slider_one", value =0, min = 0, max=500, step=10),
                                                 sliderInput(paste0('two',.x), "slider_two", value =250, min = 0, max=1000, step=10),
                                      )),
                                column(8,
                                       dataTableOutput(paste0('totals', .x))
                              ))) ) 
      
      
     tabisize <- partial(navbarPage, title = 'n Tabs app', theme = shinytheme("sandstone"))
      
     exec(tabisize, !!!tabs) #lets me pass each element in tabs as an individual argument (a loop wont do it, neither purrr::reduce()) 
    })  
    
    #to acces tab1 'totals' output
    
    output$totals1 <- renderDataTable({iris}) 
    
    
    
    
}

shinyApp(ui, server)

关于r - navbarPage、Shiny R 中不同 tabPanel 上的相同输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201183/

相关文章:

r - 以 react 方式更新 Shiny 中的 sliderInput

逐行或跨列选择非特定情况,以防取决于类型变量

r - Shiny -conditionalPanel - 将条件设置为服务器的输出

r - 更新 Shiny 的 react 数据帧会出现替换错误

html - 如何在同一个 html 页面上显示 2 个 UL 和 LI 的响应式导航菜单

r - 绘制时 Shapefile 和矢量文件不会精确重叠

r - 在 WSL 上安装 R devtools 包

r - 我想要连续有多个输入的 Shiny UI

twitter-bootstrap - 在组件中使用时,粘性导航栏在 bootstrap-vue 中不起作用

css - 隐藏 Bootstrap NavBar 的标题