Home > Enterprise >  Add a new column to a stored csv file everytime you press an actioButton in a shiny app
Add a new column to a stored csv file everytime you press an actioButton in a shiny app

Time:10-15

I have the shiny app below with 3 actionButtons. I want each time I press an actionButton to update the csv I store in my working directory with a new column. The logic will be the following:

Lets' say the first time I press the 1st actionButton "Je choisis option A" then I get a csv with a column name Bet1 and as row "Je choisis option A". The second time I press the 1st again and the csv now has a second column named "Bet2" and as row "Je choisis option A". The third time I press the second actionButton named "Je choisis le sac avec A et B" and the csv now has a third coumn named "Bet 3" and as row "Je choisis le sac avec A et B". This can happen up to 6 times. The example above will give us this.

Bet1                Bet2                          Bet3
1 Je choisis option A Je choisis option A Je choisis le sac avec A et B

#app

library(shiny)
library(shinyjs)

################ cbind datasets with different number of rows  ######
cbindPad <- function(...){
  args <- list(...)
  n <- sapply(args,nrow)
  mx <- max(n)
  pad <- function(x, mx){
    if (nrow(x) < mx){
      nms <- colnames(x)
      padTemp <- matrix(NA, mx - nrow(x), ncol(x))
      colnames(padTemp) <- nms
      if (ncol(x)==0) {
        return(padTemp)
      } else {
        return(rbind(x,padTemp))
      }
    }
    else{
      return(x)
    }
  }
  rs <- lapply(args,pad,mx)
  return(do.call(cbind,rs))
}

ui <- fluidPage(
  id="main",
  title="Risk and ambiguity",
  useShinyjs(),
  
  
                        
                        fluidRow(column(12, align='center',
                                        hr("Choisissez urne A, urne B ou un sac avec A et B:"))), DTOutput("t1"),
                        
                        ####
                        fluidRow(wellPanel(
                          splitLayout(cellWidths = c("33%", "33%", "33%"),
                                      column(12,align="center",actionButton("action11", label = "Je choisis option A")),
                                      column(12,align="center",actionButton("action12", label = "Je choisis le sac avec A et B")),
                                      column(12,align="center",actionButton("action13", label = "Je choisis option B"))) ))
               
               
               
  )

server <- function(input, output, session){
  rv <- reactiveValues(col1=NULL, col2=NULL,col3=NULL, col3=NULL,col5=NULL, col6=NULL, df=NULL)
  mylabel <- c("Je choisis option A", "Je choisis le sac avec A et B", "Je choisis option B")
  
  lapply(1:3, function(i){
    observeEvent(input[[paste0("action1",i)]], {
      if (is.null(rv$col1)) {
        rv$col1 <- mylabel[i]
      }else rv$col1 <<- c(rv$col1,mylabel[i])
    }, ignoreInit = TRUE)
    
  })
  lapply(1:3, function(i){
    observeEvent(input[[paste0("action2",i)]], {
      if (is.null(rv$col2)) {
        rv$col2 <- mylabel[i]
      }else rv$col2 <<- c(rv$col2,mylabel[i])
    })
  })
  lapply(1:3, function(i){
    observeEvent(input[[paste0("action3",i)]], {
      if (is.null(rv$col3)) {
        rv$col3 <- mylabel[i]
      }else rv$col3 <<- c(rv$col3,mylabel[i])
    }, ignoreInit = TRUE)
    
  })
  lapply(1:3, function(i){
    observeEvent(input[[paste0("action4",i)]], {
      if (is.null(rv$col4)) {
        rv$col4 <- mylabel[i]
      }else rv$col4 <<- c(rv$col4,mylabel[i])
    })
  })
  lapply(1:3, function(i){
    observeEvent(input[[paste0("action5",i)]], {
      if (is.null(rv$col5)) {
        rv$col5 <- mylabel[i]
      }else rv$col5 <<- c(rv$col5,mylabel[i])
    }, ignoreInit = TRUE)
    
  })
  lapply(1:3, function(i){
    observeEvent(input[[paste0("action6",i)]], {
      if (is.null(rv$col6)) {
        rv$col6 <- mylabel[i]
      }else rv$col6 <<- c(rv$col6,mylabel[i])
    })
  })
  
  observe({
    rv$df <- cbindPad(data.frame(Bet1 = rv$col1),data.frame(Bet2 = rv$col2),
                      data.frame(Bet3 = rv$col3),data.frame(Bet4 = rv$col4),
                      data.frame(Bet4 = rv$col5),data.frame(Bet6 = rv$col6))
    write.csv(rv$df
              , file = "solution.csv"
              , row.names=F
    )
  })
  
}


shinyApp(ui = ui, server = server)

CodePudding user response:

Try this

library(shiny)
library(shinyjs)
library(DT)

################ cbind datasets with different number of rows  ######
cbindPad <- function(...){
  args <- list(...)
  n <- sapply(args,nrow)
  mx <- max(n)
  pad <- function(x, mx){
    if (nrow(x) < mx){
      nms <- colnames(x)
      padTemp <- matrix(NA, mx - nrow(x), ncol(x))
      colnames(padTemp) <- nms
      if (ncol(x)==0) {
        return(padTemp)
      } else {
        return(rbind(x,padTemp))
      }
    }
    else{
      return(x)
    }
  }
  rs <- lapply(args,pad,mx)
  return(do.call(cbind,rs))
}

ui <- fluidPage(
  id="main",
  title="Risk and ambiguity",
  useShinyjs(),
  
  fluidRow(column(12, align='center',
                  hr("Choisissez urne A, urne B ou un sac avec A et B:"))), DTOutput("t1"),
  
  ####
  fluidRow(wellPanel(
    splitLayout(cellWidths = c("33%", "33%", "33%"),
                #uiOutput("myactions")
                column(12,align="center",actionButton("action11", label = "Je choisis option A")),
                column(12,align="center",actionButton("action12", label = "Je choisis le sac avec A et B")),
                column(12,align="center",actionButton("action13", label = "Je choisis option B"))
                )))
)

server <- function(input, output, session){
  rv <- reactiveValues(col1=NULL, col2=NULL, col3=NULL, col4=NULL, col5=NULL, col6=NULL, df=NULL, btn1=0,btn2=0,btn3=0)
  mylabel <- c("Je choisis option A", "Je choisis le sac avec A et B", "Je choisis option B")
  
  observeEvent(input[[paste0("action1",1)]], {
    rv$btn1 = 1
    rv$btn2 = 0
    rv$btn3 = 0
  }, ignoreInit = TRUE)
  observeEvent(input[[paste0("action1",2)]], {
    rv$btn1 = 0
    rv$btn2 = 1
    rv$btn3 = 0
  }, ignoreInit = TRUE)
  observeEvent(input[[paste0("action1",3)]], {
    rv$btn1 = 0
    rv$btn2 = 0
    rv$btn3 = 1
  }, ignoreInit = TRUE)
  
  observe({
    nclick <- sum(as.numeric(input$action11)   as.numeric(input$action12)   as.numeric(input$action13))
    if (nclick>0 & nclick<7){
      if (rv$btn1) rv[[paste0("col",nclick)]] <- mylabel[1]
      if (rv$btn2) rv[[paste0("col",nclick)]] <- mylabel[2]
      if (rv$btn3) rv[[paste0("col",nclick)]] <- mylabel[3]
    }
  })
  
  observe({
    rv$df <- cbindPad(data.frame(Bet1 = rv$col1),data.frame(Bet2 = rv$col2),
                      data.frame(Bet3 = rv$col3),data.frame(Bet4 = rv$col4),
                      data.frame(Bet5 = rv$col5),data.frame(Bet6 = rv$col6))
    write.csv(rv$df
              , file = "solution.csv"
              , row.names=F
    )
  })
  output$t1 <- renderDT(rv$df)
}

shinyApp(ui = ui, server = server)
  • Related