Save rective values


#1

HI,
I need to keep some records entered by the user, including the folio number, which is a reactivevalue and is the only value not saved. My code is:

saveData <- function(datas) {
  datas<- as.data.frame(t(datas))
  if (exists("responses")) {
    responses <<- rbind(responses, datas)
  } else {
    responses <<- datas
  }
}
loadData <- function() {
  if (exists("responses")) {
    responses
  }    }
my_username <- c("supervisor","corrector 1","corrector 2","corrector 3","corrector 4") 
my_password <- c("0000","1111","2222","3333","4444") 

get_role=function(user){ 
  if(user!="supervisor") { 
    return("corrector") 
  }else{ 
    return("ADMIN") 
  } 
} 

get_ui=function(role){ 
  if(role=="corrector"){ 
    return(list_field_user)
  }else{ 
    return(list_field_admin) 
  } 
} 




shinyServer(function(input, output,session) {
  
  USER <- reactiveValues(Logged = FALSE,role=NULL) 
  Folio <- reactiveValues(num=2211)
  "Numero de Folio" = isolate(as.numeric(Folio$num))
  fields <- c("Numero de folio"="Numero de Folio","Evaluacion"="evalID","Jornada"="jornadaID","sujeto"="userName",
              "Comentario"="comentID","Fecha"="date")
  ui1 <- function(){
    tagList(
      div(id = "login", 
          wellPanel(textInput("userName", "Usuario"), 
                    passwordInput("passwd", "Contrasena"), 
                    br(),actionButton("Login", "Ingresar"))) 
      #tags$style(type="text/css", '#login{ width:750px; float:left;}') 
      
    )} 
  
  ui2 <- function(){tagList(tabPanel("",get_ui(USER$role)))} 
  
  output$pass <- renderText({  
    if (USER$Logged == FALSE) {
      USER$pass
    }  
  })
  
  observe({ 
    if (USER$Logged == FALSE) { 
      if (!is.null(input$Login)) { 
        if (input$Login > 0) { 
          Username <- isolate(input$userName) 
          Password <- isolate(input$passwd) 
          Id.username <- which(my_username == Username) 
          Id.password <- which(my_password == Password) 
          if (length(Id.username) > 0 & length(Id.password) > 0) { 
            if (Id.username == Id.password) { 
              USER$Logged <- TRUE 
              USER$role=get_role(Username) 
              USER$name <- Username
            } 
          }  else { USER$pass <- "Usuario o contrasena incorrecta. Intente nuevamente"}
        } 
      } 
    }
  }) 
  
  # control logout
  observeEvent(input$logout , {
    USER$Logged <- FALSE
    USER$pass <- ""
  })
  
  
  observe({ 
    if (USER$Logged == FALSE) { 
      
      output$page <- renderUI({ 
        div(class="outer",do.call(bootstrapPage,c("Ingrese sus datos:",ui1()))) 
      }) 
    } 
    
    if (USER$Logged == TRUE) { 
      USER$name <- input$userName
      output$page <- renderUI({
        div(class="outer",do.call(navbarPage,c(inverse=TRUE,title = "Bienvenido",ui2()))) 
      }) 
      #print(ui) 
    } 
  })

  getDat <- eventReactive(input$search,{
    withProgress(
      message = 'Cargando',
      detail = 'get bd data', value=0 , {
        
        setFolio <- isolate(Folio$num)
        
        incProgress(0.5)
        
        if (!is.null(setFolio)) {
          Dat <- bd[which(bd$Idprueba %in% setFolio),]    
        } else {
          Dat <- NULL
        }
        
        setProgress(1)
      })
    return(Dat)
  })  #

  output$image2 <- renderImage({

    if (is.null(Folio$num))
      return(NULL)

    if (Folio$num == paste0(Folio$num) ) {
      return(list(
        src = paste0("www/",Folio$num,".jpg",sep=""), height = 650, width = 800,
        contentType = "image/jpeg"
      ))
    } else {
      return(NULL)
    }
  }, deleteFile = FALSE)
  
  # Whenever a field is filled, aggregate all form data
  formData <- reactive({
    datas<- sapply(fields, function(x) input[[x]])
    datas
  })
  
  # When the Submit button is clicked, save the form data
  
  observeEvent(input$submit, {
    saveData(formData())
    Folio$num <- Folio$num+1
  })
  
  # Show the previous responses
  # (update with current response when Submit is clicked)
  output$responses <- DT::renderDataTable({
    input$submit
    loadData()
  })
  output$Name <- renderText({
    paste("Hola",USER$name)
  })
  output$Fol <- renderText({
    paste("Numero de folio: ",Folio$num)
  })

})

#2

Hi! Welcome!

This is a lot of code for someone who’s unfamiliar with your project to try to parse through — I think you’ll have more success getting help with your problem if you can make a smaller, runnable-by-anyone example that demonstrates just the core thing you are trying to figure out how to do.

For some more advice about asking shiny questions, see: