You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny单选框赋值求助:选中项生效其余为0,input$RI用于公式计算

Solution for Renal Impairment Select Input in Shiny

Got it, let's solve this problem step by step. Here's how you can create the select input in Shiny that sets the selected renal impairment category to an active value (e.g., 1) and the rest to 0, ready for your formula calculations:

1. UI Setup

First, we'll define the selectInput with clear display labels and backend-friendly identifiers. This makes it easier to reference each category in the server logic:

ui <- fluidPage(
  selectInput(
    inputId = "RI",
    label = "Renal Impairment:",
    choices = c(
      "Mild" = "mild",
      "Moderate" = "moderate",
      "Severe" = "severe",
      "End Stage" = "end_stage"
    ),
    selected = NULL # Leave as NULL for no default, or set e.g., "mild" to pre-select
  ),
  
  # Optional: Add outputs to test values and calculations
  verbatimTextOutput("ri_values_display"),
  textOutput("calculation_result")
)

2. Server Logic

Next, we'll create a reactive object that dynamically updates values based on the user's selection. This object will return a list where only the selected category has a non-zero value (we'll use 1 here, but you can adjust this to any number your formula needs):

server <- function(input, output) {
  # Reactive object to generate renal impairment values
  renal_impairment_values <- reactive({
    # Initialize all categories to 0
    values <- list(
      mild = 0,
      moderate = 0,
      severe = 0,
      end_stage = 0
    )
    
    # Set the selected category to your desired active value (e.g., 1)
    if (!is.null(input$RI)) {
      values[[input$RI]] <- 1 # Change this number if your formula uses different weights
    }
    
    values
  })
  
  # Optional: Display the values to verify they work as expected
  output$ri_values_display <- renderPrint({
    cat("Current Renal Impairment Values:\n")
    print(renal_impairment_values())
  })
  
  # Example: Use the values in your formula calculation
  final_calculation <- reactive({
    # Replace this with your actual formula
    base_value <- 50
    adjustment <- (renal_impairment_values()$mild * 5) + 
                  (renal_impairment_values()$moderate * 10) + 
                  (renal_impairment_values()$severe * 15) + 
                  (renal_impairment_values()$end_stage * 20)
    
    base_value + adjustment
  })
  
  # Output the calculation result
  output$calculation_result <- renderText({
    paste("Formula Result:", final_calculation())
  })
}

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

Key Notes

  • Customize Values: If your formula requires specific non-zero values (e.g., Mild = 0.8, Moderate = 0.5), just replace the 1 in values[[input$RI]] <- 1 with your desired number for each category (you could even use a separate lookup list if values vary widely).
  • Reactive Use: The renal_impairment_values() reactive object will automatically update whenever the user changes their selection, so your formula will always use the latest values.
  • Testing: The optional output elements (ri_values_display and calculation_result) help you debug and confirm the values are behaving correctly before integrating your full formula.

内容的提问来源于stack exchange,提问作者Nax Umer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:30:51