У меня есть блестящее приложение, в котором график нужно регулировать по высоте в зависимости от ввода пользователя. В основном на участке может быть один, два или четыре подсюжета. Все хорошо, когда их один или два, но с четырьмя подзаголовками становится слишком мало. Я пытался использовать реактивную функцию, чтобы получить расчетную высоту с сервером, но получаю эту ошибку:
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()
, чтобы ваш пример заработал. - person Max   schedule 29.03.2019reactive()
оболочки. На самом деле, глядя на источник, я понял, что в этом случае также можно предоставитьheight = PlotHeight
, и он также работает (но кажется недокументированным) - person Max   schedule 19.05.2021