Chumbs
Chumbs

Reputation: 188

Merge the uploaded csv with the current data frame in r shiny

The example that I'm working with is the iris data. If the current data contains iris[1:15,], how can I upload a .csv file with more iris data and click a button to combine the uploaded data with the current data and save everything in one dataframe?

Here is what I have so far based on what I've read. I was able to create the fileInput and action button but I think my issue is with the reactive button. I'm not sure how to use it properly to achieve what I need.

library(shiny)
library(DT)

data1<-data.frame(iris[1:15,])

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      numericInput('num','Number of rows',value=10,min=0),

    actionButton("update", "Combine Data")),


    mainPanel(
      tableOutput("table")
    )
  )
)

server <- function(input, output) {
   output$table <- renderTable({
     head(data1,n=input$num)
   })


x<-reactive({
  req(input$file1)
  df_uploaded <- read.csv(input$file1$datapath,
                 header = input$header,
                 sep = input$sep,
                 quote = input$quote,
                 stringsAsFactors = FALSE)
  data2<-data.frame(df_uploaded)
  return(data2)
})

merged_data<-eventReactive(input$update,{
  datam<-rbind.data.frame(data1,x())
  return(datam)
})

# output$table <- renderTable({
#              head(merged_data(),n=input$num)})
}  


shinyApp(ui, server)

Thanks!

Upvotes: 1

Views: 584

Answers (1)

A. Suliman
A. Suliman

Reputation: 13135

The main issue is that read.csv receiving invalid argument i.e. NULL for header, sep, quote as you don't have input$header, input$sep, input$quote in UI.

library(shiny)
library(DT)

data1<-data.frame(iris[1:15,])

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      numericInput('num','Number of rows',value=10,min=0),

      actionButton("update", "Combine Data")),


    mainPanel(
      tableOutput("table")
    )
  )
)

server <- function(input, output) {
  # output$table <- renderTable({
  #   head(data1,n=input$num)
  # })

  x<-reactive({
    req(input$file1)
    df_uploaded <- read.csv(input$file1$datapath,
                            #you don't have these variables in the UI, so they will raise an error
                            #header = input$header,
                            #sep = input$sep,
                            #quote = input$quote,
                            stringsAsFactors = FALSE)
    #No need data2 and return(data2) as read.csv returns data.frame by default
    #data2<-data.frame(df_uploaded)
    #return(data2)
  })

  merged_data<-eventReactive(input$update,{
    datam<-rbind.data.frame(data1, x())
    return(datam)
  })

  output$table <- renderTable({
               head(merged_data(), n=input$num)})
}  


shinyApp(ui, server)

Upvotes: 1

Related Questions