我希望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
开启时滚动条有效?
答案 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)