动态渲染UI输入和输出R闪亮

时间:2016-10-31 11:38:04

标签: shiny

EDITE: 我添加了一个与我的问题相同的示例。我想将checkboxGroupInput更新为dataset更改。并且还希望在checkboxGroupInput选项中使用选中列来呈现表格。

服务器

shinyServer( function(input, output, session) {

    dataset_list <- list( "rock" = rock,    
                          "pressure" = pressure,
                          "cars" = cars
                         )

    observeEvent( input$n_select_input, {

         selected_dataset <- reactive({ 
                                 selected_list <- list()
                                 for( i in 1:input$n_select_input ){
                                       selected_list[[i]] <- dataset_list[[i]]
                                  }
                                 names(selected_list) <- names( dataset_list )[1:input$n_select_input]
                                 selected_list
                              })


        colname_indata_list <- reactive({

                                 colname.indata.list <- list()
                                 for( set in names( selected_dataset() ) ){
                                     colname.indata.list[[set]] <- colnames( selected_dataset()[[set]] )
                                     }
                                 colname.indata.list
                                 })


        choice_cand <- reactive({ names(selected_dataset()) })


    updateSelectInput( session,
                       "dataset",
                       choices = as.character( choice_cand() )
                      )

    choices_cand <- reactive({ colname_indata_list()[[input$dataset]] })

    updateCheckboxGroupInput( session, 
                              "column",
                              choices = as.character( choices_cand() ) 
                             )

  })


# observeEvent( input$dataset, {
#   
#   choices_cand <- reactive({ colname_indata_list()[[input$dataset]] })
#   updateCheckboxGroupInput( session, "column",
#                             choices = as.character( choices_cand() ) )
#   
# })

datasetInput <- reactive({
  switch(input$dataset,
         "rock" = rock,
         "pressure" = pressure,
         "cars" = cars)
  })

output$table <- renderTable({
  datasetInput()
  })


} )

我在##中尝试了上面的代码,它没有用。

UI

shinyUI( 
  fluidPage( titlePanel('Downloading Data'),
             sidebarLayout(
               sidebarPanel( numericInput( "n_select_input", "n select inpur", 1,
                                           min=1, max=3 ),
                              selectInput("dataset", "Choose a dataset:", 
                                         choices = "" ),
                             checkboxGroupInput( "column", "select column",
                                                 choices = "")
                             ),
               mainPanel(
                 tableOutput('table')
                 )
               )
             )
  )

FIRST WRITE:

我简化了下面的代码。我想使用更新的选择输入作为updatedCheckboxGroupInput选项的输出。但更新的选择输入input$select_group为空。我尝试了不同的解决方案..但无法解决它。 如果您只是立即运行此代码,它将无效..

fund_group <- reactive({ # this is the list of fund group including fund name
                         # for example,
                         "domestic" = c("a","b", "c"),
                         "global" = c( "aa", "bb", "cc")
                         # list name and fund name in the list are changable
                       })

我想将selectInput个选项更新为names(fund_group)更改。 所以我使用下面的代码,

observe({
         group_name <- reactive({ names(fund_group()) })
         updateSelectInput( session,
                            "select_group",
                            choices = group_name() )

         fund_list <- reactive({ fund_group()[[input$select_group]] })
         updateCheckboxGroupInput( session,
                                   "fund_in_group",
                                   choices = fund_list(),
                                   selected = fund_list() )
        })

对于用户界面,

narvarPage( "narvarTitle",
tabPanel( "tab panel",

fluidRow( column( 3, wellPanel( textOutput( "fixed_anal_date" ),
          br(),
          br(),
          selectInput( "select_group",
                       label = "Select group",
                       choices = "" ),
          br(),
          checkboxGroupInput( "fund_in_group",
                              label = "Select funds :",
                              choices = "" ),
          br()
  ) )
) )

感谢您阅读凌乱的代码......

1 个答案:

答案 0 :(得分:0)

我已经编辑了它现在正在运行的代码。你应该将两个输入放在两个不同的观察中。

这是服务器。 R:

-d

这是ui.R:

-m

<强> [编辑]:

根据您的最新更新,我修改了服务器代码,以便server <- function(input, output, session){ fund_group <- reactive({ # this is the list of fund group including fund name # for example, list("domestic" = c("a","b", "c"), "global" = c( "aa", "bb", "cc")) # list name and fund name in the list are changable }) observe({ group_name <- reactive({ names(fund_group()) }) updateSelectInput( session, "select_group", choices = group_name() ) }) observe({ fund_list <- reactive({ fund_group()[[input$select_group]] }) updateCheckboxGroupInput( session, "fund_in_group", choices = fund_list(), selected = fund_list()) }) } 更新为ui <- navbarPage( "narvarTitle", tabPanel( "tab panel", fluidRow( column( 3, wellPanel( textOutput( "fixed_anual_date" ), br(), br(), selectInput( "select_group", label = "Select group", choices = "" ), br(), checkboxGroupInput( "fund_in_group", label = "Select funds :", choices = "" ), br() ) ) ) ) ) 更改,并且还会为已检查的列呈现表。

checkboxGrouptInput

希望这有帮助!