如何避免R中render()函数中的代码重复

如何避免R中render()函数中的代码重复,r,function,shiny,R,Function,Shiny,我是新来的闪亮,所以请原谅,如果这是琐碎的 我有一个server()函数,可以输出几个绘图。这些绘图使用相同的代码块,每个都添加了自己的代码块,如下所示: shinyServer( function( input, output ){ output$plot1 <- renderPlot({ the block of the same code some code specific to plot1 }) output$plot1 <- renderPlot({ The

我是新来的闪亮,所以请原谅,如果这是琐碎的

我有一个server()函数,可以输出几个绘图。这些绘图使用相同的代码块,每个都添加了自己的代码块,如下所示:

shinyServer( function( input, output ){
output$plot1 <- renderPlot({
  the block of the same code
  some code specific to plot1
})
output$plot1 <- renderPlot({
  The block of the same code
  some code specific to plot2
})
shinyServer(功能(输入、输出){

输出$plot1您可能希望将块放入一个“反应式”函数中,就像函数一样,但它会对更改作出反应

shinyServer( function( input, output ){
  basicplot = reactive({
                 the block of the same code
              })
output$plot1 <- renderPlot({
  plot1 = basicplot()
  some code specific to plot1
})
output$plot2 <- renderPlot({
  plot2 = basicplot()
  some code specific to plot2
})
shinyServer(功能(输入、输出){
基本点=无功({
相同代码的块
})

输出$plot1谢谢。我还听了关于reactive的视频。这正是我需要的。我想补充一点,如果它利用了任何输入,你只需要使
basiclot
reactive。如果它独立于输入,那么它应该是一个传统的功能。