'R Shiny: Clicking button in tab hides button/questions/radio buttons and displays results in the same tab instead
I have a tab with n questions, each associated with Likert style radio buttons. I would like to display the selected answers in a number of different ways (plots etc.) without switching to or creating additional tabs. Instead, I would like to display the results in the same tab that I used for the questions/radio buttons i.e. pressing the button hides the questions, radio buttons and the submit button and displays, for example, a plot or a table with the results.
I created a MWE that illustrates my approach so far. Since I don't know how to best approach this I created three different tabs called Questions, Results and Hide with the intention to hide the Results tab until the submit button is clicked which will display the Results tab and hide the Questions tab. I haven't found a way to hide the Questions tab which is why I created the Hide tab as a test.
library(shiny)
library(shinyjs)
ui <- tagList(
useShinyjs(),
navbarPage(
"Hide questions/buttons and display results in same tab",
id = "navbar",
tabPanel(title = "Questions",
id = "questions",
h1("Example questions"),
uiOutput("myradios"),
actionButton("button", "Run analysis")
),
tabPanel(
title = "Results",
value = "results",
tableOutput("tableOutput"),
h1("Here are the results"),
textOutput("txt")
),
tabPanel(
title = "Hide",
value = "hide",
h1("This tab will be hidden when the button in 'Questions' tab is clicked")
)
)
)
server <- function(input, output, session) {
observe({
hide(selector = "#navbar li a[data-value=results]")
show(selector = "#navbar li a[data-value=hide]")
})
output$tableOutput <- renderTable({
if(input$button > 0){
toggle(selector = "#navbar li a[data-value=results]")
toggle(selector = "#navbar li a[data-value=hide]")
}else{
NULL
}
})
outputOptions(output, "tableOutput", suspendWhenHidden=F)
nameList <- c("Q1", "Q2")
questionList <- c("This is question 1", "This is question 2")
all_radios <- list()
for (i in 1:length(nameList)) {
all_radios[[i]] <- p(radioButtons(nameList[i], questionList[i], c("Disagree strongly" = "1", "Disagree a little" = "2","Neutral; no opinion" = "3","Agree a little" = "4","Agree strongly" = "5"), inline=T, selected=0))
}
output$myradios <- renderUI(all_radios)
output$txt <- renderText({
return(paste("The answers are:", input$Q1, "and", input$Q2, sep=" "))
})
}
shinyApp(ui = ui, server = server)
A lot is wrong with this code besides the obvious described above. Clicking the submit button again hides the 'Results' tab again for example.
I would be very grateful for any kind of help or pointer.
Edit:
Thanks to @YBS' answer, this is the working solution which also hides the button after clicking it:
library(shiny)
library(shinyjs)
ui <- tagList(
useShinyjs(),
navbarPage(
"Hide questions/buttons and display results in same tab",
id = "navbar",
tabPanel(title = "Questions",
id = "questions",
h1("Example questions"),
uiOutput("myradios"),
textOutput("txt"),
actionButton("button", "Run analysis")
)
)
)
server <- function(input, output, session) {
nameList <- c("Q1", "Q2")
questionList <- c("This is question 1", "This is question 2")
all_radios <- list()
for (i in 1:length(nameList)) {
all_radios[[i]] <- p(radioButtons(nameList[i], questionList[i], c("Disagree strongly" = "1",
"Disagree a little" = "2",
"Neutral; no opinion" = "3",
"Agree a little" = "4",
"Agree strongly" = "5"),
inline=T,
selected=0))
}
output$myradios <- renderUI(all_radios)
output$txt <- renderText({
return(paste("The answers are:", input$Q1, "and", input$Q2, sep=" "))
})
observeEvent(input$button, {
k <- input$button %% 2
if (k==1) {
hide("myradios")
show("txt")
hide("button")
}else {
show("myradios")
hide("txt")
}
}, ignoreNULL = FALSE)
}
shinyApp(ui = ui, server = server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
