cummulative
cummulative

Reputation: 3

Change the Value in numericInput when radioButtons selection changed

I want to change the value in a numericInput or sliderInput when different radio buttons are selected. I want to show the output of different models (just simple linear models) when the radio button is changed but leave the option to alter the gradient after selection.

Below is a simple reprex where the gradient can be selected and shown in the body when the radio buttons are changed but I cannot pass it to the numericInput. Is there a numericOuput? It appears that the textOutput passes data that cannot then be interpreted as a number.

library(shiny)

model_1_grad <- 1
model_2_grad <- 2
model_3_grad <- 3

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("REPREX Radio Button to Numeric Input"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            numericInput("num", "Take input from radio button", min = 0, max = 6, 
                         value = 3 # I want this to be changed by the radio button selection
                         # value = textOutput("gradient_val") # doesn't work, how can I pass a number instead of text? 
                         ), 
            radioButtons(inputId = "model_type", label = "Select Prefit Model", 
                         choices = c("Model 1", "Model 2", "Model 3"), selected = "Model 3")
        ),

        # Show a plot of the generated distribution
        mainPanel(
           textOutput("gradient_val")
        )
    )
)

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

    output$gradient_val <- renderPrint({
        ifelse(input$model_type == "Model 1", model_1_grad, 
               ifelse(input$model_type == "Model 2", model_2_grad, model_3_grad
               )
        )
    })
}

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

Upvotes: 0

Views: 46

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

You can create the numericInput in the server.

library(shiny)

model_1_grad <- 1
model_2_grad <- 2
model_3_grad <- 3

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("REPREX Radio Button to Numeric Input"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      uiOutput('num'),
      radioButtons(inputId = "model_type", label = "Select Prefit Model", 
                   choices = c("Model 1", "Model 2", "Model 3"), selected = "Model 3")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      textOutput("gradient_val")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$gradient_val <- renderPrint({
    ifelse(input$model_type == "Model 1", model_1_grad, 
           ifelse(input$model_type == "Model 2", model_2_grad, model_3_grad
           )
    )
  })
  
  output$num <- renderUI({
    val <- dplyr::case_when(input$model_type == "Model 1" ~ model_1_grad, 
                            input$model_type == "Model 2" ~ model_2_grad, 
                            input$model_type == "Model 3" ~ model_3_grad)
    numericInput("num", "Take input from radio button", min = 0, max = 6, 
                 value = val)
  })
  
}

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

I am not sure if it is possible in your actual application but instead of defining model_1_grad, model_2_grad etc in the global environment you can assign the values to radioButtons which will simplify your server logic.

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("REPREX Radio Button to Numeric Input"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      uiOutput('num'),
      radioButtons(inputId = "model_type", label = "Select Prefit Model", 
                   choices = c("Model 1" = 1, "Model 2" = 2, "Model 3" = 3), selected = 3)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      textOutput("gradient_val")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$gradient_val <- renderPrint({
      input$model_type
  })
  
  output$num <- renderUI({
    numericInput("num", "Take input from radio button", min = 0, max = 6, 
                 value = input$model_type)
  })
  
}

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

Upvotes: 0

Related Questions