如何将 insertUI() 输出添加到 R Shiny 中的 renderText() 输出?
Posted
技术标签:
【中文标题】如何将 insertUI() 输出添加到 R Shiny 中的 renderText() 输出?【英文标题】:How do I add insertUI() output to renderText() ouput in R Shiny? 【发布时间】:2018-02-28 21:47:35 【问题描述】:我正在开发一个闪亮的应用程序来为系统开发提供自动化,我需要一种方法来为用户输入添加文本框,并将生成的用户输入添加到 renderText() 输出。我不确定是否有办法遍历反应值,所以现在我不得不对 textInput() 结果中的标签进行特定调用。代码如下,注意需要修改的部分是 server 部分底部的 output$value 部分:
library(shiny)
ui <- fluidPage(
fluidRow(
column(6,
textInput("mainDesc1", label = "Main Description", value = "", width = '100%')
),
column(1,
actionButton(inputId = 'insertBtn', label = "More")
),
column(1,
actionButton(inputId = 'removeBtn', label = "Less")
)
),
tags$div(id = 'placeholder'),
fluidRow(column(12, verbatimTextOutput("value")))
)
server <- function(input, output)
## keep track of elements inserted and not yet removed
inserted <- c()
#btn <- 1
observeEvent(input$insertBtn,
btn <- input$insertBtn + 1
id <- paste0("mainDesc", btn)
insertUI(
selector = '#placeholder',
## wrap element in a div with id for ease of removal
ui = tags$div(
tags$p(fluidRow(
column(6,
textInput(paste("mainDesc", btn + 1, sep = ""), label = "", value = "", width = '100%')
)
)
),
id = id
)
)
inserted <<- c(id, inserted)
)
observeEvent(input$removeBtn,
removeUI(
## pass in appropriate div id
selector = paste0('#', inserted[length(inserted)])
)
inserted <<- inserted[-length(inserted)]
)
output$value <- renderText( noquote(paste(input[[paste("mainDesc", 1, sep = "")]], "\n",
input[[paste("mainDesc", btn + 1, sep = "")]], sep = ""))
)
shinyApp(ui = ui, server = server)
如何循环遍历由于以下原因而弹出的 userInput() 框的结果
textInput(paste("mainDesc", btn + 1, sep = ""), label = "", value = "", width = '100%')
逐行粘贴上述所有现有输出?本质上,我正在寻找的是如果用户在第一个“主要描述”文本框中输入“第一行”,单击“更多”按钮在弹出的文本框中添加“第二行”,再次单击更多并在 renderText() 框输出的弹出文本框中输入“第 3 行”:
1st line
2nd line
3rd line
如果添加了更多文本框,则应将用户输入逐行添加到 renderText() 输出中。谢谢!
【问题讨论】:
【参考方案1】:试着把btn
变成reactiveValue
:
vals <- reactiveValues(btn = 1)
# reference elsewhere in your app as vals$btn
在这里查看似乎有效的代码:
library(shiny)
ui <- fluidPage(
fluidRow(
column(6,
textInput("mainDesc1", label = "Main Description", value = "", width = '100%')
),
column(1,
actionButton(inputId = 'insertBtn', label = "More")
),
column(1,
actionButton(inputId = 'removeBtn', label = "Less")
)
),
tags$div(id = 'placeholder'),
fluidRow(column(12, verbatimTextOutput("value", placeholder = T)))
)
server <- function(input, output)
## keep track of elements inserted and not yet removed
vals <- reactiveValues(btn = 0)
observeEvent(input$insertBtn,
vals$btn <- vals$btn + 1
insertUI(
selector = '#placeholder',
## wrap element in a div with id for ease of removal
ui = tags$div(
id = paste0('line', vals$btn),
tags$p(fluidRow(
column(6,
textInput(paste("mainDesc", vals$btn + 1, sep = ""), label = paste("Line", vals$btn), value = "", width = '100%')
)
)
)
)
)
)
observeEvent(input$removeBtn,
removeUI(
## pass in appropriate div id
selector = paste0('#line', vals$btn)
)
vals$btn <- vals$btn - 1
)
output$value <- renderText(
msg <- c(input[["mainDesc1"]])
if (vals$btn > 0)
for (i in 1:vals$btn)
msg <- c(msg, input[[paste0("mainDesc", i + 1)]])
msg <- paste(msg, collapse = "\n")
)
shinyApp(ui = ui, server = server)
【讨论】:
以上是关于如何将 insertUI() 输出添加到 R Shiny 中的 renderText() 输出?的主要内容,如果未能解决你的问题,请参考以下文章
在 R 闪亮的模块中使用 actionButton + insertUI 创建多个输入