【问题标题】:making conditionalPanel work with input parameter in a R shiny Golem package使 conditionalPanel 与 R 闪亮 Golem 包中的输入参数一起工作
【发布时间】:2021-09-29 09:43:39
【问题描述】:

我通常会看到 conditionalPanel 在发生事件时工作,例如按下按钮或选中复选框等。但在这里,我希望它与运行应用程序时传递的参数一起工作。我试图仅在“输入”参数不为空时显示条件面板,但面板一直出现。这是我尝试过的代码:

app_ui.R

#' 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

#' 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

#' 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)
  )
}

【问题讨论】:

    标签: r shiny golem


    【解决方案1】:

    get_golem_options("input") 的美妙之处在于它也可以在你的 UI 中使用 :)

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

    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(...)
          }
        )
      )
    }
    

    【讨论】:

    • 哇,现在我知道我可以在 UI 中编写 if 语句了。我以前认为这是不可能的。
    • 是的,你可以做tagList( if(this) h1("this"), h2("hello") ) :)
    • 这整行是什么意思? tagList( if(this) h1("this"), h2("hello") )
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2018-09-23
    相关资源
    最近更新 更多