Last active
September 13, 2017 18:49
-
-
Save pedrokarneiro/7175dde0d267f8ba28685776d24add9c to your computer and use it in GitHub Desktop.
conditionalShowPDF - R and Shiny code to conditionally display a PDF file from a list of PDFs given in a selectInput.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################### | |
# Conditionaly displays a PDF file from a list of PDFs given | |
# in a selectInput. | |
# | |
# Author: Pedro Carneiro Junior | |
# This version: September-10-2017, Goiania-GO-Brazil | |
# | |
# * Will not work in browsers that don't support iframe; | |
# * PDF files pdf_file_1.pdf to pdf_file_n.pdf must be placed under the "www" subdirectory; | |
# * For better performance, use "Open in browser" button. | |
# | |
# * Ref. #1: https://gist.github.com/aagarw30/d5aa49864674aaf74951 | |
# * Ref. #2: https://gist.github.com/wch/9609200 | |
# | |
######################################################################### | |
library(shiny) | |
# Define UI ---- | |
ui <- fluidPage( | |
titlePanel("Show PDF file in app depending on chosen option from selectInput"), | |
sidebarLayout( | |
sidebarPanel( | |
selectInput(inputId = "choice_selected", | |
label = "Select the PDF of your choice", | |
choices = c( | |
"PDF 1" | |
, "PDF 2" | |
, "PDF 3" | |
, "PDF N" | |
), | |
selected = "PDF 1" | |
) | |
), | |
mainPanel( | |
tabsetPanel( | |
tabPanel("PDF", uiOutput("ui")), | |
tabPanel("More details", tags$p("Selected choice:"), verbatimTextOutput("choice_selected_text")) | |
) | |
) | |
) | |
) | |
# Define server logic ---- | |
server <- function(input, output) { | |
output$ui <- renderUI({ | |
if (is.null(input$choice_selected)) | |
return() | |
# Depending on input$choice_selected, we'll generate a different | |
# UI with the PDF and send it to the client. | |
switch(input$choice_selected, | |
"PDF 1" = tags$iframe(style="height:580px; width:100%; scrolling=yes", src="pdf_file_1.pdf") | |
, "PDF 2" = tags$iframe(style="height:580px; width:100%; scrolling=yes", src="pdf_file_2.pdf") | |
, "PDF 3" = tags$iframe(style="height:580px; width:100%; scrolling=yes", src="pdf_file_3.pdf") | |
, "PDF N" = tags$iframe(style="height:580px; width:100%; scrolling=yes", src="pdf_file_n.pdf") | |
) | |
}) | |
output$choice_selected_text <- renderText({ | |
input$choice_selected | |
}) | |
} | |
# Run the app ---- | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment