R Shiny App generate tabPanel in lapply (and unlist behaviour)

huangapple go评论99阅读模式
英文:

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=FALSEunlist函数,但它不起作用。非常奇怪: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

  1. How can achieve the same results by with lapply inside the UI-part ? (I have tried also with do.call, but without success)
  2. More intringuingly : I have tried to place the lapply in the UI-part inside unlist function with parameters recursive=FALSE, but it does not work. Very weird : the result of the lapply function is a list of 2 elements, but the unlist function (even with recursive=FALSE)applied on it returns 3 elements ??! Anyone has explanation ?

Thanks in advance if you could help

  1. library(shiny)
  2. mytab <- function(){
  3. lapply(c("1", "2"), function(currset){
  4. currtit <- ifelse(currset=="1", "abc", "def")
  5. tabPanel(paste0(currtit, " tab"), icon = icon("table"), value = tolower(currtit),
  6. mainPanel(
  7. width = 12, style="margin-left:0.5%; margin-right:0.5%",
  8. tabsetPanel(
  9. tabPanel(paste0("Data", currtit),
  10. fluidRow(
  11. p(HTML(paste0("<br><br>Data ", currtit)))
  12. )
  13. )))) })}
  14. shinyApp(
  15. ui =
  16. navbarPage(id = "intabset",
  17. title = "HOME",
  18. windowTitle = "Data cleaning",
  19. theme = shinytheme("cerulean"),
  20. collapsible = TRUE,
  21. tabPanel(
  22. title = " Home", icon = icon("home"),
  23. mainPanel(width = 11, style="margin-left:4%; margin-right:4%",
  24. fluidRow(h3("Home page data cleaning"))
  25. )),# end tabpanel
  26. mytab()[[1]],
  27. mytab()[[2]]
  28. ),
  29. server = function(input, output) {
  30. }
  31. )

答案1

得分: 1

使用rlang包的splice运算符!!!来实现所需的结果之一:

  1. library(shiny)
  2. library(shinythemes)
  3. mytab <- function(currset) {
  4. currtit <- if (currset == "1") "abc" else "def"
  5. tabPanel(paste0(currtit, " tab"),
  6. icon = icon("table"), value = tolower(currtit),
  7. mainPanel(
  8. width = 12, style = "margin-left:0.5%; margin-right:0.5%",
  9. tabsetPanel(
  10. tabPanel(
  11. paste0("Data", currtit),
  12. fluidRow(
  13. p(HTML(paste0("<br><br>Data ", currtit)))
  14. )
  15. )
  16. )
  17. )
  18. )
  19. }
  20. shinyApp(
  21. ui =
  22. navbarPage(
  23. id = "intabset",
  24. title = "HOME",
  25. windowTitle = "Data cleaning",
  26. theme = shinytheme("cerulean"),
  27. collapsible = TRUE,
  28. tabPanel(
  29. title = " Home", icon = icon("home"),
  30. mainPanel(
  31. width = 11, style = "margin-left:4%; margin-right:4%",
  32. fluidRow(h3("Home page data cleaning"))
  33. )
  34. ),
  35. !!!lapply(1:2, mytab)
  36. ),
  37. server = function(input, output) {
  38. }
  39. )

R Shiny App generate tabPanel in lapply (and unlist behaviour)

编辑 如果您更喜欢基本解决方案,您可以使用do.call 来实现相同的效果,如下所示:

  1. shinyApp(
  2. ui =
  3. do.call(
  4. navbarPage,
  5. c(
  6. list(
  7. id = "intabset",
  8. title = "HOME",
  9. windowTitle = "Data cleaning",
  10. theme = shinytheme("cerulean"),
  11. collapsible = TRUE,
  12. tabPanel(
  13. title = " Home", icon = icon("home"),
  14. mainPanel(
  15. width = 11, style = "margin-left:4%; margin-right:4%",
  16. fluidRow(h3("Home page data cleaning"))
  17. )
  18. )
  19. ),
  20. lapply(1:2, mytab)
  21. )
  22. ),
  23. server = function(input, output) {
  24. }
  25. )
英文:

One option to achieve your desired result would be to use the splice operator !!! from rlang:

  1. library(shiny)
  2. library(shinythemes)
  3. mytab &lt;- function(currset) {
  4. currtit &lt;- if (currset == &quot;1&quot;) &quot;abc&quot; else &quot;def&quot;
  5. tabPanel(paste0(currtit, &quot; tab&quot;),
  6. icon = icon(&quot;table&quot;), value = tolower(currtit),
  7. mainPanel(
  8. width = 12, style = &quot;margin-left:0.5%; margin-right:0.5%&quot;,
  9. tabsetPanel(
  10. tabPanel(
  11. paste0(&quot;Data&quot;, currtit),
  12. fluidRow(
  13. p(HTML(paste0(&quot;&lt;br&gt;&lt;br&gt;Data &quot;, currtit)))
  14. )
  15. )
  16. )
  17. )
  18. )
  19. }
  20. shinyApp(
  21. ui =
  22. navbarPage(
  23. id = &quot;intabset&quot;,
  24. title = &quot;HOME&quot;,
  25. windowTitle = &quot;Data cleaning&quot;,
  26. theme = shinytheme(&quot;cerulean&quot;),
  27. collapsible = TRUE,
  28. tabPanel(
  29. title = &quot; Home&quot;, icon = icon(&quot;home&quot;),
  30. mainPanel(
  31. width = 11, style = &quot;margin-left:4%; margin-right:4%&quot;,
  32. fluidRow(h3(&quot;Home page data cleaning&quot;))
  33. )
  34. ),
  35. !!!lapply(1:2, mytab)
  36. ),
  37. server = function(input, output) {
  38. }
  39. )
  40. #&gt;
  41. #&gt; Listening on http://127.0.0.1:5552

R Shiny App generate tabPanel in lapply (and unlist behaviour)

EDIT And as you prefer a base solution you could achieve the same using do.call like so:

  1. shinyApp(
  2. ui =
  3. do.call(
  4. navbarPage,
  5. c(
  6. list(
  7. id = &quot;intabset&quot;,
  8. title = &quot;HOME&quot;,
  9. windowTitle = &quot;Data cleaning&quot;,
  10. theme = shinytheme(&quot;cerulean&quot;),
  11. collapsible = TRUE,
  12. tabPanel(
  13. title = &quot; Home&quot;, icon = icon(&quot;home&quot;),
  14. mainPanel(
  15. width = 11, style = &quot;margin-left:4%; margin-right:4%&quot;,
  16. fluidRow(h3(&quot;Home page data cleaning&quot;))
  17. )
  18. )
  19. ),
  20. lapply(1:2, mytab)
  21. )
  22. ),
  23. server = function(input, output) {
  24. }
  25. )

huangapple
  • 本文由 发表于 2023年2月26日 23:37:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定