我正在尝试在我 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))))))
感谢任何帮助。谢谢。
最佳答案
使用renderUI
和htmlOutput
构建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/