我想在我的闪亮应用中创建一个动态用户界面,用户可以指定各种面板的列宽。是否可以在服务器中呈现列宽并将其作为输出传递给UI,或者宽度是否需要在UI中进行硬编码?
答案 0 :(得分:0)
这是一个单个文件脚本,可让用户选择两个面板的大小。最初我没有看到两个挑战,可能是以更清洁的方式处理。他们是:
ifelse(...)
函数中完成的。如果一个面板的宽度更改为大于6,则另一个面板需要缩小,以使总和不大于12.
old_panel_width1 <<- 6
old_panel_width2 <<- 6
ui <- bootstrapPage(
uiOutput("dynamic_panels")
)
server <- function(input, output) {
n1 <- reactive(input$n1)
n2 <- reactive(input$n2)
output$dynamic_panels <- renderUI({
#assigns an initial value of 6 when first loaded
panel_width1 <<- ifelse(is.null(input$n1), 6, n1())
panel_width2 <<- ifelse(is.null(input$n2), 6, n2())
#one way of detecting whether panel 1 or 2 was changed and then adjusting the sizes so that the sum < 12
if (panel_width1!= old_panel_width1){
if (panel_width1 > 6 && sum(panel_width1,panel_width2) > 12) panel_width2 <<- 12 - panel_width1
old_panel_width1 <<- panel_width1
}
if (panel_width2!= old_panel_width2){
if (panel_width2 > 6 && sum(panel_width1,panel_width2) > 12) panel_width1 <<- 12 - panel_width2
old_panel_width2 <<- panel_width2
}
fluidRow(
column(width = panel_width1,
numericInput('n1', 'size of panel 1', min = 1, max = 11,value = panel_width1),
plotOutput("plot1")
),
column(width = panel_width2,
numericInput('n2', 'size of panel 2',min = 1, max = 11,value = panel_width2),
plotOutput("plot2")
)
)
})
output$plot1 <- renderPlot(hist(runif(100)))
output$plot2 <- renderPlot(hist(runif(100)))
}
# Return a Shiny app object
shinyApp(ui = ui,server = server)