我有一个带有ui的闪亮应用程序,其中包含具有不同tabPanels的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')),
)))
在代码的服务器部分,我有一些响应值,这些响应值取决于插入的输入,还有一些输出(具有不同的名称),它们取决于响应值。
是否可以只编写一次用于无功值的代码,例如仅用于第一个tabPanel上的输入(因为两个面板的输入相同),或者我应该为每个tabPanel自己定义反应性值(在这种情况下,我还应该重命名输入,以便它们在输入上具有不同的名称每个tabPanel)?
例如
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()
})
}
Imo我认为这是很多文章,特别是如果您有很多参数的话。
答案 0 :(得分:0)
我想出了一种方法来创建 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)