Reputation: 133
I am trying to create widgets in Shiny by using for loop. each block contains:
I want to do condition for showing or hiding the two numeric inputs according to the value of the check box and the value of the choices selector. In the for loop that I created I added an index for each widget variable. I am wondering how can I execute the condition of the conditional panel in the for loop knowing that the variables are indexed in their names. Thanks for helping. I share with you the server.R and the UI.R scripts:
library(shiny)
shinyServer(function(input, output) {
output$input_value2 <- renderUI({
fluidRow(
lapply(1:8, function(i) {
column(width = 4,
output[[paste0('b', i)]] <- renderUI({strong(paste0('Variable N ', i))}),
checkboxInput(label = 'Inclued',paste0('c', i)),
selectInput(paste0('popDist', i), "Distribution",list("Normal" = "normal","Uniforme" = "uniforme")),
conditionalPanel(
condition = "eval(parse(text=paste0('input.popDist',i)))== 'uniforme'& (eval(parse(text=paste0('input.c',i)))==1",
numericInput(paste0('min', i), label = "Min:", value = 1),
numericInput(paste0('max', i), label = "Max:", value = 2)
),
conditionalPanel(
condition = "eval(parse(text=paste0('input.popDist',i)))== 'uniforme'& (eval(parse(text=paste0('input.c',i)))==1",
numericInput(paste0('mean', i), label ="mean:", value = 0),
numericInput(paste0('sd', i), label = "st. dev.:", value = 1)
)
)
})
)
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("TEST conditional panel"),
uiOutput("input_value2")
))
Upvotes: 2
Views: 820
Reputation: 25375
Your first condition should be:
paste0("input.popDist",i,"== 'uniforme'& input.c",i,"==1")
and the second:
paste0("input.popDist",i," == 'normal'& input.c",i,"==1")
so they become, with i=1
:
"input.popDist1== 'uniforme'& input.c1==1"
and
"input.popDist1 == 'normal'& input.c1==1"
respectively.
Working example:
library(shiny)
library(shinyjs)
server <- shinyServer(function(input, output) {
output$input_value2 <- renderUI({
fluidRow(
lapply(1:8, function(i) {
column(width = 4,
output[[paste0('b', i)]] <- renderUI({strong(paste0('Variable N ', i))}),
checkboxInput(label = 'Inclued',paste0('c', i)),
selectInput(paste0('popDist', i), "Distribution",list("Normal" = "normal","Uniforme" = "uniforme")),
conditionalPanel(
condition = paste0("input.popDist",i,"== 'uniforme'& input.c",i,"==1"),
numericInput(paste0('min', i), label = "Min:", value = 1),
numericInput(paste0('max', i), label = "Max:", value = 2)
),
conditionalPanel(
condition = paste0("input.popDist",i," == 'normal'& input.c",i,"==1 "),
numericInput(paste0('mean', i), label ="mean:", value = 0),
numericInput(paste0('sd', i), label = "st. dev.:", value = 1)
)
)
})
)
})
})
library(shiny)
ui <- shinyUI(fluidPage(
useShinyjs(),
titlePanel("TEST conditional panel"),
uiOutput("input_value2")
))
shinyApp(ui,server)
Upvotes: 2