Append data to existing dataframe in R

Hi Is there a way to append data to existing data in R. For example below application has dataframe populated based on user values , but right now, it is only creating new row and not appending.

Can we append this dataframe. I tried using reactive values as well. But did not work. Is there a way to achieve this?

library(shiny)

ui <- basicPage(
  
  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      textInput('c', 'Text A',"c1"),
      textInput('d', 'Text B',"d1"),
      textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1"),
      uiOutput('f1')
    ),
    column(
      width = 6,
      tags$p(tags$span(id = "valueA", "")),
      tags$script(
        "$(document).on('shiny:inputchanged', function(event) {
          if (event.name === 'a') {
            $('#valueA').text(event.value);
          }
        });
        "
      )
      ,tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){
  
  output$f1 <- renderUI({
    if(input$a == "a2"){
      textInput('z', 'Text B',"z1")
    } else {
      NULL
    }
  })
  
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
    # print(x)
    # data.frame(
    #   names = names(x),
    #   values = paste0(unlist(x, use.names = FALSE), Sys.time())
    # )
    
    tibble(
      key = names(x),
      data = x
    ) %>% 
      mutate(data = map(data, as.character)) %>% 
      unnest(data)
  })
  
  # observe({
  #   if(file.exists("file.csv")){
  #     write.table(AllInputs(), "file.csv", sep = ",", col.names = F, append = T, row.names = F)
  #   } else {
  #     write.csv(AllInputs(), "file.csv", sep = ",", row.names = F)
  #   }
  #   
  # })
  
  output$show_inputs <- renderTable({
    
    AllInputs()
  })
})
shinyApp(ui = ui, server = server)

expected output (Provided if the user changes a, e and b values)

key	 data.x  data.y
a	 a1           fd
e	 e1           fg
b	 b1           gh
c	 c1           c1
d	 d1          d1
f	 f1           f1
library(shiny)
library(tidyverse)

ui <- basicPage(
  
  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      textInput('c', 'Text A',"c1"),
      textInput('d', 'Text B',"d1"),
      textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1"),
      uiOutput('f1')
    ),
    column(
      width = 6,
      tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){
  
  
  AllInputs <- reactiveVal(NULL)
  
  newest <- reactive({
    x <- reactiveValuesToList(input)

   tibble(
      key = names(x),
      data = x
    ) %>% 
      unnest(data)

  })
  
  observeEvent(newest(),{
    if(isTruthy(AllInputs())){
    AllInputs(merge(AllInputs(),newest(),
              by="key"))} else {
                AllInputs(newest())
              }
  })

  output$show_inputs <- renderTable({
    
    AllInputs()
  })
})
shinyApp(ui = ui, server = server)

Thanks. It is working.
But if you change Text A(input$a) to "a2", f1 gets generated and this is not printing :slight_smile:

I got rid of it. Its not relevant to whay you asked for help with.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.