Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE: Shiny: Compare two dfs, highlight differences, edit + download new df
- My goal is to build a Shiny app where I can upload two dataframes and the app will:
- * Automatically highlight discrepancies between the two dataframes
- * Allow direct editing to the dataframe
- * Allow me to download the newly-edited dataframe
- I have created a working reprex using gt, but so far I am unable to add editing/downloading capability, as I couldn't translate gt's highlighting into something compatible with DT or rhandsomtable. Not included here, but I also was able to replicate [this tutorial][1] on rhandsomtable to allow upload of two dataframes, but I couldn't add the feature to highlight discrepant cells. I would appreciate any recommendations or directions on packages, thank you!
- ## uses gt table, not editable
- ```
- library(shiny)
- library(gt)
- library(rhandsontable)
- library(tidyverse)
- library(shinythemes)
- library(rsconnect)
- library(DT)
- dat1 <- data.frame(
- emp_id = c(1:5),
- emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
- salary = c(623.3,515.2,611.0,735.0,844.25))
- dat2 <- data.frame(
- emp_id = c(1:5),
- emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
- salary = c(623.3,515.2,611.0,729.0,843.25))
- ui <- navbarPage("This is my Shiny app.",
- theme = shinytheme("flatly"),
- tabPanel("Upload",
- titlePanel("Upload your datafiles"),
- sidebarLayout(
- sidebarPanel(
- ## File 1
- fileInput('file1', 'Data Entry #1',
- accept=c('text/csv',
- 'text/comma-separated-values,text/plain',
- '.csv')),
- tags$hr(),
- ## File 2
- fileInput('file2', 'Data Entry #2',
- accept=c('text/csv',
- 'text/comma-separated-values,text/plain',
- '.csv')),
- tags$hr(),
- ),
- mainPanel(gt_output("contents"))
- )
- ),
- )
- server <- function(input, output, session) {
- df1 <- reactive({ dat1
- #inFile <- input$file1
- #if (is.null(input$file1))
- # return(NULL)
- #read.csv(inFile$datapath)
- })
- df2 <- reactive({ dat2
- #inFile <- input$file2
- #if (is.null(input$file2))
- # return(NULL)
- #read.csv(inFile$datapath)
- })
- compared <- reactive({
- req(df1())
- req(df2())
- tbl_diffs <- which(df1() != df2(), arr.ind = TRUE)
- tbl_compare <- df2() %>%
- gt()
- for (i in seq_len(nrow(tbl_diffs))) {
- tbl_compare <- tbl_compare %>%
- tab_style(
- style = list(
- cell_fill(color = "#FFFF00")
- ),
- locations = cells_body(
- columns = tbl_diffs[[i, "col"]],
- rows = tbl_diffs[[i, "row"]]
- )
- )
- }
- tbl_compare
- })
- output$contents <- render_gt(
- compared()
- )
- }
- shinyApp(ui = ui, server = server)
- ```
- ## failed attempt at dt
- ```
- library(shiny)
- library(gt)
- library(rhandsontable)
- library(tidyverse)
- library(shinythemes)
- library(rsconnect)
- library(DT)
- dat1 <- data.frame(
- emp_id = c(1:5),
- emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
- salary = c(623.3,515.2,611.0,735.0,844.25))
- dat2 <- data.frame(
- emp_id = c(1:5),
- emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
- salary = c(623.3,515.2,611.0,729.0,843.25))
- ui <- navbarPage("This is my Shiny app.",
- theme = shinytheme("flatly"),
- tabPanel("Upload",
- titlePanel("Upload your datafiles"),
- sidebarLayout(
- sidebarPanel(
- ## File 1
- fileInput('file1', 'Data Entry #1',
- accept=c('text/csv',
- 'text/comma-separated-values,text/plain',
- '.csv')),
- tags$hr(),
- ## File 2
- fileInput('file2', 'Data Entry #2',
- accept=c('text/csv',
- 'text/comma-separated-values,text/plain',
- '.csv')),
- tags$hr(),
- ),
- mainPanel(dataTableOutput("contents"))
- )
- ),
- )
- server <- function(input, output, session) {
- df1 <- reactive({ dat1
- #inFile <- input$file1
- #if (is.null(input$file1))
- # return(NULL)
- #read.csv(inFile$datapath)
- })
- df2 <- reactive({ dat2
- #inFile <- input$file2
- #if (is.null(input$file2))
- # return(NULL)
- #read.csv(inFile$datapath)
- })
- compared <- reactive({
- req(df1())
- req(df2())
- tbl_diffs <- which(df1() != df2(), arr.ind = TRUE) %>% as.data.frame()
- tbl_compare <- df2() %>% as.data.frame()
- tbl_gt_B <- df2() %>% as.data.frame()
- for (i in seq_len(nrow(tbl_diffs))) {
- tbl_gt_B <-
- formatStyle(tbl_gt_B,
- color = "yellow",
- columns = tbl_diffs[[i, "col"]],
- rows = tbl_diffs[[i, "row"]]) }
- tbl_compare
- })
- output$contents <- renderDataTable(
- compared()
- )
- }
- shinyApp(ui = ui, server = server)
- ```
- [1]: http://stla.github.io/stlapblog/posts/shiny_editTable.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement