1

这是一个伪代码,用于根据使用和所做的选择生成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())
})
}
}
}

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

.getReactiveEnvironment()$currentContext 中的错误:如果没有活动的反应上下文,则不允许操作。(你试图做一些只能从反应式表达式或观察者内部完成的事情。)

如何根据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())
})
})
}

这只是从第二个observewhereinput$plot_subset=='Group'而不是从第一个 when产生情节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())
})
})
}
}
}
4

1 回答 1

3

由于您的示例不可重现,因此这里有一个带有mtcarsand的示例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)
于 2020-03-26T15:43:17.700 回答