如何在 Shiny R 中使用 radioButton 和 actionButton 值进行反应式绘图

How to reactively plot using radioButton and actionButton values in Shiny R

这是一个伪代码,用于根据 radioButtonactionButton 的选择生成

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

我正在尝试在触发 radioButton 值='sample' 和 actionButton 时通过执行特定函数来生成绘图。同样,当radioButton值='group'和actionButton被触发时,执行另一个特定的函数。如上所示,我尝试使用 ifobserveEvent。但是,这没有按预期工作。它给出以下错误:

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

如何根据 radiobuttonactionButton 值生成图表?谢谢,

版本 2: 根据建议,我将 if 放在 observer 环境中,如下所示:

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

这只会从第二个 observeinput$plot_subset=='Group' 处生成图,而不是从第一个 input$plot_subset=='Sample'

处生成图

版本 3: 也有与 版本 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())
})
})
}
}
}

由于您的示例不可重现,这里有一个 mtcarsiris 的示例:

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)