0

I have an app where I would like to add different numbers of UiOutputs based on a number chosen by the user. Right now I am finding myself doing a ton of redundant conditional panels to create the outcome I am looking for. I was wondering if there is an easier way to produce the desired outcome through a function like "switch".

Here is the code using conditional Panels. The outcome is desirable. The amount of conditional panels and repetition is not.

I will only show with 2 conditional Panels but in my app I have 9. The app simply asks the users how many stocks they want to chose in their portfolio and the amount of shares for each stock.

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)

enter image description here

enter image description here

Jordan Wrong
  • 1,205
  • 1
  • 12
  • 32

1 Answers1

0

With my limited Shiny / R experience, I see two routes: one in which you dynamically generate variable names, and one where you just pretty it up a bit. I'm not confident I can do the former justice, so here's a way to do the latter:

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)

Line 17 calls out my two cents: keep stock1 and weight1, then change your conditionalPanel logic a bit to add in the new inputs. With fluidRow and a pair of columns, successive variables have their own rows.

Extending this wouldn't be much cleaner, but at least each conditionalPanel would only have two new inputs and that would shorten your code a little

Punintended
  • 727
  • 3
  • 7