Search code examples
shinyreactivertf

Reactive function to read rtf in rshiny


I am new to R and RShiny. Am building an application to compare two rtf files where a user can choose the two files dynamically. Is there a reactive function that I can use to read an rtf file before I pass it on to renderDiffr? Here is one version of the code I generated but the issue I am having is for these two rtf files I pick under folder1 and folder2 should be passed to renderDiffr below. I am sure there is a simple solution that I am yet to figure out. Would appreciate your help.

library(diffr)
library(shiny)

ui <- fluidPage(
  # Main title of the page
  titlePanel(HTML("<center>Comparison of two files</center>")),
  
  # Browse buttons to select files
  sidebarLayout(position="left",
                sidebarPanel(
                    #fileInput("selectfolder1","Select file from folder 1"),
                    #fileInput("selectfolder2","Select file from folder 2"),
                    # Submit button to perform the compare               
                    actionButton("goButton", "Compare", class = "btn-success")                   
                ),
  
  mainPanel(
    verbatimTextOutput("folder1"),
    verbatimTextOutput("folder2"),
    diffrOutput("value")
  )))


shinyServer(
  
  server <- function(input, output, session){
    
    re1<-reactive({
        file1<-file.choose()
    })
    
    output$folder1<-renderText({
        re1()
    })
    
    re2<-reactive({
        file2<-file.choose()
    })
    
    output$folder2<-renderText({
      re2()
    })    
    
    re3<-reactive({
      input$goButton
        x<-diffr(folder1,folder2, before="Folder 1",after="Folder 2")
    })
    
    output$value<-renderDiffr({
      re3()
    })  
}
)

shinyApp(ui, server)

Solution

  • According to this documentation you pass the result of diffr() to diffrOutput().

    What I changed:

    • When you use a reactive function make sure that you return the result at the end of it. I use return() for clarity (though it's not necessary).
    • In diffr(folder1,folder2, ...) I used diffr(re1(), re2(), ...) for obvious reasons.
    • I also skipped the re3 reactive In this sample and used diffr() directly in renderDiffr. But that is merely to reduce complexity.
    library(diffr)
    library(shiny)
    
    ui <- fluidPage(
      # Main title of the page
      titlePanel(HTML("<center>Comparison of two files</center>")),
      
      # Browse buttons to select files
      sidebarLayout(position="left",
                    sidebarPanel(
                      #fileInput("selectfolder1","Select file from folder 1"),
                      #fileInput("selectfolder2","Select file from folder 2"),
                      # Submit button to perform the compare               
                      actionButton("goButton", "Compare", class = "btn-success")                   
                    ),
                    
                    mainPanel(
                      verbatimTextOutput("folder1"),
                      verbatimTextOutput("folder2"),
                      diffrOutput("FileDiff", width="600px", height="auto")
                    )))
    
    
    shinyServer(
      
      server <- function(input, output, session){
        
        re1<-reactive({
          file1<-file.choose()
          file1
        })
        
        output$folder1<-renderText({
          re1()
        })
        
        re2<-reactive({
          file2<-file.choose()
          file2
        })
        
        output$folder2<-renderText({
          re2()
        })    
    
        # re3<-reactive({
        #   input$goButton
        #   x<-diffr(folder1,folder2, before="Folder 1",after="Folder 2")
        #   return(x)
        # })
        
        output$FileDiff <- renderDiffr({
          input$goButton
          diffr(re1(), re2(), before="Folder 1", after="Folder 2")
        })  
      }
    )
    
    shinyApp(ui, server)