【问题标题】:How to arrange or align the output of renderUI如何排列或对齐renderUI的输出
【发布时间】:2020-07-24 09:24:51
【问题描述】:

我正在学习如何使用renderUI 动态生成多个绘图。这是我设计的示例应用程序 (https://yuchenw.shinyapps.io/Format_UI_Example/)。我们的想法是设计一个应用程序,允许用户在mtcars 数据集中选择一个或多个参数,并将行索引和值动态绘制为散点图。

示例应用程序有效,但所有图都在一列中对齐。随着用户选择的参数越多,绘图的数量增加,网页的长度也增加。此外,还有很多空白。如果可能的话,我想将多个图排列或对齐为两列或三列结构,以减少网页的长度并减少空白。

我通常使用column 函数并设置width 参数来实现这一点。但我不知道如何使用renderUI。我将不胜感激。

这里是代码。

### This script creates an R shiny app that plot mpg, disp, and hp, from the mtcars data set

# Load packages
library(shiny)
library(tidyverse)

# Load data
data("mtcars")

# Add row id
mtcars2 <- mtcars %>% mutate(ID = 1:n())

# ui
ui <- fluidPage(
  sidebarPanel(
    selectInput(inputId = "sel", label = "Select one or more parameters",
                choices = names(mtcars), multiple = TRUE)
  ),
  mainPanel(
    uiOutput("plots")
  )
)

# server
server <- function(input, output, session){
  
  # Create plot tag list
  output$plots <- renderUI({
    plot_output_list <- lapply(input$sel, function(par) {
      plotname <- paste("plot", par, sep = "_")
      plotOutput(plotname)
    })
    
    do.call(tagList, plot_output_list)
  })
  
  # Dynamically generate the plots based on the selected parameters
  observe({
    req(input$sel)
    lapply(input$sel, function(par){
      output[[paste("plot", par, sep = "_")]] <- renderPlot({
        ggplot(mtcars2, aes_string(x = "ID", y = par)) +
          geom_point() +
          ggtitle(paste("Plot: ", par))
      },         
      width = 250,
      height = 250)
    })
  })
}

# Run app
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    试试这个:

    plotOutput(plotname, height = '250px', inline=TRUE)
    

    它会给你以下输出:

    【讨论】:

    • 谢谢。如果可能,您能否详细解释一下您是如何确定高度为250px 的?
    • 基于您在renderPlot 中的绘图大小。如果您想要更大的情节,请相应地进行设置。
    猜你喜欢
    • 1970-01-01
    • 2019-10-05
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2019-06-21
    相关资源
    最近更新 更多