0
votes

I have a dynamic UI with to select inputs in order to select the row and column of the data frame and a numeric input for introduce the new number. When I change a number and after I choose another row, shiny changes the number of the new row with the last number used.

server.R

shinyServer(function(input, output) {

  output$select1 = renderUI(
    selectInput("contrato","Selecciona un codi", c(variables[,1],"Select"), "Select") )

  output$select2 = renderUI(
    if (is.null(input$contrato) || input$contrato == "Select"){
        return()
    }else{ 
        selectInput("variable", "Selecciona una variable", c(colnames(variables)[-1], "Select"), "Select")
  })

  output$select3 = renderUI(
    if (is.null(input$variable) || input$variable == "Select" ){
        return()
    }else{ 
      num <- variables[which(input$contrato == variables$garantias), which(input$variable == colnames(variables))]
      numericInput("var", "Modificar:", num)
    })

  result <- reactive({
    if(is.null(input$variable) || is.null(input$contrato) ) {
      return()
    } else {
    variables[which(input$contrato == variables$garantias), which(input$variable == colnames(variables))] <<- as.numeric(input$var)
    return(variables)
    }
  })

  output$View = renderTable({
    if(is.null(input$contrato) || is.null(input$variable) ) {
      return()
    } else {
      result()
    } 

  })
})

ui.R

shinyUI(fluidPage(
  titlePanel("Test"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("select1"),
      uiOutput("select2"),
      uiOutput("select3")
    ),
    mainPanel(h3("Data frame: "),
              tableOutput("View"))
  )
))  

And the data frame:

variables <- data.frame(garantias = c("C00", "C11", "C22", "C33"), c.retencio = rep(2,4), t.mort = rep(3,4), comi = rep(4,4), desp = rep(5,4), PB = rep(6,4))

Thanks alot!

1

1 Answers

1
votes

The issue is that the app does not know when you want things to change. For example, if you wanted to change two rows of the same columns to be "4" all you would do is change the row number. But shiny can't tell if that is what you want to do when you change the row number, or if you are going to change the column number as well.

One way to deal with this is to put an actionButton in the ui and then only change the values when the button is pushed. So to the ui add below the 3rd uiOutput in the sidebarPanel:

actionButton(inputId=modificar, label="Modifica") 

and change in the server:

result <- reactive({
    input$modificar
    isolate({
      if(is.null(input$variable) || is.null(input$contrato) ) {
        return()
         } else {
         variables[which(input$contrato == variables$garantias),    
             which(input$variable == colnames(variables))] <<-  
             as.numeric(input$var)
         return(variables)
         }
   })
 })

This should isolate the change to variables so that those changes will only take place when the user hits the button.