收集整个闪亮的应用程序中的所有用户输入 [英] Collect All user inputs throughout the Shiny App
本文介绍了收集整个闪亮的应用程序中的所有用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在UI中的其他位置显示textInput()
中的值,而不必经过server.R
,内容非常详细,如下所示?
ui.R
library(shiny)
shinyUI(
fluidPage(
textInput('text_in', label = 'Write text here'),
# elsewhere in the UI...
textOutput('text_out')
))
server.R
library(shiny)
shinyServer(function(input, output) {
output$text_out = renderText(input$text_in)
})
对于这个示例来说还不错,但是当我需要多次执行时,它会变得非常冗长。我的愿望是收集用户在整个应用程序中输入的所有内容,并在最后将它们编译成一个漂亮的表格,这样他们就可以确认所有内容都布局正确。
我已经看到,在conditionalPanel()
中使用JavaScript表达式时,您可以引用输入元素,而无需通过服务器,但我不确定如何在此特定实例的外部实现该元素。
推荐答案
要访问所有输入,可以使用reactiveValuesToList
服务器端。您可以通过如下所示的Javascript事件访问输入值(我以@Pork Chop为例):
library(shiny)
ui <- basicPage(
fluidRow(
column(
width = 6,
textInput('a', 'Text A',"a1"),
textInput('b', 'Text B',"b1"),
textInput('c', 'Text A',"c1"),
textInput('d', 'Text B',"d1"),
textInput('e', 'Text A',"e1"),
textInput('f', 'Text B',"f1")
),
column(
width = 6,
tags$p("Text A :", tags$span(id = "valueA", "")),
tags$script(
"$(document).on('shiny:inputchanged', function(event) {
if (event.name === 'a') {
$('#valueA').text(event.value);
}
});
"
),
tableOutput('show_inputs')
)
)
)
server <- shinyServer(function(input, output, session){
AllInputs <- reactive({
x <- reactiveValuesToList(input)
data.frame(
names = names(x),
values = unlist(x, use.names = FALSE)
)
})
output$show_inputs <- renderTable({
AllInputs()
})
})
shinyApp(ui = ui, server = server)
这篇关于收集整个闪亮的应用程序中的所有用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文