1

Here is a pseudo code to generate a based on the choice made with radioButton and actionButton.

ui <- fluidPage(
mainPanel(
tabsetPanel(
  tabPanel("Plots",  
           fluidRow(column(4,wellPanel(
             actionButton("action_plot","Generate Plots"),  
             h6(textOutput("numheat")),
             radioButtons("plot_subset",label="Chose by sample or group?",
                          choices=c("Sample","Group"),selected="Sample"),

             conditionalPanel("input.plot_subset=='Sample'",
                              selectizeInput("view_sample_plot",
                                             label = h5("Select Samples"),
                                             choices = NULL,
                                             multiple = TRUE,
                                             options = list(placeholder = 'select samples to plot')
                              )
             ),
             conditionalPanel("input.plot_subset=='Group'",
                              checkboxGroupInput("view_group_plot",
                                                 label=h5("Select Groups to View"),
                                                 choices="",
                                                 selected="")
             )
           )),
           column(8,
                  tabsetPanel(
                    tabPanel(title="Plot",
                             #textOutput("which_genes"),
                             h4(textOutput("plot_title")),
                             plotOutput("plot_rna",height="800px")
                    )
                  )
           )
           )
  )
)
)
)

server <- function(input, output, session) {

if(input$plot_subset=='Sample'){
observeEvent(input$action_plot ,{ 
output$plot_rna <- renderPlot({
data_plot1()
function1_plot(data_plot1())
})
})
} else if(input$plot_subset=='group'){
observeEvent(input$action_plot ,{
output$plot_rna <- renderPlot({
data_plot2()
function2_plot(data_plot2())
})
}
}
}

I am trying to generate a plot by executing specific function when radioButton value='sample' and actionButton is triggered. Similarly, execute another specific function when the radioButton value='group' and actionButton is triggered. I tried using if and observeEvent as shown above. However, this is not working as expected. It gives the below error:

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

How can I generate plot based on the radiobutton and actionButton values? Thanks,

Version 2: As per the suggestion, i have put the if in observer environment as shown below:

server <- function(input, output, session) {

observe({(input$plot_subset=='Sample'})
observeEvent(input$action_plot ,{ 
output$plot_rna <- renderPlot({
data_plot1()
function1_plot(data_plot1())
})
})

observe({input$plot_subset=='Group'})
observeEvent(input$action_plot ,{
output$plot_rna <- renderPlot({
data_plot2()
function2_plot(data_plot2())
})
})
}

This is only producing the plot from the second observe where input$plot_subset=='Group' but not from the first when input$plot_subset=='Sample'

Version 3: also has the same issue as version 2.

   server <- function(input, output, session) {

observe({if(input$plot_subset=='Sample'){
observeEvent(input$action_plot ,{ 
output$plot_rna <- renderPlot({
data_plot1()
function1_plot(data_plot1())
})
})
} else if(input$plot_subset=='Group'){
observeEvent(input$action_plot ,{
output$plot_rna <- renderPlot({
data_plot2()
function2_plot(data_plot2())
})
})
}
}
}
chas
  • 1,565
  • 5
  • 26
  • 54
  • Try to put your `if` conditions inside a unique `observe` environment – bretauv Mar 26 '20 at 14:24
  • seems i have put the condition inside `observe` in an incorrect way as it is giving the output only from the later observe block . could you hint to fix the behaviour – chas Mar 26 '20 at 15:09
  • You incorrectly used `observe`. Try `observe({ all your conditions and observeEvent, etc. })` – bretauv Mar 26 '20 at 15:20
  • still heading in the wrong direction in Version 3 – chas Mar 26 '20 at 15:30

1 Answers1

4

As your example is not reproducible, here's an example with mtcars and iris:

library(shiny)

ui <- fluidPage(
  radioButtons(inputId = "test", "radio buttons", choices = c("iris", "mtcars")),
  actionButton("run", "Run"),
  plotOutput("plot")
  )

server <- function(input, output, session) {

  observe({
    observeEvent(input$run, {
      if (input$test == "iris") {
        output$plot <- renderPlot({
          plot(iris)
        })
      }

      else if (input$test == "mtcars") {
        output$plot <- renderPlot({
          plot(mtcars)
        })
      }
    })
  })

}

shinyApp(ui, server)
bretauv
  • 7,756
  • 2
  • 20
  • 57