r - 根据输入 Shiny 的 iframe

标签 r iframe shiny

我正在尝试在我 Shiny 的应用程序中构建一个 iframe。困难在于它的 react 性,它应该根据用户输入加载不同的图像。

我尝试构建一个普通的 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")
)

但我在 react 性方面遇到了麻烦。我认为我只需将正确的链接粘贴到 tags$iframe 中,但不幸的是这不起作用。这是我的文件:

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

感谢任何帮助。谢谢。

最佳答案

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

关于r - 根据输入 Shiny 的 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25849968/

相关文章:

css - 通过 shiny 添加/删除 css 类到 div

r - 来自 rhandsontable 在 Shiny 问题中的输入

r - 在 qqplot 和 Shiny 中命名图例

从 base reshape vs 从具有缺失值的 reshape2 reshape

r - R中输出数据帧的函数

css - Redactor 图片上传设置内联样式

javascript - 使用 jQuery 从 YouTube 视频 iFrame 链接中提取 ID

iframe - 如何在 Apex 中自动调整 iframe 的大小?

r - 文档引用类及其方法与 roxygen2

r - 如何为 R 包创建自定义启动消息?