如何在Shiny R中使用动态创建的选项卡制作条件面板? [英] How to make conditional panels with dynamically created tabs in Shiny R?

查看:213
本文介绍了如何在Shiny R中使用动态创建的选项卡制作条件面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,因此我可以动态创建为ID分配值的标签,以便我可以访问它们。如果我想在特定的动态创建的标签上放置某些内容,则可以在用户界面(通常可以在其中找到条件面板)中创建条件面板,但是我无法找到一种在选项卡上动态创建条件面板的方法。



这是我的代码,下面将对其解释:



UI:

  library(shiny)
ShinyUI(navbarPage( TiGr,

tabPanel( File Input Page,
fluidPage( Input)),

tabPanel(摘要统计和绘图,
fluidPage( Statistics)),

tabPanel( 时间簇,
fluidPage( cluster),
actionButton( subClust,标签= Create Subcluster),
uiOutput( tabs),
uiOutput ( condo),
conditionalPanel(condition = input.level == 1,
numericInput('clusters','Cluster count',9,
min = 1,max = 9)
),
conditionalPanel(condition = input.level == 2,
uiOutput( int1))


))

服务器:

 库(发光)

ShinyServer(function(input,output,session){
output $ tabs = renderUI({

Tabs< -as.list(rep(0,input $ subClust +1))
for(i in 0:length(Tabs)){
Tabs [i] = lapply(paste( Layer,i,sep =),tabPanel,value = i )
}
do.call(tabsetPanel,c(Tabs,id = level))
})


output $ condo = renderUI ({{
for(i in 1:input $ subClust + 1){
conditionalPanel(condition = paste( input.level ==,i,sep =)),
numericInput (簇,簇数,9,
最小值= 1,最大值= 9))
}
})



outVar< -reactive({
vars< -input $ clusters
return(strtoi(vars))
})
output $ int1 = renderUI({
numericInput('clusters1','Cluster count',outVar(),
min = 1,max = outVar())
})

condVar< -reactive({
vars< -input $ subClust
return(strtoi(vars))
})

}

因此在UI中,重要性标签是时间群集标签。我有一个可以单击的按钮,它将创建新的标签页。然后,我有选项卡输出,并尝试进行自动的条件面板输出(不执行任何操作)。



我有2个明确定义的条件面板,(我希望能够将它们全部放入uiOutput( condo)中,但现在它们只是用来强调显式创建条件面板与尝试动态创建条件面板之间的结果差异。



<服务器页面上有一种输出选项卡的方法,一种试图制作条件面板的方法(不执行任何操作)以及其他一些与动态输入有关的方法(只要输入1发生变化,输入2就会重置为输入1)。



有人可以解释我在服务器中使用条件页面方法的错误吗?(这是R的显式形式输出)

 > conditionalPanel(condition = input.level == 1,
+ numericInput('clusters','Cluster count',9,
+最小值= 1,最大值= 9))
< div数据显示-如果= input.level == 1>
< label for = clusters> Cluster count< / label>
< input id = clusters type = number value = 9 min = 1 max = 9 />
< / div>

这是R的尝试输出结果

 > conditionalPanel(condition = paste( input.level ==,1,sep =),
+ numericInput('clusters','Cluster count',9,
+ min = 1,max = 9))
< div data-display-if = input.level == 1>
< label for = clusters> Cluster count< / label>
< input id = clusters type = number value = 9 min = 1 max = 9 />
< / div>


解决方案

好的,我能够找到解决方案通过查看相关问题,可以动态地创建选项卡。我只是将选项卡更改为条件页面,然后在主面板的最后运行do.call以正确格式化条件页面。



我了解到的是,您不能仅使用一堆UI功能进行for循环并期望它们弹出(遗憾的是,它不起作用像那样)。这是我的服务器代码已更改:

  output $ condo = renderUI({
ConPages< -as.list(rep (0,condVar()+ 1))
for(i in 0:length(ConPages)){
ConPages [i] = lapply(paste( input.level ==,i,sep =),conditionalPanel,numericInput('clusters','Cluster count',9,min = 1,max = 9))
}
do.call(mainPanel,list(ConPages))
})


Okay, so I'm able to dynamically create tabs that are assigned values to an id, so I can access them. I'm able to create conditional panels in the UI (where they're normally found) if I want to put something on a specific dynamically created tab, but I'm having trouble finding a way to dynamically create conditional panels on my tabs.

Here's my code, I'll explain it below:

UI:

library(shiny)
shinyUI(navbarPage("TiGr",

                   tabPanel("File Input Page",
                            fluidPage("Input")),

                   tabPanel("Summary Statistics and Plots",
                            fluidPage("Statistics")),

                   tabPanel("Time Clusters",
                            fluidPage("cluster"),
                            actionButton("subClust", label = "Create Subcluster"),
                            uiOutput("tabs"),
                            uiOutput("condo"),
                            conditionalPanel(condition="input.level==1",
                                             numericInput('clusters', 'Cluster count', 9,
                                                          min = 1, max = 9)
                            ), 
                            conditionalPanel(condition="input.level==2",
                                             uiOutput("int1"))

                   )
))

Server:

library(shiny)

shinyServer(function(input, output, session) {
  output$tabs=renderUI({

    Tabs<-as.list(rep(0,input$subClust+1))
    for (i in 0:length(Tabs)){
      Tabs[i]=lapply(paste("Layer",i,sep=" "),tabPanel,value=i)
    }
    do.call(tabsetPanel,c(Tabs,id="level"))
  })


  output$condo=renderUI({
    for (i in 1:input$subClust+1){
      conditionalPanel(condition=paste("input.level==",i,sep=""),
                       numericInput('clusters', 'Cluster count', 9,
                                    min = 1, max = 9))
    }
  })



  outVar<-reactive({
    vars <-input$clusters
    return(strtoi(vars))
  })
  output$int1=renderUI({
    numericInput('clusters1', 'Cluster count', outVar(),
                 min = 1, max = outVar())
  })

  condVar<-reactive({
    vars<-input$subClust
    return(strtoi(vars))
  })

}
)

So in the UI, the tab of importance is the time clusters tab. I have a button that I can click that will create new tab pages. I then have the tab output and an attempt at an automated conditional panel output (does nothing).

I have 2 conditional panels defined explicitly, (I want to be able to put these all in uiOutput("condo"), but right now they're just used to highlight the difference in outcomes between explicitly creating conditional panels and trying to make them dynamically.

The server page has a method to output the tabs, a method that tries to make conditional panels (does nothing), and some other methods that relate to dynamic inputs. (input 2 resets to input 1, whenever input 1 changes).

Could anyone explain what I'm doing wrong with the conditional pages method in the server? (this is R's output of the explicit form)

> conditionalPanel(condition="input.level==1",
+                  numericInput('clusters', 'Cluster count', 9,
+                               min = 1, max = 9))
<div data-display-if="input.level==1">
  <label for="clusters">Cluster count</label>
  <input id="clusters" type="number" value="9" min="1" max="9"/>
</div> 

And this is R's output of my attempt

> conditionalPanel(condition=paste("input.level==",1,sep=""),
+                  numericInput('clusters', 'Cluster count', 9,
+                               min = 1, max = 9))
<div data-display-if="input.level==1">
  <label for="clusters">Cluster count</label>
  <input id="clusters" type="number" value="9" min="1" max="9"/>
</div> 

解决方案

Ok, I was able to find a solution, by looking at a relevant problem, the issues around creating tabs dynamically. I just changed tabs into conditional pages and ran a do.call with the main panel at the end to properly format my conditional pages.

What I've learned is you can't just make a for loop with a bunch of UI features and expect them to pop up (sadly, it doesn't work like that). Here's my server code that changed:

  output$condo=renderUI({
    ConPages<-as.list(rep(0,condVar()+1))
    for (i in 0:length(ConPages)){
      ConPages[i]=lapply(paste("input.level==",i,sep=""),conditionalPanel,numericInput('clusters', 'Cluster count', 9, min = 1, max = 9))
    }
    do.call(mainPanel,list(ConPages))
  })

这篇关于如何在Shiny R中使用动态创建的选项卡制作条件面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆