首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态图高

动态图高
EN

Stack Overflow用户
提问于 2015-08-07 16:23:42
回答 1查看 7.3K关注 0票数 15

我有一个闪亮的应用程序,其中的情节需要调整的高度,根据用户的输入。基本上,情节可能有一个,两个,或四个子图。一切都是好的,当有一两个,但有四个,子情节被压到一个太小的大小。我尝试使用一个反应性函数来给出服务器的计算高度,但是我得到了以下错误:

代码语言:javascript
复制
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

我想要做的一个非常简单的版本是:

代码语言:javascript
复制
library(shiny)

ui <- fluidPage(

   fluidRow(
      column(2, 
             radioButtons( inputId = 'plotcount', label = 'Plot Count', 
                                 choices = c('1' = 1, 
                                             '2' = 2,
                                             '4' = 4
                                 ),
                           selected = '1'
             )
      ),
      column(10, 
             plotOutput( outputId = 'plots' )
      )
   )
)

server <- function(input, output) {

   PlotHeight = reactive(
      return( 500+250*(floor(input$plotcount/4)))
   )

   output$plots = renderPlot(height = PlotHeight(), {

      if( as.numeric(input$plotcount) == 0 ){
         plot.new()
         return()
      }
      print(c( floor(sqrt(as.numeric(input$plotcount))),
               ceiling(sqrt(as.numeric(input$plotcount)))
      ))
      opar = par( mfrow = c( floor(sqrt(as.numeric(input$plotcount))),
                             ceiling(sqrt(as.numeric(input$plotcount)))
                             )
                  )
      for( i in 1:as.numeric(input$plotcount) ){
         plot(1:100, 1:100, pch=19)
      }
      par(opar)
   })
}

shinyApp(ui =ui, server = server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-07 17:29:33

使用renderUI

代码语言:javascript
复制
library(shiny)

ui <- fluidPage(
  fluidRow(
    column(
      width = 2
      , radioButtons(
          inputId = 'plotcount'
        , label   = 'Plot Count'
        , choices = as.character(1:4)
      )
    ),
    column(
      width = 10
      , uiOutput("plot.ui")
    )
  )
)

server <- function(input, output) {

  plotCount <- reactive({
    req(input$plotcount)
    as.numeric(input$plotcount)
  })

  plotHeight <- reactive(350 * plotCount())      

  output$plots <- renderPlot({

    req(plotCount())

    if (plotCount() == 0){
      plot.new()
      return()
    }

    opar <- par(mfrow = c(plotCount(), 1L))

    for (i in 1:plotCount()) {
      plot(1:100, 1:100, pch = 19)
    }

    par(opar)
  })

  output$plot.ui <- renderUI({
    plotOutput("plots", height = plotHeight())
  })

}

shinyApp(ui = ui, server = server)
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31882463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档