eknagi
eknagi

Reputation: 3

How to feed a value from an output object as an input into another output object?

I'm working on a simulation-based app. I have 2 initial numeric input objects, "max imp count" and "population size". Based on what is fed into these numeric input objects, the app generates 2 more input objects (really as output objects)- "select_proportion" and "select_probability". If the "max imp count" is 3, the app should generate 8 new input objects- 4 which ask for proportion (proportion0, proportion1, proportion2, proportion3), and 4 of which ask for probability0, probability1, probability2 and probability3. I want to feed these proportion and probability values into sample functions that work in the following manner:

1) sample(c(0,input$max_imp, 1), size=input$population, replace=TRUE, prob= these take the proportion values

sample for binary values for all proportion brackets:
2) sample(c(0,1), length(proportion_i), replace=TRUE, prob=these take the probability values)

Ideally, I would like to have this all in a dataframe where I have columns for which proportion bracket a record belongs to and whether they have 0 or 1.

Code:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Simulation"),
  dashboardSidebar(
    sidebarMenu(
      numericInput("max_imp", "max imp count", 0, min = 0, max = 15, step = 1),
      numericInput("population", "population size", 1, min = 0, max = 100000, step = 1),
      menuItemOutput("menuitem")
    )
  ),
  dashboardBody(
    uiOutput("select_proportion"),
    uiOutput("select_probability")
  ))


server <- function(input, output) {

    output$select_proportion = renderUI(
      lapply(0:(input$max_imp), function(i){
      numericInput(inputId = "i1", label = paste0("proportion",i), 0, min = 0, max = 1, step = 0.05)}))

    output$select_probability = renderUI(
    lapply(0:(input$max_imp), function(i){
      numericInput(inputId = "i2", label = paste0("probability",i), 0, min = 0, max = 1, step = 0.05)}))

}

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

Upvotes: 0

Views: 274

Answers (1)

Sebastian Vock
Sebastian Vock

Reputation: 1

Your output elements are only UI container which are then filled with the input elements defined in the renderUI functions. You can easily access the values of these input elements as you do it with your other inputs.

The only thing here is that you have to set the IDs of the inputs in a dynamic way e.g. instead of id = "i2" for the second input use id = paste("input", i, "2", sep = "-"). This will create you input$max_imp + 1 different inputs. The values can then be accessed via input$input_1_2.

BR Sebastian

Upvotes: 0

Related Questions