基于闪亮的selectizeInput渲染多个iframe

时间:2018-02-16 02:53:36

标签: r iframe shiny

猜测这对经验丰富的r闪亮用户来说非常基础,但希望它可以帮助其他新学员。

如何在下面的脚本中显示多个iframe? (即如果用户同时选择a和b,则iframe在页面上以堆叠顺序显示)。

谢谢,

    library(shiny)

ui <- fluidPage(

  # Application title
  titlePanel("FRED Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(

      selectizeInput("checkGroup2", label = h3("Leading Indicators to compare"), 
                     choices = c("a","b"),multiple=TRUE, options = list(placeholder = 'Select graph(s)'))      
    ),

    # Show a plot of the iframe
    mainPanel(

      htmlOutput("frame")

       # fluidRow(
       #   column(3, htmlOutput("frame"))
       # ),
       # fluidRow(
       #   column(3, htmlOutput("frame"))
       # )    
    )
  )
)

server <- function(input, output) {       
  output$frame <- renderUI({
    if (input$checkGroup2 %in% "b"){

    iframeb <- tags$iframe(src="//fred.stlouisfed.org/graph/graph-landing.php?g=irzW&width=670&height=475" , height=475, width=670)
    iframeb
    }        
    })

output$framea <- renderUI({

    if (input$checkGroup2 %in% "a"){
      iframea <- tags$iframe(src="//fred.stlouisfed.org/graph/graph-landing.php?g=igJe&width=670&height=475", height=475, width=670)
      iframea
    }
      })      
}

# Run the application 
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您可以使用conditionalPanels,下面的工作示例。您可能想要考虑checkBoxGroupInput是否更适合您的应用程序中的这种选择,而不是selectizeInput,如果您不熟悉那些。

希望这有帮助!

enter image description here

library(shiny)

ui <- fluidPage(

  # Application title
  titlePanel("FRED Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(

      selectizeInput("checkGroup2", label = h3("Leading Indicators to compare"), 
                     choices = c("a","b"),multiple=TRUE, options = list(placeholder = 'Select graph(s)'))

    ),

    # Show a plot of the iframe
    mainPanel(
      conditionalPanel('input.checkGroup2 != null && input.checkGroup2.indexOf("a")!=-1',
                       tags$iframe(src="//fred.stlouisfed.org/graph/graph-landing.php?g=igJe&width=670&height=475", height=475, width=670)
      ),
      conditionalPanel('input.checkGroup2 != null && input.checkGroup2.indexOf("b")!=-1',
                       tags$iframe(src="//fred.stlouisfed.org/graph/graph-landing.php?g=irzW&width=670&height=475" , height=475, width=670)
      )

    )
  )
)


server <- function(input, output) {

}

# Run the application 
shinyApp(ui = ui, server = server)