r - 收集整个 Shiny 应用程序中的所有用户输入

标签 r shiny

有没有办法显示来自 textInput() 的值? UI 中的其他地方而无需通过 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)

关于r - 收集整个 Shiny 应用程序中的所有用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41031584/

相关文章:

R Shiny : How to loop output plotly graphs

r 为 modebarButtons 设置全局选项

r - 从 Windows 机器使用 R 的 SSH/SCP

html - R:使用 rvest 抓取动态链接

R数据表: How to "label" consecutive values in a column?

R Leaflet PopupGraph - 在map_marker_click上添加PopupGraphs

R Shiny : refreshing/overriding actionButton() output

r - 如何在 R shiny 中动态绘制多个绘图网格

r - 在 R data.table 计算中使用上一行的值

javascript - Shiny 的自定义输出