在闪亮中动态生成 argonSidebarItem

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

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 &lt;- argonDashPage(
  header = argonDashHeader(
    color = &quot;header_custom&quot;,
    gradient = TRUE,
    top_padding = 3,
    bottom_padding = 3
  ),
  sidebar = argonDashSidebar(
    id = &quot;app_sidebar&quot;,
    vertical = TRUE,
    side = &quot;left&quot;,
    size = &quot;md&quot;,
    background = &quot;sidebar_custom&quot;,
    wellPanel(id = &quot;well_panel&quot;, argonDash::argonSidebarMenu(
      shiny::br(),
      argonDash::argonSidebarItem(tabName = &quot;home&quot;, &quot;Home&quot;, icon = htmltools::tags$i(class = paste0(&quot;fa fa-home&quot;))),
      shiny::br(),
      argonDash::argonSidebarItem(tabName = &quot;study_metrics&quot;, &quot;Study Metrics&quot;,icon = argonR::argonIcon(name = &quot;sound-wave&quot;)),
      shiny::br(),
      argonDash::argonSidebarItem(tabName = &quot;demo_app&quot;, &quot;Demo App&quot;),
      shiny::br()
    )
    )
  ),
  body = argonDashBody(
    id = &quot;app_body&quot;,
    argonTabItems(
      argonTabItem(tabName = &quot;home&quot;, p(&quot;Test&quot;)),
      argonTabItem(tabName = &quot;study_metrics&quot;,
        br(),textInput(&#39;id_study_metrics&#39;, &#39;text&#39;, placeholder = &#39;study_metrics&#39;)
      ),
      argonTabItem(tabName = &quot;demo_app&quot;,
        br(),textInput(&#39;id_demo_app&#39;, &#39;text&#39;, placeholder = &#39;demo_app&#39;)
      )
    )
  ),
  footer = NULL
)

server &lt;- function(input, output, session) {
  
}
shinyApp(ui, server)

在闪亮中动态生成 argonSidebarItem

The part that I want to change is -

 argonTabItem(tabName = &quot;study_metrics&quot;,
        br(),textInput(&#39;id_study_metrics&#39;, &#39;text&#39;, placeholder = &#39;study_metrics&#39;)
      ),
      argonTabItem(tabName = &quot;demo_app&quot;,
        br(),textInput(&#39;id_demo_app&#39;, &#39;text&#39;, placeholder = &#39;demo_app&#39;)
      )

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 &lt;- argonDashPage(
  header = argonDashHeader(
    color = &quot;header_custom&quot;,
    gradient = TRUE,
    top_padding = 3,
    bottom_padding = 3
  ),
  sidebar = argonDashSidebar(
    id = &quot;app_sidebar&quot;,
    vertical = TRUE,
    side = &quot;left&quot;,
    size = &quot;md&quot;,
    background = &quot;sidebar_custom&quot;,
    wellPanel(id = &quot;well_panel&quot;, argonDash::argonSidebarMenu(
      shiny::br(),
      argonDash::argonSidebarItem(tabName = &quot;home&quot;, &quot;Home&quot;, icon = htmltools::tags$i(class = paste0(&quot;fa fa-home&quot;))),
      shiny::br(),
      argonDash::argonSidebarItem(tabName = &quot;study_metrics&quot;, &quot;Study Metrics&quot;,icon = argonR::argonIcon(name = &quot;sound-wave&quot;)),
      shiny::br(),
      argonDash::argonSidebarItem(tabName = &quot;demo_app&quot;, &quot;Demo App&quot;),
      shiny::br()
    )
    )
  ),
  body = argonDashBody(
    id = &quot;app_body&quot;,
    argonTabItems(
      argonTabItem(tabName = &quot;home&quot;, p(&quot;Test&quot;)),
      uiOutput(&quot;apps_ui&quot;)
    )
  ),
  footer = NULL
)


server &lt;- function(input, output, session) {
  apps_fns &lt;- c(&quot;id_study_metrics&quot;, &quot;id_demo_app&quot;)
  prefix_removed &lt;- sub(&quot;id_&quot;, &quot;&quot;, apps_fns)
  
  output$apps_ui &lt;- renderUI({
    lapply(seq_along(apps_fns), function(i) {
      argonTabItem(
        tabName = prefix_removed[i],
        br(),
        textInput(apps_fns[i], &quot;text&quot;, placeholder = prefix_removed[i])
      )
    })
  })
}

shinyApp(ui, server)

but that does not respect the tabs and all the inputs are consolidated into the same screen.

在闪亮中动态生成 argonSidebarItem

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 = &quot;app_body&quot;,
    uiOutput(&quot;apps_ui&quot;)
  )
# ....

Server

output$apps_ui &lt;- renderUI({
    tabs &lt;- lapply(seq_along(apps_fns), function(i) {
      argonTabItem(
        tabName = prefix_removed[i],
        br(),
        textInput(apps_fns[i], &quot;text&quot;, placeholder = prefix_removed[i])
      )
    })
    tabs &lt;- c(
       list(argonTabItem(tabName = &quot;home&quot;, p(&quot;Test&quot;))),
       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 &quot;tabs&quot; 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.

huangapple
  • 本文由 发表于 2023年5月11日 12:26:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224156.html
匿名

发表评论

匿名网友

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

确定