R Shiny:单击选项卡中的按钮会隐藏 button/questions/radio 按钮并在同一选项卡中显示结果
R Shiny: Clicking button in tab hides button/questions/radio buttons and displays results in the same tab instead
我有一个包含 n 个问题的选项卡,每个问题都与李克特风格的单选按钮相关联。我想以多种不同的方式(绘图等)显示所选答案,而无需切换到或创建其他选项卡。相反,我想在用于 questions/radio 按钮的同一选项卡中显示结果,即按下按钮隐藏问题、单选按钮和提交按钮并显示,例如,绘图或 table 与结果。
我创建了一个 MWE 来说明我目前的方法。由于我不知道如何最好地解决这个问题,我创建了三个不同的选项卡,分别称为 Questions
、Results
和 Hide
,目的是在提交按钮之前隐藏 Results
选项卡单击将显示 Results
选项卡并隐藏 Questions
选项卡。我还没有找到隐藏 Questions
选项卡的方法,这就是我创建 Hide
选项卡作为测试的原因。
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)
除了上面描述的明显错误之外,这段代码还有很多错误。例如,再次单击提交按钮会再次隐藏 'Results' 选项卡。
如有任何帮助或指点,我将不胜感激。
编辑:
感谢@YBS 的回答,这是有效的解决方案,它在单击按钮后也会隐藏它:
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)
也许您正在寻找这个。
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")
),
tabPanel(
title = "Results",
value = "results",
tableOutput("tableOutput"),
h1("Here are the results"),
textOutput("txt1")
),
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=" "))
})
output$txt1 <- renderText({
return(paste("The answers are:", input$Q1, "and", input$Q2, sep=" "))
})
observeEvent(input$button, {
k <- input$button %% 2
#print(k)
if (k==1) {
hide("myradios")
show("txt")
}else {
show("myradios")
hide("txt")
}
}, ignoreNULL = FALSE)
}
shinyApp(ui = ui, server = server)
我有一个包含 n 个问题的选项卡,每个问题都与李克特风格的单选按钮相关联。我想以多种不同的方式(绘图等)显示所选答案,而无需切换到或创建其他选项卡。相反,我想在用于 questions/radio 按钮的同一选项卡中显示结果,即按下按钮隐藏问题、单选按钮和提交按钮并显示,例如,绘图或 table 与结果。
我创建了一个 MWE 来说明我目前的方法。由于我不知道如何最好地解决这个问题,我创建了三个不同的选项卡,分别称为 Questions
、Results
和 Hide
,目的是在提交按钮之前隐藏 Results
选项卡单击将显示 Results
选项卡并隐藏 Questions
选项卡。我还没有找到隐藏 Questions
选项卡的方法,这就是我创建 Hide
选项卡作为测试的原因。
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)
除了上面描述的明显错误之外,这段代码还有很多错误。例如,再次单击提交按钮会再次隐藏 'Results' 选项卡。
如有任何帮助或指点,我将不胜感激。
编辑:
感谢@YBS 的回答,这是有效的解决方案,它在单击按钮后也会隐藏它:
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)
也许您正在寻找这个。
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")
),
tabPanel(
title = "Results",
value = "results",
tableOutput("tableOutput"),
h1("Here are the results"),
textOutput("txt1")
),
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=" "))
})
output$txt1 <- renderText({
return(paste("The answers are:", input$Q1, "and", input$Q2, sep=" "))
})
observeEvent(input$button, {
k <- input$button %% 2
#print(k)
if (k==1) {
hide("myradios")
show("txt")
}else {
show("myradios")
hide("txt")
}
}, ignoreNULL = FALSE)
}
shinyApp(ui = ui, server = server)