RStudio Shiny动态选择

时间:2014-11-18 15:53:09

标签: r shiny rstudio

在我的RStudio Shiny中,我在我的selectInput中找到了server.R,在ui.R上我获得了tags语句来更改选择框的宽度和高度。

它在页面加载时有效,但当我到达单一类型时它恢复到默认大小。任何想法如何解决?

关于ui.R

# [...]
  ,div(class="span6"
   ,radioButtons("viz_multiple", "Select Type:",
          c("Select From List (can use Up/Down + Enter)" = "multiple",
            "Search One (Delete then type keyword)"  = "single")
    )
   )
  )
  ,div(class='row-fluid'
  ,div(class='span12', uiOutput("image_list"))
  ,tags$head(tags$style(type="text/css", "select#iimage_list             { width: 1000px; height: 40px; }"))
  )
 # [...]

在server.R

# [...]

output$image_list <- renderUI({
  imagelist = image_ls()
  iimage_list <- as.vector(sort(unique(as.character(imagelist)),decreasing=TRUE))
  length_list = length(iimage_list)
  selectInput("iimage_list",paste0("samples (",length_list,")"),choices=iimage_list, selectize = input$viz_multiple == 'single')
})
# [...]

当用户从tags切换到multiple时,有关如何应用single命令的任何想法吗?

1 个答案:

答案 0 :(得分:2)

您还需要动态添加css。要定位选择性输入,您需要定位select#dataset + .selectize-control,而不是select#dataset

库(有光泽)

runApp(list(
  ui = bootstrapPage(
    radioButtons("viz_multiple", "Select Type:",
                 c("Select From List (can use Up/Down + Enter)" = "multiple",
                   "Search One (Delete then type keyword)"  = "single")
    )
    , uiOutput("myUI")
  ),
  server = function(input, output){
    output$myUI <- renderUI({
      myCSS <-if(input$viz_multiple == 'single'){
        tags$style(type="text/css", "select#dataset + .selectize-control{ width: 1000px; height: 40px; }")
      }else{
        tags$style(type="text/css", "select#dataset { width: 1000px; height: 40px; }")
      }
      tagList(
        selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris'), selectize = (input$viz_multiple == 'single'))
        , myCSS
      )
    })
  }
))

或有两个单独的CSS条目,一个用于select,另一个用于selectize

library(shiny)
runApp(list(
  ui = bootstrapPage(
    radioButtons("viz_multiple", "Select Type:",
                 c("Select From List (can use Up/Down + Enter)" = "multiple",
                   "Search One (Delete then type keyword)"  = "single")
    )
    , uiOutput("myUI")
    , tags$style(type="text/css", "select#dataset + .selectize-control{ width: 1000px; height: 40px; }")
    , tags$style(type="text/css", "select#dataset { width: 1000px; height: 40px; }")
  ),
  server = function(input, output){
    output$myUI <- renderUI({
      selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris'), selectize = (input$viz_multiple == 'single'))
    })
  }
))