MMPD
MMPD

Reputation: 5

How to use debounce in R Shiny without using dyplr or magrittr pipes

I'm trying to get debounce to work without using dplyr or magrittr as my server (at work) seems unable to render output when I load these libraries. I'm sure I'm missing something basic here but I can't seem to get my head around getting debounce to work for me.

ui.R

library(shiny)

fluidPage(
  titlePanel("Debounce Test")
  ,selectizeInput("rweek", "Select number(s):"
                  ,c(1,2,3)
                  ,multiple = TRUE
                  ,selected = "1"
                  ,width = '100%')
  ,textInput(inputId = "abctext", label = "Type anything")
  ,textOutput(outputId = "finalText")
)

server.R

server <- function (input, output) {


  finalSelections <- reactive ({
    typedtext <- input$abctext
    weeklist<- input$rweek
    countt <- length(weeklist)

    typedtextd <- debounce(typedtext,2000)
    weeklistd <- debounce(weeklist,2000)
    counttd <- length(weeklistd)

    final_data <- c(typedtextd, counttd)
    #final_data <- c(typedtext, countt)
    return(final_data)
  }) 

  output$finalText <- renderText({
    paste0("You have typed: ", finalSelections()[1], "; You have selected ", finalSelections()[2], " item(s)")
  })

  }

The code works fine when I comment out the debounce sections. I want to be able to wait a bit for the user to select inputs (especially SelectizeInput) before printing out the output.

Upvotes: 0

Views: 276

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33550

Welcome to Stackoverflow!

debounce() needs a reactive expression to work - Please see ?debounce(). In your code you are passing strings (inside a reactive expression).

I guess this is what you are after:

library(shiny)

ui <- fluidPage(
  titlePanel("Debounce Test"),
  selectizeInput("rweek", "Select number(s):", c(1, 2, 3), multiple = TRUE, selected = "1", width = '100%'),
  textInput(inputId = "abctext", label = "Type anything"),
  textOutput(outputId = "finalText")
)

server <- function(input, output, session) {

  typedtext <- reactive({input$abctext})
  weeklist <- reactive({input$rweek})

  typedtextd <- debounce(typedtext, 2000)
  weeklistd <- debounce(weeklist, 2000)


  finalSelections <- reactive ({
    countt <- length(weeklist())
    counttd <- length(weeklistd())

    final_data <- c(typedtextd(), counttd)
    #final_data <- c(typedtext(), countt)
    return(final_data)
  })

  output$finalText <- renderText({
    paste0(
      "You have typed: ",
      finalSelections()[1],
      "; You have selected ",
      finalSelections()[2],
      " item(s)"
    )
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions