Thomas
Thomas

Reputation: 1444

Shiny iframe depending on input

I am trying to build an iframe into my shiny app. The difficulty is with its reactivity, it should load a different image depending on user input.

I tried to build a normal iframe, which works fine:

ui.R

column(3,
   tags$iframe(src="http://www.parlament.ch/SiteCollectionImages/profil/225x225/3014.jpg", height=242, width=242, frameborder=0, seamless="seamless")
)

But I'm having trouble with the reactivity. I thought that I just have to paste the correct link into tags$iframe but unfortunately that doesn't work. Here are my files:

server.R

 library(shiny)
 shinyServer(function(input, output) {

 output$naame <- renderUI({
   members <- data.frame(name=c("Name 1", "Name 2", "Name 3"), nr=c(3014, 2565, 2670))
   selectInput("Member", 
            label=h5("Choose a person"),
            choices=members[,2],
            selected=NULL)
   })


 loadpic <- reactive({ 
    if (!is.null(input$Member)){
     #input$Member can be any number, I use 3014 as an example. bildurl is then
     #http://www.parlament.ch/SiteCollectionImages/profil/225x225/3014.jpg
     zahhl <- paste0(members[which(members==input$Member),2])
     bildurl <- paste0("http://www.parlament.ch/SiteCollectionImages/profil/225x225/", sep="", zahhl, sep="", ".jpg")

  return(bildurl)}

})

  output$imagehead <- renderText({
    loadpic()
})

ui.R

shinyUI(fluidPage(titlePanel("Choose your preferred name"), 
              sidebarLayout(
                sidebarPanel(
     fluidRow(#Chooses Name
                    column(6,
                           uiOutput("naame")
                           )),
     mainPanel(fluidRow(column(3,tags$iframe(src=htmlOutput("imagehead"), height=242, width=242))))))

Any help is appreciated. Thanks.

Upvotes: 3

Views: 2931

Answers (1)

jdharrison
jdharrison

Reputation: 30425

Use renderUI and htmlOutput to construct the iframe rather the renderText there was also numerous errors. A simplified version of the code follows (only your first link gives a picture):

ui.R

shinyUI(fluidPage(titlePanel("Choose your preferred name"), 
                  sidebarLayout(
                    sidebarPanel(
                      fluidRow(
                        column(6,
                               selectInput("Member", label=h5("Choose a person"),
                                           choices=members[,2])
                        ))),
                    mainPanel(fluidRow(
                      column(3, htmlOutput("imagehead"))
                    )
                    )
                  )
)
)

server.R

library(shiny)
shinyServer(function(input, output) {
  loadpic <- reactive({ 
    validate(
      need(input$Member, "Member input is null!!")
    )
    zahhl <- members[which(members$nr==input$Member),2]
    paste0("http://www.parlament.ch/SiteCollectionImages/profil/225x225/", zahhl, ".jpg")
  })
  output$imagehead <- renderUI({
    tags$iframe(src=loadpic(), height=242, width=242)
  })
}
)

global.R

members <- data.frame(name=c("Name 1", "Name 2", "Name 3"), nr=c(3014, 2000, 4358))

Upvotes: 4

Related Questions