【发布时间】:2020-07-07 04:54:19
【问题描述】:
这是一个伪代码,用于根据radioButton 和actionButton 的选择生成plot。
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())
})
})
}
这只是从第二个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())
})
})
}
}
}
【问题讨论】:
-
尝试将您的
if条件放在一个独特的observe环境中 -
似乎我以不正确的方式将条件放入
observe中,因为它仅从后面的观察块中提供输出。你能提示修复这种行为吗 -
您错误地使用了
observe。试试observe({ all your conditions and observeEvent, etc. }) -
在版本 3 中仍然朝着错误的方向前进
标签: plot r user-interface shiny radio-button action-button