Shiny iframe depending on input

I am trying to create an iframe in my shiny application. The difficulty is its reactivity, it has to load another image depending on user input.

I tried to create a normal iframe that works great:

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 reactivity issues. I thought I just need to paste the correct link in tags$iframe

, but unfortunately this 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. Thank.

+3


source to share


1 answer


Use renderUI

and htmlOutput

for building iframe and renderText

there were numerous bugs as well. Below is a simplified version of the code (only your first link gives an image):

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))

      

+4


source







All Articles