R 如何排列或对齐渲染器的输出
我正在学习如何使用renderUI动态生成多个绘图。下面是我设计的一个示例应用程序。其想法是设计一个应用程序,允许用户在mtcars数据集中选择一个或多个参数,并将行索引和值动态绘制为散点图 示例应用程序可以工作,但所有绘图都在一列中对齐。随着用户选择更多参数,绘图的数量增加,网页的长度也增加。此外,还有很多空白。如果可能的话,我希望将多个绘图排列或对齐为两列或三列结构,以减少网页的长度并减少空白 我通常使用column函数并设置width参数来实现这一点。但我不知道如何使用renderUI。我将感谢任何帮助 这是代码R 如何排列或对齐渲染器的输出,r,shiny,R,Shiny,我正在学习如何使用renderUI动态生成多个绘图。下面是我设计的一个示例应用程序。其想法是设计一个应用程序,允许用户在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)
试试这个:
plotOutput(plotname, height = '250px', inline=TRUE)
它将为您提供以下输出:
非常感谢。如果可能的话,你能解释一下你是如何根据renderPlot中的绘图大小来决定高度为250px的吗。如果你想要一个更大的图,相应地设置它。