【问题标题】:Alternative To Conditional Panel In Shiny R [duplicate]Shiny R中条件面板的替代方案[重复]
【发布时间】:2019-12-14 12:04:58
【问题描述】:

我有一个应用程序,我想根据用户选择的数字添加不同数量的 UiOutputs。现在我发现自己做了很多冗余的条件面板来创建我正在寻找的结果。我想知道是否有一种更简单的方法可以通过“switch”之类的功能产生所需的结果。

这是使用条件面板的代码。结果是可取的。条件面板的数量和重复次数不大。

我只会显示 2 个条件面板,但在我的应用程序中我有 9 个。该应用程序只是询问用户他们想在其投资组合中选择多少股票以及每只股票的股票数量。

library(shiny)

tickers = c("SPY", "IWM", "QQQ")

ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    mainPanel(
        h3("lets test the corr Mat"),
        #inputs stock
        #inputs weight
       radioButtons("howMany", "How Many ETF's?", choices = c(1, 2, 3, 4, 5, 6), selected = 1),
        actionButton("stest", "Stress Portfolio"),
       conditionalPanel("input.howMany == '1'", 
                        selectizeInput("stock1", "choseStock 1", choices = tickers, selected = "SPY"),
                        numericInput("weight1", "Choose Number of Shares 1", value = 100, min = 0, max = NA, step = 1)),
       conditionalPanel("input.howMany == '2'",
                        selectizeInput("stock1", "choseStock 1", choices = tickers, selected = "SPY"),
                        selectizeInput("stock2", "choseStock 2", choices = tickers, selected = "QQQ"),
                        numericInput("weight1", "Choose Number of Shares 1", value = 100, min = 0, max = NA, step = 1),
                        numericInput("weight2", "Choose Number of Shares 2", value = 100, min = 0, max = NA, step = 1))
    )


)

# Define server logic required to draw a histogram
server <- function(input, output) {



}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny shinydashboard shiny-reactivity


    【解决方案1】:

    凭借我有限的 Shiny / R 经验,我看到了两种方法:一种是动态生成变量名,另一种是稍微修饰一下。我不相信我能做到前者的正义,所以这里有一种方法可以做到后者:

    tickers = c("SPY", "IWM", "QQQ")
    
    ui <- fluidPage(
    
      # Application title
      titlePanel("Old Faithful Geyser Data"),
    
      mainPanel(
        h3("lets test the corr Mat"),
        radioButtons("howMany", "How Many ETF's?", choices = c(1, 2, 3, 4, 5, 6), selected = 1),
        actionButton("stest", "Stress Portfolio"),
    
        fluidRow(
          column(width=6, selectizeInput("stock1", "choseStock 1", choices = tickers, selected = "SPY")),
          column(width=6, numericInput("weight1", "Choose Number of Shares 1", value = 100, min = 0, max = NA, step = 1))
      ),
    
    # This is really all I had to offer
        conditionalPanel("input.howMany > 1",
                    fluidRow(
                      column(width=6, selectizeInput("stock2", "choseStock 2", choices = tickers, selected = "QQQ")),
                      column(width=6, numericInput("weight2", "Choose Number of Shares 2", value = 100, min = 0, max = NA, step = 1))
                    )
        )
    
      )
    
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
    
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    第 17 行显示我的两分钱:保留 stock1weight1,然后稍微更改您的 conditionalPanel 逻辑以添加新输入。使用fluidRow 和一对columns,连续的变量有自己的行。

    扩展它不会更干净,但至少每个conditionalPanel 只会有两个新输入,这会稍微缩短你的代码

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 2016-08-14
      • 2014-11-18
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多