Hi Peter,
Thanks for the reply.
Yep, it sounds good and indeed it will work...
But, in the context of my app, something that I don't want to see happen if continue in such way.
If you have time to spend, could you please check out this minimal case test ?
I tried to explain as clear as possible what I want to achieve.
English is not my mother tongue so I hope it will not be too messy.
Lastly, one cannot upload txt file in R community post thus ui.R and server.R are c/p below :
Many thanks,
(If somtehing wrong with this test app, feel free to contact me).
Laurent
ui.R
library(shiny)
library(shinythemes)
library(shinyWidgets)
shinyUI(
ui = fluidPage(
titlePanel("Minimal test case"),
fluidRow(
column(3,
selectInput(inputId = "choice",
label = "Make a choice",
choices = list("Choice_1" = "c1", "Choice_2" = "c2"), selected = "c1"),
verbatimTextOutput("textinfo")
),
column(3,
conditionalPanel(condition = "input.tabset1 == 'Tab1' | input.tabset1 == 'Tab2'",
uiOutput("input1")
),
conditionalPanel(condition = "input.tabset1 == 'Tab3'",
uiOutput("input1_bis")
)
)
),
fluidRow(
column(12,
tabsetPanel(id = "tabset1",
tabPanel("Main tab 1", value = "Tab1",
verbatimTextOutput("text_tab1")
),
tabPanel("Main tab 2", value = "Tab2",
verbatimTextOutput("text_tab2")
),
tabPanel("Secondary tab", value = "Tab3",
verbatimTextOutput("text_tab3")
)
)
)
)
)
)
SERVER.R
library(shiny)
library(shinythemes)
library(shinyWidgets)
server = function(input, output, session){
output$textinfo <- renderText({
paste("Info:",input$tabset1)
})
output$input1 <- renderUI({
# if (input$tabset1 == "Tab1" | input$tabset1 == "Tab2") {
# opt.pickerinput <- list(
# `actions-box` = TRUE,
# `deselect-all-text` = "Deselect all",
# `select-all-text` = "Select all",
# `none-selected-text` = "Make a choice",
# `live-search` = TRUE,
# `multiple-separator` = " | "
# ) } else {
# opt.pickerinput <- list(
# `max-options` = 1,
# `max-options-text` = "Only one choice!",
# `none-selected-text` = "Make a choice")
# }
# pickerInput("input1", "Picker input 1", choices = as.character(c(1:5)) , multiple = TRUE, options = opt.pickerinput)
pickerInput("input1", "Picker input 1", choices = as.character(c(1:5)) , multiple = TRUE, options = list(
`actions-box` = TRUE,
`deselect-all-text` = "Deselect all",
`select-all-text` = "Select all",
`none-selected-text` = "Make a choice",
`live-search` = TRUE,
`multiple-separator` = " | "
))
})
output$input1_bis <- renderUI({
if(input$tabset1 == "Tab3") {
pickerInput("input1_bis", "Picker input 1 for Secondary tab", choices = as.character(c(1:5)) , multiple = TRUE, options = list(
`max-options` = 1,
`max-options-text` = "Only one choice!",
`none-selected-text` = "Make a choice"))
} else {
NULL
}
})
output$text_tab1 <- renderText({
"I have nothing to say :-) Input 1 works.
Please select something and then switch to Main tab 2"
})
output$text_tab2 <- renderText({
"Normally, Picker input 1 would have been flushed since input1 is reevaluated on server side. Indeed there is an if() statement in the renderUI(input1).
In the context of this app, this is an issue since I want to keep available user's choices made in Main tab 1.
As one can see, to avoid this phenomenon, I had no choice to set up another renderUI(picker Input) for the Secondary tab. Therefore, there's no if() statement in input1 (check commented code) anymore.
In this way, input1 won't be reevaluated and user's choices remain visible in Main tab 2:-)
BUT
I hope that these changes (i.e. other options in pikcerInput) could be done more straightforward with shinyWidget::updatePickerInput on client side
Example :
observeEvent(input$tabset1, {
if (input$tabset1 == Tab3) {
updatePickerInput(session, inputId = input1, selected = , options = list(
max-options = 1,
max-options-text = Only one choice!,
none-selected-text = Make a choice))
This would avoid the need to create another pickerinput on server side for this kind of app"
})
output$text_tab3 <- renderText({
"For this tab, it's not a big deal if Picker input 1 is flushed..."
})
}