1

I have used this answer to embed a pdf in my shiny app. When I first generate the pdf through shiny UI inputs and "submit" with an action button the pdf is generated from the UI inputs and displays in the Viewer fine.

However when I change the document through the UI and and submit again the viewer still displays the old report even though the file contents have changed.

How can I force it to display the new pdf?

This is reproducible example of my problem, works best when opened in browser.

ui.R

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      textInput("textin", label = "text input")
      , actionButton("makepdf", "Make pdf")
    ),

    mainPanel(
      htmlOutput("pdfviewer")
    )
  )
))

server.R

library(shiny)
shinyServer(function(input, output) {
observeEvent(input$makepdf,{
  title <- input$textin
  print(title)
  pdf("www/test.pdf")
  plot(mtcars$mpg,mtcars$disp, main = title)
  dev.off()
})


 observeEvent(input$makepdf,({
   output$pdfviewer <-renderText({
    return(paste('<iframe style="height:600px; width:100%" src="'," test.pdf", '"></iframe>', sep = ""))
    })
  })
)

})

Steps to reporoduce:

  1. Launch app in browser
  2. Type anything in the "text input" box and click "Make pdf"
  3. Type something different in the "text input" box and click "Make pdf"
  4. Observe the pdf viewer still shows output from step 2 even though the source file in your www folder will display the change made in step 3.
Community
  • 1
  • 1
DataJack
  • 341
  • 2
  • 13

1 Answers1

1

I do not know why this works instead of the script in my question but using renderUI in conjunction with tags$iframe as per this answer solves the issue. I welcome any explanations as to why one works and not the other.

Change server.ui to the following:

library(shiny)
shinyServer(function(input, output) {
  observeEvent(input$makepdf,{
    title <- input$textin
    print(title)
    pdf("www/test.pdf")
    plot(mtcars$mpg,mtcars$disp, main = title)
    dev.off()
  })

  output$pdfviewer<- renderUI({
       if((input$makepdf)<1) return()
       test <- " test.pdf"
       my_test <- tags$iframe(src=test, height=600, width = "100%")#, width=535
       print(my_test)
       return({my_test})
                             })

})
Community
  • 1
  • 1
DataJack
  • 341
  • 2
  • 13
  • After more experience I now understand why this is correct over my original example. For any one who wants to make shiny apps regularly I think it is essential to understand that actual concept of reactivity within shiny and it is a must to watch [these two talks](https://www.rstudio.com/resources/webinars/shiny-developer-conference/) from Joe Cheng. . – DataJack Mar 14 '17 at 10:43