2015-08-07 21 views
5

Kullanıcı girişine bağlı olarak çizimin yükseklik ayarlaması gereken parlak bir uygulamasına sahibim. Temel olarak, arsa bir, iki veya dört alt parsellere sahip olabilir. Bir ya da iki tane olduğunda her şey yolundadır, ancak dört ile alt alanlar çok küçük bir boyuta ezilirler. Bana sunucuyla hesaplanan yükseklik vermek için reaktif bir işlevi kullanmak için denedim, ama bu hatayı alıyorum: Yapmam çalışılıyor neParlaklık içinde dinamik çizim yüksekliği

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

Çok basitleştirilmiş versiyonu buradadır:

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) 

cevap

8

Kullanım renderUI:

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, 
      uiOutput("plot.ui") 
    ) 
) 
) 

server <- function(input, output) { 

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

    output$plots = renderPlot({ 

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

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

} 

shinyApp(ui =ui, server = server) 
+0

Mükemmel !! Teşekkür ederim!! – KirkDCO

İlgili konular