【问题标题】:UI function in Shiny RShiny R 中的 UI 功能
【发布时间】:2021-09-04 13:48:54
【问题描述】:

我需要在 Shiny 的其他输入功能中添加一些输入,我不知道该怎么做,我已经完成了一个 renderUI,然后是一个 uiOutput,但我没有功能...... 我添加代码:


inputs = function(dades1){
  for (i in 1:ncol(dades1)) {
      if (dades1[i]=="Edad") {
      
      insertUI(     
        selector = "$Edad",
        where = "afterEnd",
        multiple = TRUE, immediate = TRUE,
        ui = radioButtons( inputId = "sel1", 
                           choices = c("1","0"), 
                           selected = c(1,0)))
    }
    
    else if (dades1[i]=="Gender") {
      insertUI(     
        selector = "$Gender",
        where = "afterEnd",                 multiple = TRUE, immediate = TRUE,
        ui = 
          radioButtons( inputId = "sel2", 
                        choices = c("1","0"), 
                        selected = c(1,0)))}}}

(我已经缩短了函数,但还有更多的 'else ifs') 然后一旦完成我添加到服务器的功能:

output$cc<-renderUI({
          dades1<-select(cov,input$vars)
          dades1<-cbind(UCI,dades1)
          inputs(dades1)
        })

最后到 ui:

uiOutput("cc"),

【问题讨论】:

  • 您的问题不完整,因为您只提供了应用程序的一部分,因此很难提供答案。下次您将创建一个问题时,请记住它必须是可重现的。你不应该在renderUI 中使用insertUI。 insertUI 背后的想法是在例如内部使用它。 observeEvent。我认为你应该重写你的应用程序。也请查看shinyjs::hide/shinyjs::show,他们可能就是您要找的。​​span>

标签: r function user-interface shiny


【解决方案1】:
  1. insertUI 通常在观察者内部被调用。无需在响应式中调用它。
  2. radioButtons 缺少参数,选择的长度应为 1。

我制作了一个示例应用程序,它调用一个在按下按钮时插入 ui 的函数。

library(shiny)
library(purrr)

inputs <- function(dades1) {
    
    #same as for (i in 1:ncol(dades1)) {...}
    walk(1:ncol(dades1), ~{
        
        #.x instead of i
        if (names(dades1[.x]) == 'Species') {
            
            insertUI(     
                selector = "#add",
                where = "afterEnd",
                multiple = TRUE, immediate = TRUE,
                ui = radioButtons( inputId = "sel1",
                                   label = 'New Input',
                                   choices = c("1","0"), 
                                   selected = 1))
        }
        
    })
    
}


ui <- fluidPage(
  actionButton('add', 'Add Input')
)

server <- function(input, output, session) {
  
    observeEvent(input$add, {
        
        inputs(iris)
    
    })    
    
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多