英文:
R Shiny App generate tabPanel in lapply (and unlist behaviour)
问题
我正在尝试使用lapply
创建Shiny App的UI部分,更准确地说,是生成包含嵌套tabsetPanel
的多个tabPanel
。
我已经或多或少地成功做到了,但只是通过在UI部分之外放置一个函数(并不是很方便或优雅)。
有人能告诉我
1)如何在UI部分内部使用lapply
来实现相同的结果?(我也尝试过使用do.call
,但没有成功)
2)更有趣的是:我尝试将lapply
放置在UI部分内部,并在其中使用参数recursive=FALSE
的unlist
函数,但它不起作用。非常奇怪:lapply
函数的结果是一个包含2个元素的列表,但对其应用unlist
函数(即使带有recursive=FALSE
)却返回3个元素?!有人有解释吗?
如果你能帮忙,提前感谢。
英文:
I am trying to create the UI part of a Shiny App with lapply
, more precisely, to generate several tabPanel
containing nested tabsetPanel
.
I have +or- succeeded to do it, but only by using a function placed outside the UI-part (not very convenient nor elegant).
Could anyone tell me
- How can achieve the same results by with
lapply
inside the UI-part ? (I have tried also withdo.call
, but without success) - More intringuingly : I have tried to place the
lapply
in the UI-part insideunlist
function with parametersrecursive=FALSE
, but it does not work. Very weird : the result of thelapply
function is a list of 2 elements, but theunlist
function (even withrecursive=FALSE
)applied on it returns 3 elements ??! Anyone has explanation ?
Thanks in advance if you could help
library(shiny)
mytab <- function(){
lapply(c("1", "2"), function(currset){
currtit <- ifelse(currset=="1", "abc", "def")
tabPanel(paste0(currtit, " tab"), icon = icon("table"), value = tolower(currtit),
mainPanel(
width = 12, style="margin-left:0.5%; margin-right:0.5%",
tabsetPanel(
tabPanel(paste0("Data", currtit),
fluidRow(
p(HTML(paste0("<br><br>Data ", currtit)))
)
)))) })}
shinyApp(
ui =
navbarPage(id = "intabset",
title = "HOME",
windowTitle = "Data cleaning",
theme = shinytheme("cerulean"),
collapsible = TRUE,
tabPanel(
title = " Home", icon = icon("home"),
mainPanel(width = 11, style="margin-left:4%; margin-right:4%",
fluidRow(h3("Home page data cleaning"))
)),# end tabpanel
mytab()[[1]],
mytab()[[2]]
),
server = function(input, output) {
}
)
答案1
得分: 1
使用rlang
包的splice运算符!!!
来实现所需的结果之一:
library(shiny)
library(shinythemes)
mytab <- function(currset) {
currtit <- if (currset == "1") "abc" else "def"
tabPanel(paste0(currtit, " tab"),
icon = icon("table"), value = tolower(currtit),
mainPanel(
width = 12, style = "margin-left:0.5%; margin-right:0.5%",
tabsetPanel(
tabPanel(
paste0("Data", currtit),
fluidRow(
p(HTML(paste0("<br><br>Data ", currtit)))
)
)
)
)
)
}
shinyApp(
ui =
navbarPage(
id = "intabset",
title = "HOME",
windowTitle = "Data cleaning",
theme = shinytheme("cerulean"),
collapsible = TRUE,
tabPanel(
title = " Home", icon = icon("home"),
mainPanel(
width = 11, style = "margin-left:4%; margin-right:4%",
fluidRow(h3("Home page data cleaning"))
)
),
!!!lapply(1:2, mytab)
),
server = function(input, output) {
}
)
编辑 如果您更喜欢基本解决方案,您可以使用do.call
来实现相同的效果,如下所示:
shinyApp(
ui =
do.call(
navbarPage,
c(
list(
id = "intabset",
title = "HOME",
windowTitle = "Data cleaning",
theme = shinytheme("cerulean"),
collapsible = TRUE,
tabPanel(
title = " Home", icon = icon("home"),
mainPanel(
width = 11, style = "margin-left:4%; margin-right:4%",
fluidRow(h3("Home page data cleaning"))
)
)
),
lapply(1:2, mytab)
)
),
server = function(input, output) {
}
)
英文:
One option to achieve your desired result would be to use the splice operator !!!
from rlang
:
library(shiny)
library(shinythemes)
mytab <- function(currset) {
currtit <- if (currset == "1") "abc" else "def"
tabPanel(paste0(currtit, " tab"),
icon = icon("table"), value = tolower(currtit),
mainPanel(
width = 12, style = "margin-left:0.5%; margin-right:0.5%",
tabsetPanel(
tabPanel(
paste0("Data", currtit),
fluidRow(
p(HTML(paste0("<br><br>Data ", currtit)))
)
)
)
)
)
}
shinyApp(
ui =
navbarPage(
id = "intabset",
title = "HOME",
windowTitle = "Data cleaning",
theme = shinytheme("cerulean"),
collapsible = TRUE,
tabPanel(
title = " Home", icon = icon("home"),
mainPanel(
width = 11, style = "margin-left:4%; margin-right:4%",
fluidRow(h3("Home page data cleaning"))
)
),
!!!lapply(1:2, mytab)
),
server = function(input, output) {
}
)
#>
#> Listening on http://127.0.0.1:5552
EDIT And as you prefer a base solution you could achieve the same using do.call
like so:
shinyApp(
ui =
do.call(
navbarPage,
c(
list(
id = "intabset",
title = "HOME",
windowTitle = "Data cleaning",
theme = shinytheme("cerulean"),
collapsible = TRUE,
tabPanel(
title = " Home", icon = icon("home"),
mainPanel(
width = 11, style = "margin-left:4%; margin-right:4%",
fluidRow(h3("Home page data cleaning"))
)
)
),
lapply(1:2, mytab)
)
),
server = function(input, output) {
}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论