R语言 如何将tagList输入从列表修改为单个标签

ljo96ir5  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(78)

我使用下面的代码在我的R shiny应用程序中创建一个独立的radioactive列表。

req(files_list_creator_1())
    numVar <- asInt(length(files_list_creator_1()))
    print(numVar)
    
    taglist = list(radioButtons(inputId = "file_num", label ="File number", choices = c("Choices will appear here")))
    for (i in 1:numVar){
      taglist = append(taglist, list(radioButtons(inputId = paste("x_var_of_file", i, sep ="_") , label = paste("x Variables of file", i, sep = "-"), choices = c("Choices will appear here"))))
      taglist = append(taglist, list(radioButtons(inputId = paste("y_var_of_file", i, sep ="_") , label = paste("y Variables of file", i, sep = "-"), choices = c("Choices will appear here"))))
      print(taglist)
      
    }
tagList(taglist)
})

这里files_list_creator_1()是一个React式服务器函数,它读取用户上传的Excel文件作为文件框,然后将它们组合成一个列表,并将此列表作为输出返回。
在运行代码时,应用程序加载时没有任何错误,但无线电广播列表不可见。有人能帮忙解决这个问题吗?我正在使用uiOutput(“Variables”)来输出单选按钮
tagList是一个R函数,taglist是我自己的列表,我在其中存储标记。我认为问题在于tagList应该是这样工作的:tagList(tag1,tag2,....),而我试图让它工作:tagList(list(tag1,tag2,...))如何更改此设置?
下面是一个可复制示例的完整代码和相关部分:

library(shiny)
library(readxl)
library(tidyverse)
library(here)
library(ggplot2)
library(plotly)
library(dplyr)
library(checkmate)
library(reshape2)
library(data.table)

ui <- fluidPage(
  titlePanel("Test App"),
  
  mainPanel(
    tabsetPanel(
      tabPanel(
        "Data",        
        fileInput("file_1", "Please choose Excel file", multiple = TRUE, accept = ".xlsx", buttonLabel = "Browse"),
        uiOutput("Variables")
        
      ),
      
      tabPanel(
        "Comparison Panel",

        
      )
      
      
    )
    
  )
  
)

server <- function(input, output, session){
  
  files_list_creator_1 <- reactive({
    files_list <- list()
    for (paths in input$file_1$datapath){  
      file_to_add <- as.data.frame(read_excel(paths))
      print(as.data.frame(read_excel(paths)))
      files_list <- append(files_list, list(file_to_add))
    
    }
    return(files_list) 
    
  })
#--------------------------------------------  
  output$variables <- renderUI({
    req(files_list_creator_1())
    numVar <- asInt(length(files_list_creator_1()))
    print(numVar)
    
    taglist = list(radioButtons(inputId = "file_num", label ="File number", choices = c("Choices will appear here")))
    for (i in 1:numVar){
      taglist = append(taglist, list(radioButtons(inputId = paste0("x_var_of_file", i, sep ="_") , label = paste("x Variables of file", i, sep = "-"), choices = c("Choices will appear here"))))
      taglist = append(taglist, list(radioButtons(inputId = paste0("y_var_of_file", i, sep ="_") , label = paste("y Variables of file", i, sep = "-"), choices = c("Choices will appear here"))))
      
    }
    do.call("tagList", taglist)
  })

shinyApp(ui = ui, server = server)
f87krz0w

f87krz0w1#

Shiny的UI函数接受列表很好(也没有renderUI-你真的需要它吗?)。请查看以下 * 可复制 * 的示例:

library(shiny)

ui <- fluidPage(
  uiOutput("rbs")
)

server <- function(input, output, session) {
  output$rbs <- renderUI({
    my_list_of_tags <- lapply(paste0("rb_", 1:10), function(x){radioButtons(inputId = x, label = x, choices = c("A", "B"))})
    # my_tagList <- do.call(tagList, my_list_of_tags) # not needed
  })
}

shinyApp(ui, server)

**编辑:**上面的例子的问题是,你正在使用uiOutput("Variables")(“V”)沿着output$variables <- renderUI(“v”)。因此,不呈现输出。

代码的固定版本:

library(shiny)
library(readxl)
library(tidyverse)
library(here)
library(ggplot2)
library(plotly)
library(dplyr)
library(checkmate)
library(reshape2)
library(data.table)

ui <- fluidPage(
  titlePanel("Test App"),
  mainPanel(
    tabsetPanel(
      tabPanel(
        "Data",        
        fileInput("file_1", "Please choose Excel file", multiple = TRUE, accept = ".xlsx", buttonLabel = "Browse"),
        uiOutput("variables")
      ),
      tabPanel(
        "Comparison Panel"
      )
    )
  )
)

server <- function(input, output, session){
  files_list_creator_1 <- reactive({
    files_list <- list()
    for (paths in input$file_1$datapath){  
      file_to_add <- as.data.frame(read_excel(paths))
      print(as.data.frame(read_excel(paths)))
      files_list <- append(files_list, list(file_to_add))
    }
    return(files_list) 
  })
  output$variables <- renderUI({
    req(files_list_creator_1())
    numVar <- asInt(length(files_list_creator_1()))
    print(numVar)
    taglist = list(radioButtons(inputId = "file_num", label ="File number", choices = c("Choices will appear here")))
    for (i in 1:numVar){
      taglist = append(taglist, list(radioButtons(inputId = paste0("x_var_of_file", i, sep ="_") , label = paste("x Variables of file", i, sep = "-"), choices = c("Choices will appear here"))))
      taglist = append(taglist, list(radioButtons(inputId = paste0("y_var_of_file", i, sep ="_") , label = paste("y Variables of file", i, sep = "-"), choices = c("Choices will appear here"))))
    }
    do.call("tagList", taglist)
  })
}

shinyApp(ui = ui, server = server)

相关问题