首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使conditionalPanel在R闪亮的Golem包中使用输入参数

使conditionalPanel在R闪亮的Golem包中使用输入参数
EN

Stack Overflow用户
提问于 2021-07-22 04:37:37
回答 1查看 164关注 0票数 0

我通常会看到conditionalPanel在事件发生时工作,比如按下按钮或选中复选框等,但在这里,我希望它能够在运行应用程序时传递一个参数。只有当“输入”参数不是null时,我才会试图显示条件面板,但是面板总是会出现。下面是我尝试过的代码:

app_ui.R

代码语言:javascript
复制
#' The application User-Interface
#'
#' @param request Internal parameter for `{shiny}`.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # List the first level UI elements here
    fluidPage(
      h1("testapp"),
      conditionalPanel(
        condition = "!is.null(r.input)",
        p('there is an input ')

      )
    )
  )
}

app_server.R

代码语言:javascript
复制
#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_server <- function( input, output, session ) {
  # List the first level callModules here
  r <- reactiveValues(query = reactiveValues())

  observe({
    input <- golem::get_golem_options("input")
    r$input <- input

  })
}

run_app.R

代码语言:javascript
复制
#' Run the Shiny Application
#'
#' @param input input
#'
#' @export
#' @importFrom shiny shinyApp
#' @importFrom golem with_golem_options
run_app <- function(
  input = NULL
) {
  with_golem_options(
    app = shinyApp(
      ui = app_ui,
      server = app_server
    ),
    golem_opts = list(input = input)
  )
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-22 06:23:47

get_golem_options("input")的优点在于它也可以在您的UI中使用:)

所以我认为你可以做一些更简单的事情,比如:

代码语言:javascript
复制
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # List the first level UI elements here
    fluidPage(
      h1("testapp"),
      if (!is.null(golem::get_golem_options("input")){
          your_UI_here(...)
      }
    )
  )
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68479257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档