【问题标题】:How to reactively plot using radioButton and actionButton values in Shiny R如何在 Shiny R 中使用 radioButton 和 actionButton 值进行响应式绘图
【发布时间】:2020-07-07 04:54:19
【问题描述】:

这是一个伪代码,用于根据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 value='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())
})
})
}

这只是从第二个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


【解决方案1】:

由于您的示例不可重现,这里有一个带有 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)

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 2019-02-15
    • 2021-02-19
    • 2020-07-27
    • 2020-07-31
    • 2017-11-13
    • 2021-11-11
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多