【问题标题】:Shiny iframe depending on input闪亮的 iframe 取决于输入
【发布时间】:2014-09-15 14:05:50
【问题描述】:

我正在尝试将 iframe 构建到我闪亮的应用程序中。困难在于它的反应性,它应该根据用户输入加载不同的图像。

我尝试构建一个正常的 iframe,效果很好:

ui.R

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

但我的反应性有问题。我以为我只需将正确的链接粘贴到tags$iframe 中,但不幸的是这不起作用。这是我的文件:

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

感谢任何帮助。谢谢。

【问题讨论】:

  • 请提供一个可重现的最小示例来说明您遇到的问题。
  • @jdharrison:我更新了代码。我希望它是可重现的......

标签: r iframe shiny


【解决方案1】:

使用renderUIhtmlOutput 构造iframe 而不是renderText 也有很多错误。代码的简化版本如下(只有您的第一个链接给出了图片):

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

服务器.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)
  })
}
)

全球.R

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

【讨论】:

  • 谢谢。我也试过了,我得到的只是应用程序中的 url,尽管它没有加载图片。我怀疑它与src=function 有关,这不起作用?
  • 是的,除了第一个之外,您的网址无效。所以parlament.ch/SiteCollectionImages/profil/225x225/3014.jpg 有效,但parlament.ch/SiteCollectionImages/profil/225x225/2000.jpgparlament.ch/SiteCollectionImages/profil/225x225/4358.jpg 不是有效的网址。代码完全正确。您还需要在浏览器中查看它。例如,它不会在 rstudio 查看器中正确显示。
  • 如果zahhl 是 3014 它也不起作用,它只显示 url。它以某种方式不加载图片。此外,我还查找了其他一些特别有效的数字,并用它们更新了我的问题。
  • 我既感动又困惑。你的例子也适用于我。为什么我的不能为我工作,它仍然只显示网址而不是加载图片......我会回复你的。提前致谢。
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 2017-05-06
  • 2017-08-17
  • 2014-01-08
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多