Reputation: 1
I have been trying to create a selection list box in shiny for different options such as Mild, Moderate, Severe and End Stage renal impairment. I want to write code that works with the value of one of the selection options, and the rest should be 0. Can anyone please help me? Another thing is that the input data is going to be used in an equation that is called input$RI (Renal Impairment). Here is the code that I am trying to use:
In UI:
fluidPage(
selectInput("RI",
"Renal Impairment:",
choices = list("Normal" = 1,
"Mild" = 2,
"Moderate + Severe" = 3,
"End Stage" = 4),
selected = 1),
hr(),
fluidRow(column(4, verbatimTextOutput("0")))
)
In Server File:
function(input, output) {
output$RI <- renderPrint({ input$RI })
if (RI == 1) {
1 <- 0
2 <- 0
3 <- 0
4 <- 0
}
if (RI == 2) {
1 <- 0
2 <- 1
3 <- 0
4 <- 0
}
if (RI == 3) {
1 <- 0
2 <- 0
3 <- 1
4 <- 0
}
if (RI == 4) {
1 <- 0
2 <- 0
3 <- 0
4 <- 1
}
}
Thank you in advance.
Upvotes: 0
Views: 1415
Reputation: 2695
You need to assign variables appropriately. You need to wrap reactive output in reactive spaces. If you need to return multiple items, you need to wrap those items into a single list.
library(shiny)
ui <- fluidPage(
selectInput("RI",
"Renal Impairment:",
choices = list("Normal" = 1,
"Mild" = 2,
"Moderate + Severe" = 3,
"End Stage" = 4),
selected = "Normal"),
br(),
fluidRow(column(4, verbatimTextOutput("myoutput")))
)
server <- function(input, output) {
RI <- reactive({
if (input$RI == 1) {
a <- 0
b <- 0
c <- 0
d <- 0
out <- list(a,b,c,d)
}
if (input$RI == 2) {
a <- 0
b <- 1
c <- 0
d <- 0
out <- list(a,b,c,d)
}
if (input$RI == 3) {
a <- 0
b <- 0
c <- 1
d <- 0
out <- list(a,b,c,d)
}
if (input$RI == 4) {
a <- 0
b <- 0
c <- 0
d <- 1
out <- list(a,b,c,d)
}
return(out)
})
output$myoutput <- renderPrint({
RI()
})
}
shiny::shinyApp(ui = ui, server = server)
Upvotes: 1