英文:
Dynamically generate argonSidebarItem in shiny
问题
您想要动态生成选项卡项,但在您的尝试中,所有输入都在同一个屏幕上。要实现所需的动态选项卡,请将 uiOutput("apps_ui")
放在适当的选项卡内,如下所示:
library(argonDash)
library(argonR)
library(shiny)
ui <- argonDashPage(
header = argonDashHeader(
color = "header_custom",
gradient = TRUE,
top_padding = 3,
bottom_padding = 3
),
sidebar = argonDashSidebar(
id = "app_sidebar",
vertical = TRUE,
side = "left",
size = "md",
background = "sidebar_custom",
wellPanel(id = "well_panel", argonDash::argonSidebarMenu(
shiny::br(),
argonDash::argonSidebarItem(tabName = "home", "Home", icon = htmltools::tags$i(class = paste0("fa fa-home"))),
shiny::br(),
argonDash::argonSidebarItem(tabName = "dynamic_tabs", "Dynamic Tabs", icon = argonR::argonIcon(name = "sound-wave")),
shiny::br()
))
),
body = argonDashBody(
id = "app_body",
argonTabItems(
argonTabItem(tabName = "home", p("Test")),
argonTabItem(tabName = "dynamic_tabs", uiOutput("apps_ui")) # 放在 "dynamic_tabs" 选项卡内
)
),
footer = NULL
)
server <- function(input, output, session) {
apps_fns <- c("id_study_metrics", "id_demo_app")
prefix_removed <- sub("id_", "", apps_fns)
output$apps_ui <- renderUI({
lapply(seq_along(apps_fns), function(i) {
argonTabItem(
tabName = prefix_removed[i],
br(),
textInput(apps_fns[i], "text", placeholder = prefix_removed[i])
)
})
})
}
shinyApp(ui, server)
现在,uiOutput("apps_ui")
已放置在名为 "Dynamic Tabs" 的选项卡中,您应该能够看到动态生成的选项卡项。
英文:
My expected output looks like this -
library(argonDash)
library(argonR)
library(shiny)
ui <- argonDashPage(
header = argonDashHeader(
color = "header_custom",
gradient = TRUE,
top_padding = 3,
bottom_padding = 3
),
sidebar = argonDashSidebar(
id = "app_sidebar",
vertical = TRUE,
side = "left",
size = "md",
background = "sidebar_custom",
wellPanel(id = "well_panel", argonDash::argonSidebarMenu(
shiny::br(),
argonDash::argonSidebarItem(tabName = "home", "Home", icon = htmltools::tags$i(class = paste0("fa fa-home"))),
shiny::br(),
argonDash::argonSidebarItem(tabName = "study_metrics", "Study Metrics",icon = argonR::argonIcon(name = "sound-wave")),
shiny::br(),
argonDash::argonSidebarItem(tabName = "demo_app", "Demo App"),
shiny::br()
)
)
),
body = argonDashBody(
id = "app_body",
argonTabItems(
argonTabItem(tabName = "home", p("Test")),
argonTabItem(tabName = "study_metrics",
br(),textInput('id_study_metrics', 'text', placeholder = 'study_metrics')
),
argonTabItem(tabName = "demo_app",
br(),textInput('id_demo_app', 'text', placeholder = 'demo_app')
)
)
),
footer = NULL
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
The part that I want to change is -
argonTabItem(tabName = "study_metrics",
br(),textInput('id_study_metrics', 'text', placeholder = 'study_metrics')
),
argonTabItem(tabName = "demo_app",
br(),textInput('id_demo_app', 'text', placeholder = 'demo_app')
)
I want to generate this tab items dynamically because this list going to expand in future.
My attempt -
library(argonDash)
library(argonR)
library(shiny)
ui <- argonDashPage(
header = argonDashHeader(
color = "header_custom",
gradient = TRUE,
top_padding = 3,
bottom_padding = 3
),
sidebar = argonDashSidebar(
id = "app_sidebar",
vertical = TRUE,
side = "left",
size = "md",
background = "sidebar_custom",
wellPanel(id = "well_panel", argonDash::argonSidebarMenu(
shiny::br(),
argonDash::argonSidebarItem(tabName = "home", "Home", icon = htmltools::tags$i(class = paste0("fa fa-home"))),
shiny::br(),
argonDash::argonSidebarItem(tabName = "study_metrics", "Study Metrics",icon = argonR::argonIcon(name = "sound-wave")),
shiny::br(),
argonDash::argonSidebarItem(tabName = "demo_app", "Demo App"),
shiny::br()
)
)
),
body = argonDashBody(
id = "app_body",
argonTabItems(
argonTabItem(tabName = "home", p("Test")),
uiOutput("apps_ui")
)
),
footer = NULL
)
server <- function(input, output, session) {
apps_fns <- c("id_study_metrics", "id_demo_app")
prefix_removed <- sub("id_", "", apps_fns)
output$apps_ui <- renderUI({
lapply(seq_along(apps_fns), function(i) {
argonTabItem(
tabName = prefix_removed[i],
br(),
textInput(apps_fns[i], "text", placeholder = prefix_removed[i])
)
})
})
}
shinyApp(ui, server)
but that does not respect the tabs and all the inputs are consolidated into the same screen.
I did a lot of attempts using tagList
etc but none of them are working for me the way I want it to. Sorry for the very long code but I wanted to make sure that I give a complete reproducible code similar to my case.
答案1
得分: 1
我会在您的服务器上呈现整个选项卡集,而不仅仅是一些选项卡:
#### UI
```r
# ...
body = argonDashBody(
id = "app_body",
uiOutput("apps_ui")
)
# ....
Server
output$apps_ui <- renderUI({
tabs <- lapply(seq_along(apps_fns), function(i) {
argonTabItem(
tabName = prefix_removed[i],
br(),
textInput(apps_fns[i], "text", placeholder = prefix_removed[i])
)
})
tabs <- c(
list(argonTabItem(tabName = "home", p("Test"))),
tabs)
do.call(argonTabItems, tabs)
})
Explanation
在您的原始方法中,您有一个包含动态输入的 div。这对应于一个选项卡。最终,您希望像这样:
argonTabs(tab1, tab2, tab3)
但最终您传递了
argonTabs(tab1, list(tab2, tab3))
这在概念上是不同的(基本上您告诉R
在第二个tab
中将两个"tabs"
放入列表中)。因此,您必须让服务器呈现整个内容,以便更精细地控制如何填充选项卡。
<details>
<summary>英文:</summary>
I would render the __whole__ tab set in your server and not only some tabs:
#### UI
```r
# ...
body = argonDashBody(
id = "app_body",
uiOutput("apps_ui")
)
# ....
Server
output$apps_ui <- renderUI({
tabs <- lapply(seq_along(apps_fns), function(i) {
argonTabItem(
tabName = prefix_removed[i],
br(),
textInput(apps_fns[i], "text", placeholder = prefix_removed[i])
)
})
tabs <- c(
list(argonTabItem(tabName = "home", p("Test"))),
tabs)
do.call(argonTabItems, tabs)
})
Explanation
In your original approach you have one div holding the dynamic input. This corresponds to one tab. Eventually you want something like:
argonTabs(tab1, tab2, tab3)
but in the end you are passing
argonTabs(tab1, list(tab2, tab3))
which is conceptually different (basically you are telling R
to put two "tabs"
in a list on the second tab
). Thus, you have to let the server do the rendering of the whole thing to get finer control of how to fill the tabs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论