R Shiny 使用 renderUI 和函数结果创建输入

R Shiny create input using renderUI and function results

我是 R Shiny 的新手,我正在尝试创建一个应用程序,我需要它尽可能地具有交互性。我正在处理的一个问题是这个。在 ui.R 我有以下内容:

helpText("Select a feature"),
uiOutput("sliders")

并且在 server.R 中:

output$sliders <- renderUI({
    selectInput('feature_name', 
                'Feature name', #Description  
                choice = c("F1"='1', "F2"='2'))
  })

我的问题是,是否可以更改 renderUI 中的某些内容,而不是静态设置 c("F1"='1', "F2"='2')) 我可以通过一个函数的结果,所以它可以更动态(一个函数根据用户所做的事情生成一个功能列表,并将列表传递给 renderUI 以创建 selectInput)。类似以下内容:

output$sliders <- renderUI({
    selectInput('feature_name', 
                'Feature name', #Description  
                choice = c(feature_creator(method)))
  })

其中 "feature_creator" 是一个函数,它 returns: "F1"='1', "F2"='2' 根据用户选择的方法 (变量 "method" 已定义并且我有值)。我的问题更具体是 "feature_creator" return 应该作为输出什么?

希望我的问题足够清楚。如果我应该在问题描述中添加任何内容,请告诉我。

任何帮助将不胜感激。谢谢

假设您真正需要的只是 method 参数,这并不难。

1) 在您的 ui 中输入 radioButtons(),这将使用户 select 一个方法,让我们给它 inputId="MethodChoice".

2) 在 selectInput 中的 choice 参数中,您应该使用 choice=c(feature_creator(input$MethodChoice))

然后feature_creator会根据用户选择的方法得到一个文本值。

为了在 choice 中工作,feature_creator 应该 return 命名列表,其格式类似于您硬编码的内容。