【发布时间】:2021-06-17 05:56:40
【问题描述】:
我有两个actionButtons 允许在数字 1-6 中导航。但是,我希望仅在显示数字 1 时删除“后退”按钮(但否则保留),并在数字 6 上删除“下一步”按钮(并保留在数字 1-5 中),以便提供关于序列开始和结束的视觉辅助。
请在底部查看我不成功的代码(#commented out 因为它会阻止成功过渡到数字 2-5)。
如何有条件地渲染这些按钮?
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
fluidRow(
div(column(5,uiOutput("back"),align= "right")),
div( column(5,uiOutput("next_q")),align = "left")),
fluidRow(
column(5,offset = 3,id = "qa1", uiOutput("ui_q1"),br(),br(),
)),
)
server <- function(input, output, session) {
qbank=reactive(as.vector(c(1,2,3,4,5,6)))
values <- reactiveValues()
values$count <- 1
myReactives <- reactiveValues(reactInd = 0)
observe({
input$next_q
myReactives$reactInd <- 1
})
observe({
input$back
myReactives$reactInd <- 2
})
ntext <- eventReactive(input$next_q,{
if(values$count != length(qbank())){
values$count <- values$count + 1
return(qbank()[values$count])
}
else{
return(qbank()[length(qbank())])
}
})
btext <- eventReactive(input$back,{
if(values$count != 1){
values$count <- values$count - 1
return(qbank()[values$count])
}
else{
return(qbank()[1])
}
})
q <- reactive({
if(input$next_q == 0){
return(qbank()[1])
}
else if(myReactives$reactInd==1) return(
ntext())
else if(myReactives$reactInd==2)return(
btext())
})
q1 = reactive((q()[1]))
output$q1 = renderText(q1())
output$ui_q1 = renderUI(textOutput("q1"))
output$back = renderUI({
## I cannot get the following if statement to function correctly
# if(values$count==1) return (NULL)
# else
(actionButton("back","back"))})
output$next_q = renderUI(
## I cannot get the following if statement to function correctly
# if(values$count==length(qbank()))
# return (NULL)
# else
actionButton("next_q","next"))
}
shinyApp(ui, server)
谢谢
【问题讨论】: