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

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

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

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) {
  }
)

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

编辑 如果您更喜欢基本解决方案,您可以使用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 &lt;- function(currset) {
  currtit &lt;- if (currset == &quot;1&quot;) &quot;abc&quot; else &quot;def&quot;
  tabPanel(paste0(currtit, &quot; tab&quot;),
    icon = icon(&quot;table&quot;), value = tolower(currtit),
    mainPanel(
      width = 12, style = &quot;margin-left:0.5%; margin-right:0.5%&quot;,
      tabsetPanel(
        tabPanel(
          paste0(&quot;Data&quot;, currtit),
          fluidRow(
            p(HTML(paste0(&quot;&lt;br&gt;&lt;br&gt;Data &quot;, currtit)))
          )
        )
      )
    )
  )
}

shinyApp(
  ui =
    navbarPage(
      id = &quot;intabset&quot;,
      title = &quot;HOME&quot;,
      windowTitle = &quot;Data cleaning&quot;,
      theme = shinytheme(&quot;cerulean&quot;),
      collapsible = TRUE,
      tabPanel(
        title = &quot; Home&quot;, icon = icon(&quot;home&quot;),
        mainPanel(
          width = 11, style = &quot;margin-left:4%; margin-right:4%&quot;,
          fluidRow(h3(&quot;Home page data cleaning&quot;))
        )
      ),
      !!!lapply(1:2, mytab)
    ),
  server = function(input, output) {
  }
)
#&gt; 
#&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:

shinyApp(
  ui =
    do.call(
      navbarPage,
      c(
        list(
          id = &quot;intabset&quot;,
          title = &quot;HOME&quot;,
          windowTitle = &quot;Data cleaning&quot;,
          theme = shinytheme(&quot;cerulean&quot;),
          collapsible = TRUE,
          tabPanel(
            title = &quot; Home&quot;, icon = icon(&quot;home&quot;),
            mainPanel(
              width = 11, style = &quot;margin-left:4%; margin-right:4%&quot;,
              fluidRow(h3(&quot;Home page data cleaning&quot;))
            )
          )
        ),
        lapply(1:2, mytab)
      )
    ),
  server = function(input, output) {
  }
)

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:

确定