1

Below is the code I have for my shiny app. The ultimate goal is for the user to upload 2 csv files that have the same column titles and the output should be the rows that are different between the 2 csv files. The app seems to upload the csv files OK, but there is no table shown in the shiny app. Additionally, I hope to export the full output table to a csv eventually, but I have yet to include that in my code.

library(shiny)
library(dplyr)
library(gridExtra)
library(grid)
library(stringr)
ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose First Data File",
                    multiple = FALSE, accept = c("text/csv", "text/comma-separated-values, text/plain",
                                             ".csv")),
      fileInput("file2", "Choose Second Data File",
                multiple = FALSE, accept = c("text/csv", "text/comma-separated-values, text/plain",
                                             ".csv")),
      tags$hr(),
      checkboxInput("header", "Header", TRUE),
      radioButtons("sep", "Separator", choices = c(Comma = ",",
                                                   Semicolon = ";",
                                                   Tab = "\t"),
                   selected = ","),
      radioButtons("quote", "Quote", choices = c(None = "",
                                                 "Double Quote" = '"',
                                                 "Single Quote" = "'"),
                   selected = '"'),
      tags$hr(),
      radioButtons("disp", "Display", choices = c(Head = "head", All = "all"), selected = "head")
    ),
    mainPanel(
      tableOutput("contents", "export")
    )
  )
)
options(shiny.maxRequestSize = 30*1024^2)
server <- function(input, output) {
  data <- reactive({
    df <- read.csv(input$file1$datapath,
               header = input$header
               quote = input$quote)
    df2 <- read.csv(input$file2$datapath,
                header = input$header
                quote = input$quote)
    df$COLUMNA <- str_replace_all(df$COLUMNA, "[[:punct:]]", "")
    df2$COLUMNA <- str_replace_all(df2$COLUMNA, "[[:punct:]]", "")
    diff_df <- anti_join(df, df2, by = c("COLUMNA", "COLUMNB", "COLUMNC"))
    return(diff_df)
  }) 
}
output$contents <- renderTable({
  req(input$file1, input$file2)
  if(input$disp == "head") {
    head(data()$diff_df)
  }
  else {data()$diff_df}
})

shinyApp(ui, server)
2
  • Could you add 2 example .csv files in the proper format so we can try to reproduce your problem? Commented Sep 14, 2018 at 15:43
  • Some random thoughts: 1) add req(input$file1, input$file2) to data<-reactive({, since that can't run without both. 2) Load each file in a separate reactive, then merge them in a 3rd reactive. 3) rename data since it clashes with the built in data() function (unlikely to be the problem, but it's good practice) Commented Sep 14, 2018 at 15:54

1 Answer 1

1

Have you tried to move the following code into your server function?

output$contents <- renderTable({
  req(input$file1, input$file2)
  if(input$disp == "head") {
    head(data()$diff_df)
  }
  else {data()$diff_df}
})

This renderTable should work in server function, but it seems you put it out of server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.