这是一个伪代码,用于根据使用和所做的选择生成绘图。radioButton
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())
})
}
}
}
我试图通过在radioButton
value='sample' 并被actionButton
触发时执行特定函数来生成绘图。radioButton
同样,当value='group' 并被actionButton
触发时,执行另一个特定的函数。我尝试使用if
和observeEvent
如上所示。但是,这并没有按预期工作。它给出了以下错误:
.getReactiveEnvironment()$currentContext 中的错误:如果没有活动的反应上下文,则不允许操作。(你试图做一些只能从反应式表达式或观察者内部完成的事情。)
如何根据radiobutton
和actionButton
值生成绘图?谢谢,
版本 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())
})
})
}
这只是从第二个observe
whereinput$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())
})
})
}
}
}