如何在Shiny中使用自己的一组小部件创建多个页面

时间:2015-10-13 16:34:25

标签: r shiny

我希望能够创建多个页面,每个页面都有一组小部件下拉,单选按钮和绘制地图的空间。 Shiny教程展示了如何创建多个页面

shinyUI(navbarPage("My Application",
  tabPanel("Component 1"),
  tabPanel("Component 2"),
  tabPanel("Component 3")
))

如何在每个页面中添加小部件,例如如何将以下内容添加到组件1?

  sidebarLayout(
    sidebarPanel(
        selectizeInput(
            'id', label = "Year", choices =   NULL,multiple=FALSE,selected="X2015",
            options = list(create = TRUE,placeholder = 'Choose the year')

        ),
        # Make a list of checkboxes
        radioButtons("radio", label = h3("Radio buttons"),
                     choices = list("Choice 1" = 1,
                                   "Choice 2" = 2)

     mainPanel(
        plotOutput("distPlot")
    )

2 个答案:

答案 0 :(得分:5)

您应该阅读tabPanel的参考页:http://shiny.rstudio.com/reference/shiny/latest/tabPanel.html

shinyUI(
    navbarPage("My Application",
             tabPanel(
               "Component 1",
               sidebarLayout(
                 sidebarPanel(
                   selectizeInput(
                     'id', label="Year", choices=NULL, multiple=F, selected="X2015",
                     options = list(create = TRUE,placeholder = 'Choose the year')
                   ),
                   # Make a list of checkboxes
                   radioButtons("radio", label = h3("Radio buttons"),
                                choices = list("Choice 1" = 1, "Choice 2" = 2)
                   )
                 ),
                 mainPanel( plotOutput("distPlot") )
               )
             ),
             tabPanel("Component 2"),
             tabPanel("Component 3")
  )
)

答案 1 :(得分:1)

您可以执行tabPanel("Component 1", ...),用所有sidebarPanel代码替换点。或者,在服务器端使用renderUI

library(shiny)

shinyApp(
    shinyUI(
        navbarPage("My Application",
                   tabPanel("Component 1", uiOutput('page1')),
                   tabPanel("Component 2"),
                   tabPanel("Component 3")
                   )
    ),
    shinyServer(function(input, output, session) {
        output$page1 <- renderUI({
            sidebarLayout(
                sidebarPanel(
                    selectizeInput(
                        'id', label = "Year", choices =   NULL,multiple=FALSE,selected="X2015",
                        options = list(create = TRUE,placeholder = 'Choose the year')
                    ),
                    ## Make a list of checkboxes
                    radioButtons("radio", label = h3("Radio buttons"),
                                 choices = list("Choice 1" = 1, "Choice 2" = 2))
                ),
                mainPanel(
                    plotOutput('distPlot')
                )
            )
        })

        output$distPlot <- renderPlot({ plot(1) })
    })
)