1

I want the user to input a value in a text box in shiny and then be able to click on a button to review the value s/he typed before. I got this to work with the code below, but it is far from ideal. For one thing it requires me to click the "go back" button twice to work (no idea why). Any suggestions are appreciated. (Also, note that I included the table of iterations in the output just for reference).

library(shiny)

q <- new.env() # create new environment
assign("iteration",1,envir = q)
log <- data.frame(iteration=NULL,id=NULL)
assign("log",log,envir = q)

ui <- fluidPage(
    actionButton("back", "go back"),
    textInput("id","enter id",value=1),
    tableOutput("x")
)


server <- function(input, output,clientData, session) {
  observeEvent(input$id,{
    id <- input$id
    iteration <- get("iteration", envir = q) # get iteration
    log <- get("log",envir = q) #'gets df'
    temp <- data.frame(iteration=iteration,id=id,stringsAsFactors = F)
    if (!nrow(log))  log <- rbind(log,temp) # for first iteration only
    else log[iteration,] <- temp
    assign("log",log,envir = q)
    iteration <- iteration+1
    assign("iteration",iteration,envir = q)
  })

  # back button

  observeEvent(input$back,{
    iteration <- get("iteration", envir = q) # get iteration
    iteration <- iteration-1
    assign("iteration",iteration,envir = q)
    log <- get("log",envir = q) #'gets df'
    #get data
    id <- log$id[iteration]
    updateTextInput(session,"id", value=id)
  })

  # for visualising table

  x <- eventReactive(input$id,{
    get("log",envir = q)
  })

  output$x <- renderTable({x()})
}

shinyApp(ui = ui, server = server)

I want a "go back" button than one clicked shows the value typed previously in the text box.

(A similar question exist, but I do believe mine is different enough).

Community
  • 1
  • 1
InspectorSands
  • 2,859
  • 1
  • 18
  • 33

0 Answers0