r闪亮的面板滚动条移动文本而不是单选按钮

时间:2015-12-04 01:40:44

标签: r shiny

我希望sidebarPanel包含大量内嵌 radioButton小部件,以控制mainPanel中某些输出的行为。我希望能够滚动浏览radioButton小部件,而无需向下滚动mainPanel。但是,我到目前为止尝试这样做的方式导致整个页面滚动,或sidebarPanel滚动,但留下单选按钮(看起来非常奇怪)。请注意,当我将radioButton窗口小部件保持垂直(inline = FALSE)时,不会发生这种情况。通过以下代码,您可以更改radioButton窗口小部件是否在inline打开或关闭的情况下显示。

ui.R

library(shiny)
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      id = "tPanel",style = "overflow-y:scroll; max-height: 600px",
      uiOutput("RadioGrid2")),
    mainPanel(uiOutput("InlineChooser")) )
))

server.R

    library(shiny)
shinyServer(function(input, output) {
  #Data
  RowNames = LETTERS
  ColumnNames = c("1","2","3","4","5")

  #Define a function that creates rows of radio buttons
  RadioRow = function(label, opts){
    choices = as.list(1:length(opts))
    names(choices) = opts
    radioButtons(paste0("Row",label),label=label,choices=choices,selected=1, inline = input$InRows)}

  #Define a function that creates the radio grid
  RadioGrid = function(RowNames, ColumnNames){lapply(X = RowNames, FUN = RadioRow, opts = ColumnNames)}

  #define a reactive object to hold the radio grid
  WidgetGrid2 = reactive({RadioGrid(RowNames, ColumnNames)})

  #create the output object to display the grid
  output$RadioGrid2 = renderUI({tagList(WidgetGrid2())})

  #create output object to select whether to do inline or not
  output$InlineChooser = renderUI(radioButtons("InRows", label = "Put in rows?", choices = list("yes" = TRUE, "no" = FALSE), selected = TRUE))
})

如何设置它以便在inline开启时滚动条有效?

1 个答案:

答案 0 :(得分:2)

当您在行中显示单选按钮时,它们具有CSS postion:relative,因此将postion:relative设置为容器div有助于正确定位它们(​​而不是浮动)。试试这个:

library(shiny)

ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      id = "tPanel",style = "overflow-y:scroll; max-height: 600px; position:relative;",
      uiOutput("RadioGrid2")),
    mainPanel(uiOutput("InlineChooser")) )
))

server <- shinyServer(function(input, output) {
  #Data
  RowNames = LETTERS
  ColumnNames = c("1","2","3","4","5")

  #Define a function that creates rows of radio buttons
  RadioRow = function(label, opts){
    choices = as.list(1:length(opts))
    names(choices) = opts
    radioButtons(paste0("Row",label),label=label,choices=choices,selected=1, inline = input$InRows)}

  #Define a function that creates the radio grid
  RadioGrid = function(RowNames, ColumnNames){lapply(X = RowNames, FUN = RadioRow, opts = ColumnNames)}

  #define a reactive object to hold the radio grid
  WidgetGrid2 = reactive({RadioGrid(RowNames, ColumnNames)})

  #create the output object to display the grid
  output$RadioGrid2 = renderUI({tagList(WidgetGrid2())})

  #create output object to select whether to do inline or not
  output$InlineChooser = renderUI(radioButtons("InRows", label = "Put in rows?", choices = list("yes" = TRUE, "no" = FALSE), selected = TRUE))
})

shinyApp(ui, server)