R发光的多重情节 [英] Multiple Plots in R Shiny

查看:168
本文介绍了R发光的多重情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Shiny应用程序的主面板中显示多个图。



我使用



这是我的应用程序的样子,只有一个图





这里是我的ui.R

  shinyUI(fluidPage(


titlePanel(Baseball ),

sidebarLayout(
sidebarPanel(
selectInput($ b $var,label =选择一组可视化),
choices = c (总体=总体,进攻=进攻,投手=投手,辛普森一家=辛普森),
选择=进攻)
),

mainPanel(
plotOutput(plotOffense)





我是否需要使用某些东西而不是mainPanel,以允许在浏览器中放置更多图形?

解决方案

嗯,实际上只有两件事情必须发生: plotOutput 为实际输出创建div,并且需要调用 renderPlot 以便以正确的方式格式化绘图。所以,下面是一些可以动态实现这个功能的函数,并且让您可以使用宽度/高度/列数(类似于多槽)进行操作,只是采用闪亮的方式。请参阅此要点



我把东西分成函数,但它也可以直接放在服务器函数中。



编辑:我忘了提及,宽度和高度输入框是文本,并且应该是有效的CSS,例如它可以是10,10px或10%。



<$一些示例数据
dat< - setNames(data.frame(matrix())($) (runif(100),10)),letters [1:10])
dat $ time< --seq(nrow(dat))

##做一些随机地块,因为它看起来更酷
##但是你只需定义10个不同的图
rndmPlot< - function(input)
sample(list(geom_line(),geom_bar(stat ='identity')) geom_point(),geom_jitter(),
geom_density(aes_string(x = input $ var),inherit.aes = FALSE)),1)

makePlotContainers< - function(n,ncol = 2,prefix =plot,height = 100,width =100%,...){
##验证输入
validateCssUnit(width)
validateCssU nit(height)

##构造plotOutputs
lst < - lapply(seq.int(n),函数(i)
plotOutput(sprintf('%s_%g ',prefix,i),height = height,width = width))

##使列
lst < - lapply(split(lst,(seq.int(n) - 1)%/%ncol),函数(x)列(12 / ncol,x))
do.call(tagList,lst)
}

renderPlots< - 函数(n,input,output,prefix =plot){
for(i in seq.int(n)){
local({
ii< - i#need i这里评价
##这些将是你的10个图而不是
的输出[[sprintf('%s_%g',prefix,ii)]]< - renderPlot({
ggplot(dat ,aes_string(x ='time',y = input $ var))+ rndmPlot(input)
})
})
}
}

ui< - shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput('nplots','图的数量',min = 1,max = 1 0,value = 8),
selectInput(var,label =Choose,choices = letters [1:10]),
textInput('height','Plot Height',value = 100),
textInput('width','Width',value =100%),
sliderInput('ncol','Columns',min = 1,max = 3,value = 2)
),
mainPanel(
uiOutput('plots')





server< - shinyServer(function(input,output){
output $ plots< - renderUI({
makePlotContainers(input $ nplots,ncol = input $ ncol,height = input $ height ,width = input $ width)
})
observeEvent(输入$ nplots,renderPlots(输入$ nplots,输入,输出))
})

shinyApp ui,server)


I'm attempting to display multiple plots in my main panel in my Shiny app.

I am using the multiplot function from R cookbook

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)

# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)

numPlots = length(plots)

# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
  # Make the panel
  # ncol: Number of columns of plots
  # nrow: Number of rows needed, calculated from # of cols
  layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                   ncol = cols, nrow = ceiling(numPlots/cols))
}

if (numPlots==1) {
  print(plots[[1]])

} else {
  # Set up the page
  grid.newpage()
  pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

  # Make each plot, in the correct location
  for (i in 1:numPlots) {
    # Get the i,j matrix positions of the regions that contain this subplot
    matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

    print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                    layout.pos.col = matchidx$col))
  }
}
}

And then calling multiplot

    multiplot(p1,p2,p3,p4,p5,p6, cols=1)

But it looks like it's trying to squish everything in to the main panel

Here is what my app looks like with only one plot

Here is my ui.R

    shinyUI(fluidPage(


  titlePanel("Baseball"),

  sidebarLayout(
    sidebarPanel(
      selectInput(
        "var", label = "Choose a group to visualize",
        choices =c(Overall = "overall", Offense = "offense", Pitchers = "pitchers", Simpsons = "simpsons"),
        selected = "offense")
    ),

    mainPanel(
      plotOutput("plotOffense")

    )
    )
    )
    )

Do I need to be using something instead of mainPanel that allows for more graphics to be placed in the browser?

解决方案

Well, there are really just two things that have to happen: plotOutput should be called to create the div for the actual output, and renderPlot needs to be called to format the plot in the correct way. So, here are a some functions that can do this dynamically, and let you play with the width/height/number of columns, similar to the multiplot, only in a shiny way. Refer to this gist as well.

I separated things into functions, but it could be just put straight into the server function as well.

EDIT: I forgot to mention, the width and height entry boxes are text, and should be valid CSS, so it could be 10, 10px, or 10% for example.

library(shiny)
library(ggplot2)

## Some sample data
dat <- setNames(data.frame(matrix(runif(100),10)), letters[1:10])
dat$time <- seq(nrow(dat))

## Make some random plots because it looks cooler
## But you would just define your 10 different plots
rndmPlot <- function(input)
    sample(list(geom_line(), geom_bar(stat='identity'), geom_point(), geom_jitter(),
               geom_density(aes_string(x=input$var), inherit.aes=FALSE)), 1)

makePlotContainers <- function(n, ncol=2, prefix="plot", height=100, width="100%", ...) {
    ## Validate inputs
    validateCssUnit(width)
    validateCssUnit(height)

    ## Construct plotOutputs
    lst <- lapply(seq.int(n), function(i)
        plotOutput(sprintf('%s_%g', prefix, i), height=height, width=width))

    ## Make columns
    lst <- lapply(split(lst, (seq.int(n)-1)%/%ncol), function(x) column(12/ncol, x))
    do.call(tagList, lst)
}

renderPlots <- function(n, input, output, prefix="plot") {
    for (i in seq.int(n)) {
        local({
            ii <- i  # need i evaluated here
            ## These would be your 10 plots instead
            output[[sprintf('%s_%g', prefix, ii)]] <- renderPlot({
                ggplot(dat, aes_string(x='time', y=input$var)) + rndmPlot(input)
            })
        })
    }
}

ui <- shinyUI(
    fluidPage(
        sidebarLayout(
            sidebarPanel(
                sliderInput('nplots', 'Number of Plots', min=1, max=10, value=8),
                selectInput("var", label = "Choose", choices=letters[1:10]),
                textInput('height', 'Plot Height', value="100"),
                textInput('width', 'Width', value="100%"),
                sliderInput('ncol', 'Columns', min=1, max=3, value=2)
            ),
            mainPanel(
                uiOutput('plots')
            )
        )
    )
)

server <- shinyServer(function(input, output) {
    output$plots <- renderUI({
        makePlotContainers(input$nplots, ncol=input$ncol, height=input$height, width=input$width)
    })
    observeEvent(input$nplots, renderPlots(input$nplots, input, output))
})

shinyApp(ui, server)

这篇关于R发光的多重情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆