【问题标题】:Dynamic plot height in ShinyShiny中的动态绘图高度
【发布时间】:2015-10-31 04:16:09
【问题描述】:

我有一个闪亮的应用程序,其中的情节需要根据用户输入调整高度。基本上,情节可以有一个、两个或四个子情节。当有一个或两个时,一切都很好,但是如果有四个,子图就会被压缩到太小的尺寸。我尝试使用反应函数来计算服务器的高度,但出现此错误:

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.)

我正在尝试做的一个非常简化的版本在这里:

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)

【问题讨论】:

  • 实际上,将height = PlotHeight() 替换为height = function() PlotHeight() 就足以使您的示例工作。
  • @Max 你能解释一下为什么/如何工作吗?
  • @mihagazvoda 请参阅 renderPlot 的帮助页面 - shiny.rstudio.com/reference/shiny/latest/renderPlot.html - 他们提到您需要提供一个函数,要了解它为什么起作用,请检查函数源 - 在那里你可以看到如果该函数以高度(或宽度)的形式提供,然后在reactive() 包装器中执行。事实上查看源代码,我意识到在这种情况下也可以提供height = PlotHeight,它也可以工作(但似乎没有记录)

标签: r shiny


【解决方案1】:

使用renderUI:

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)

【讨论】:

  • 完美!!谢谢!!
猜你喜欢
  • 1970-01-01
  • 2020-12-17
  • 2014-04-19
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多