R 如何从闪亮的应用程序中消除占位符空白?
当我制作闪亮的应用程序时,我经常为如何在页面上为用户安排绘图和文本输出而苦恼。因此,我想尝试一种方法,让用户能够选择要显示的输出。例如,用户可能能够显示3个图形和2个文本块,但可能只想显示第一个图形和一个文本块。我通常可以在渲染函数中使用if语句来完成这项工作。但是,我发现shiny仍然会添加空白,即使…用户没有指示应该显示绘图。这似乎是某种占位符空间,最终在应用程序中留下了大洞。在我的示例中,应用程序显示第二个绘图,但有一大块空白使页面看起来很难看 我发现:这不太适用。似乎没有一篇文章能解决这个具体问题R 如何从闪亮的应用程序中消除占位符空白?,r,ggplot2,shiny,R,Ggplot2,Shiny,当我制作闪亮的应用程序时,我经常为如何在页面上为用户安排绘图和文本输出而苦恼。因此,我想尝试一种方法,让用户能够选择要显示的输出。例如,用户可能能够显示3个图形和2个文本块,但可能只想显示第一个图形和一个文本块。我通常可以在渲染函数中使用if语句来完成这项工作。但是,我发现shiny仍然会添加空白,即使…用户没有指示应该显示绘图。这似乎是某种占位符空间,最终在应用程序中留下了大洞。在我的示例中,应用程序显示第二个绘图,但有一大块空白使页面看起来很难看 我发现:这不太适用。似乎没有一篇文章能解决这
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
plotOutput("distPlot"),
plotOutput("distPlot2"))))
server <- function(input, output) {
output$distPlot <- renderPlot({
if(input$bins == 4){
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')})
}
shinyApp(ui = ui, server = server)
库(闪亮)
ui在这种特殊情况下,有两种可能的选择,一种是将第一个绘图包装在shinny::conditionalPanel()
中,另一种是使用shinny::renderUI()
Shining::conditionalPanel()
示例
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
conditionalPanel("input.bins == 4", plotOutput("distPlot")),
plotOutput("distPlot2"))))
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')})
}
shinyApp(ui = ui, server = server)
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
uiOutput("distPlots"))))
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')})
output$distPlots <- renderUI({
if(input$bins == 4)
fluidPage(plotOutput("distPlot"), plotOutput("distPlot2"))
else
plotOutput("distPlot2")
})
}
shinyApp(ui = ui, server = server)
这两种方法都产生了以下效果:
这是一个很好的解决方案,但我可能有太多的输出组合,它们依赖于不同的输入组合,因此不实用。通过在有问题的图形上调整plotOutput函数中的height参数,我能够接近我所需要的。