Advertisement
Guest User

stack_05302022

a guest
Jun 1st, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. TITLE: Shiny: Compare two dfs, highlight differences, edit + download new df
  2.  
  3. My goal is to build a Shiny app where I can upload two dataframes and the app will:
  4.  
  5. * Automatically highlight discrepancies between the two dataframes
  6. * Allow direct editing to the dataframe
  7. * Allow me to download the newly-edited dataframe
  8.  
  9.  
  10. 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!
  11.  
  12. ## uses gt table, not editable
  13.  
  14. ```
  15. library(shiny)
  16. library(gt)
  17. library(rhandsontable)
  18. library(tidyverse)
  19. library(shinythemes)
  20. library(rsconnect)
  21. library(DT)
  22.  
  23. dat1 <- data.frame(
  24. emp_id = c(1:5),
  25. emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  26. salary = c(623.3,515.2,611.0,735.0,844.25))
  27.  
  28. dat2 <- data.frame(
  29. emp_id = c(1:5),
  30. emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  31. salary = c(623.3,515.2,611.0,729.0,843.25))
  32.  
  33. ui <- navbarPage("This is my Shiny app.",
  34. theme = shinytheme("flatly"),
  35. tabPanel("Upload",
  36. titlePanel("Upload your datafiles"),
  37. sidebarLayout(
  38. sidebarPanel(
  39.  
  40. ## File 1
  41. fileInput('file1', 'Data Entry #1',
  42. accept=c('text/csv',
  43. 'text/comma-separated-values,text/plain',
  44. '.csv')),
  45. tags$hr(),
  46.  
  47. ## File 2
  48. fileInput('file2', 'Data Entry #2',
  49. accept=c('text/csv',
  50. 'text/comma-separated-values,text/plain',
  51. '.csv')),
  52. tags$hr(),
  53.  
  54. ),
  55.  
  56. mainPanel(gt_output("contents"))
  57. )
  58. ),
  59. )
  60.  
  61. server <- function(input, output, session) {
  62.  
  63. df1 <- reactive({ dat1
  64. #inFile <- input$file1
  65. #if (is.null(input$file1))
  66. # return(NULL)
  67. #read.csv(inFile$datapath)
  68. })
  69.  
  70. df2 <- reactive({ dat2
  71. #inFile <- input$file2
  72. #if (is.null(input$file2))
  73. # return(NULL)
  74. #read.csv(inFile$datapath)
  75. })
  76.  
  77. compared <- reactive({
  78.  
  79. req(df1())
  80. req(df2())
  81.  
  82. tbl_diffs <- which(df1() != df2(), arr.ind = TRUE)
  83. tbl_compare <- df2() %>%
  84. gt()
  85. for (i in seq_len(nrow(tbl_diffs))) {
  86. tbl_compare <- tbl_compare %>%
  87. tab_style(
  88. style = list(
  89. cell_fill(color = "#FFFF00")
  90. ),
  91. locations = cells_body(
  92. columns = tbl_diffs[[i, "col"]],
  93. rows = tbl_diffs[[i, "row"]]
  94. )
  95. )
  96. }
  97. tbl_compare
  98. })
  99.  
  100. output$contents <- render_gt(
  101. compared()
  102. )
  103. }
  104.  
  105. shinyApp(ui = ui, server = server)
  106. ```
  107.  
  108. ## failed attempt at dt
  109.  
  110. ```
  111. library(shiny)
  112. library(gt)
  113. library(rhandsontable)
  114. library(tidyverse)
  115. library(shinythemes)
  116. library(rsconnect)
  117. library(DT)
  118.  
  119. dat1 <- data.frame(
  120. emp_id = c(1:5),
  121. emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  122. salary = c(623.3,515.2,611.0,735.0,844.25))
  123.  
  124. dat2 <- data.frame(
  125. emp_id = c(1:5),
  126. emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  127. salary = c(623.3,515.2,611.0,729.0,843.25))
  128.  
  129. ui <- navbarPage("This is my Shiny app.",
  130. theme = shinytheme("flatly"),
  131. tabPanel("Upload",
  132. titlePanel("Upload your datafiles"),
  133. sidebarLayout(
  134. sidebarPanel(
  135.  
  136. ## File 1
  137. fileInput('file1', 'Data Entry #1',
  138. accept=c('text/csv',
  139. 'text/comma-separated-values,text/plain',
  140. '.csv')),
  141. tags$hr(),
  142.  
  143. ## File 2
  144. fileInput('file2', 'Data Entry #2',
  145. accept=c('text/csv',
  146. 'text/comma-separated-values,text/plain',
  147. '.csv')),
  148. tags$hr(),
  149.  
  150. ),
  151.  
  152. mainPanel(dataTableOutput("contents"))
  153. )
  154. ),
  155. )
  156.  
  157. server <- function(input, output, session) {
  158.  
  159. df1 <- reactive({ dat1
  160. #inFile <- input$file1
  161. #if (is.null(input$file1))
  162. # return(NULL)
  163. #read.csv(inFile$datapath)
  164. })
  165.  
  166. df2 <- reactive({ dat2
  167. #inFile <- input$file2
  168. #if (is.null(input$file2))
  169. # return(NULL)
  170. #read.csv(inFile$datapath)
  171. })
  172.  
  173. compared <- reactive({
  174.  
  175. req(df1())
  176. req(df2())
  177.  
  178. tbl_diffs <- which(df1() != df2(), arr.ind = TRUE) %>% as.data.frame()
  179. tbl_compare <- df2() %>% as.data.frame()
  180.  
  181. tbl_gt_B <- df2() %>% as.data.frame()
  182.  
  183. for (i in seq_len(nrow(tbl_diffs))) {
  184. tbl_gt_B <-
  185. formatStyle(tbl_gt_B,
  186. color = "yellow",
  187. columns = tbl_diffs[[i, "col"]],
  188. rows = tbl_diffs[[i, "row"]]) }
  189. tbl_compare
  190. })
  191.  
  192. output$contents <- renderDataTable(
  193. compared()
  194. )
  195. }
  196.  
  197. shinyApp(ui = ui, server = server)
  198. ```
  199.  
  200.  
  201. [1]: http://stla.github.io/stlapblog/posts/shiny_editTable.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement