如何解决将输入返回为反应式闪亮列表中的列表-插入UI中的动态UI
我正在使用先前解决方案R shiny dynamic UI in insertUI中的代码
现在在我的应用中,输入由服务器呈现,并显示为不同的元素/行,如下所示
$row_1
[1] "LV1" "x1" "x2" "x3"
$row_2
[1] "LV2" "x4" "x5" "x6"
我实际上是希望得到这样的东西:
"LV1" "x1" "x2" "x3"
"LV2" "x4" "x5" "x6"
我尝试了一些操作,但是不确定如何更改handle()
函数以获取所需的输出。
屏幕截图和代码:
如果尝试运行闪亮的应用程序,则会看到以下内容:
点击+ LV
按钮,然后只需选择LV1
,然后多选x1
x2
x3
,依此类推。
输出结果如下:
这是完整的代码
library(shiny)
newlist <- as.list(c("LV1","LV2","x1","x2","x3","x4","x5","x6"))
row_ui <- function(id) {
ns <- NS(id)
fluidRow(
column(2,uiOutput(ns("ui_placeholder"))),column(2,uiOutput(ns("ui_placeholder2"))
)
)
}
row_server <- function(input,output,session) {
return_value <- reactive({c(input$variable1,input$variable2)})
ns <- session$ns
output$ui_placeholder <- renderUI({
selectInput(ns("variable1"),"LV:",choices = c(' ',newlist),selected = NULL)
})
output$ui_placeholder2 <- renderUI({
selectInput(ns("variable2"),"Ind:",selected = NULL,multiple = TRUE)
})
list(return_value = return_value)
}
ui <- fluidPage(
div(id="placeholder"),actionButton("addLine","+ LV"),verbatimTextOutput("out"),verbatimTextOutput("listout5")
)
server <- function(input,session) {
handler <- reactiveVal(list())
observeEvent(input$addLine,{
new_id <- paste("row",input$addLine,sep = "_")
insertUI(
selector = "#placeholder",where = "beforeBegin",ui = row_ui(new_id)
)
handler_list <- isolate(handler())
new_handler <- callModule(row_server,new_id)
handler_list <- c(handler_list,new_handler)
names(handler_list)[length(handler_list)] <- new_id
handler(handler_list)
})
output$out <- renderPrint({
lapply(handler(),function(handle) {
handle()
})
})
}
shinyApp(ui,server)
解决方法
尝试一下:
output$out <- renderPrint({
invisible(lapply(handler(),function(handle) {
cat(paste0(paste0(handle(),collapse = " "),"\n"))
}))
})
handle()
返回一个列表,第一次调用paste0
创建一个字符串,第二次调用paste0
添加新行,以使来自一个模块的所有值都在同一行。 invisible
防止打印来自lapply
的返回值(仅打印来自cat
的副作用)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。