显示/隐藏基于numericInput和actionButton的输入

时间:2016-11-18 05:59:13

标签: r shiny shinyjs

闪亮的应用程序具有以下元素:

  • 一个numericInput字段,其值介于0和3之间
  • 一个uiOutput,其中包含三个隐藏的textInput字段
  • 一个actionButton

我想要实现的是点击按钮后show隐藏的textInput字段。显示的字段数取决于numericInput中选择的数字 下面的完整功能代码成功执行此操作;但是,有一个问题我似乎无法找到解决方案。例如,如果我选择3并单击按钮,则会显示3个隐藏的textInput字段(yay!),但如果我立即选择低于3的数字并单击按钮,不想要的字段仍然存在。我怎样才能做到这一点?感谢

library(shiny)
library(shinyjs)

ui <- fluidPage(

  useShinyjs(),

  numericInput(inputId = "num", label = "How many inputs do you want to show?", value = 1, min = 1, max = 3),

  uiOutput(outputId = "out"),

  actionButton(inputId = "go", label = "Click me!")
)

server <- function(input, output){

  output$out <- renderUI({
    numinputs <- lapply(1:3, function(i){
      textInput(inputId = paste0("txt", i), label = paste0("Text input ", i))
    })
    shinyjs::hidden(numinputs)
  })

  observeEvent(eventExpr = input$go, handlerExpr = {
    for(i in seq(input$num)){
      shinyjs::show(id = paste0("txt", i))
    }
  })
}

shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:1)

我已修改您的代码以完全按照您的意愿执行操作。

library(shiny)
library(shinyjs)

ui <- fluidPage(

  useShinyjs(),

  numericInput(inputId = "num", label = "How many inputs do you want to show?", value = 1, min = 1, max = 3),

  uiOutput(outputId = "out"),

  actionButton(inputId = "go", label = "Click me!")
)

server <- function(input, output){
  nout <- 0

  output$out <- renderUI({
    numinputs <- lapply(1:3, function(i){
      textInput(inputId = paste0("txt", i), label = paste0("Text input ", i))
    })
    shinyjs::hidden(numinputs)
  })

  observeEvent(eventExpr = input$go, handlerExpr = {


      if(nout > input$num){ # If the current no. of inputs is less than previous hide the inputs
        for(i in nout:(nout-as.numeric(input$num))){
          shinyjs::hide(id = paste0("txt", i)) 
        } 

      }else{
        for(i in seq(input$num)){
        shinyjs::show(id = paste0("txt", i)) 
      }

    }

    nout <<- input$num
  })
}

shinyApp(ui = ui, server = server)

希望它有所帮助!

答案 1 :(得分:1)

此代码类似于@SBista,但没有nout变量。

library(shiny)
library(shinyjs)

ui <- fluidPage(

    useShinyjs(),

    numericInput(inputId = "num", label = "How many inputs do you want to show?", value = 1, min = 1, max = 3),

    uiOutput(outputId = "out"),

    actionButton(inputId = "go", label = "Click me!")
)

server <- function(input, output){
    output$out <- renderUI({
        numinputs <- lapply(1:3, function(i){
            textInput(inputId = paste0("txt", i), label = paste0("Text input ", i))
        })
        shinyjs::hidden(numinputs)
    })

    observeEvent(eventExpr = input$go, handlerExpr = {
        n <- seq(length.out = as.numeric(input$num))
        lapply(seq(3), function(i) {
            if(i %in% n) {
                shinyjs::show(id = paste0("txt", i))
            } else{
                shinyjs::hide(id = paste0("txt", i))
            }
        })
    })
}

shinyApp(ui = ui, server = server)

#edit - 没有shinyjs / dinamically创建

# if you comment the lines marked with # no-button
# the app will change the number of textInputs as soon as you change the numericInput
library(shiny)

ui <- fluidPage(
    numericInput(inputId = "num", label = "How many inputs do you want to show?", value = 1, min = 1, max = 3),
    uiOutput(outputId = "out"),
    actionButton(inputId = "go", label = "Click me!") # no-button
)

server <- function(input, output){
    output$out <- renderUI({
        input$go # no-button
        isolate( # no-button
            numinputs <- lapply(seq(length.out = req(input$num)), function(i){
                textInput(inputId = paste0("txt", i), label = paste0("Text input ", i))
            })
        ) # no-button
    })
}

shinyApp(ui = ui, server = server)