RShiny:如何通过fileInput用文件内容更新textAreaInput?

RShiny:如何通过fileInput用文件内容更新textAreaInput?,r,shiny,R,Shiny,我正在构建一个闪亮的应用程序,它将使用1)文本区域字段中提供的默认内容来形成将由应用程序执行的查询,或2)允许用户从文件中的文本上传查询,然后使用上传的内容(现在位于文本区域)作为要执行的查询。除了我无法将文本文件加载到TextArea输入字段之外,所有工作都正常。我已尝试使用updateTextAreaInput。几乎没有例子,我也没有成功 下面是一些成功允许用户选择文本文件的示例代码。选择后,文本文件的内容将显示在应用程序的“调试”部分。如何使用updateTextAreaInput或其他方

我正在构建一个闪亮的应用程序,它将使用1)文本区域字段中提供的默认内容来形成将由应用程序执行的查询,或2)允许用户从文件中的文本上传查询,然后使用上传的内容(现在位于文本区域)作为要执行的查询。除了我无法将文本文件加载到TextArea输入字段之外,所有工作都正常。我已尝试使用updateTextAreaInput。几乎没有例子,我也没有成功

下面是一些成功允许用户选择文本文件的示例代码。选择后,文本文件的内容将显示在应用程序的“调试”部分。如何使用updateTextAreaInput或其他方式将此内容输入到textAreaInput(或其他可编辑文本字段,如textArea)中?注意:出于测试目的,我使用的是output$text而不是output$query

非常感谢您的建议,工作代码示例更是如此

library(shiny)

ui <- fluidPage(
    titlePanel("Load Text File into textAreaInput"),
    wellPanel(
        column(12, fileInput('fileRQ',   'Load Text File')),
        fluidRow(
            textAreaInput(inputId="query", "Text Content",rows=12, width='90%', 
         "# Default/example text. To be replaced by content of a file.")
        )
    ),
    fluidRow(
       tags$hr(),
       tags$h3("Debug"),
       verbatimTextOutput("text")    
    )
)

server <- function(input, output) {
    fileText <- eventReactive(input$fileRQ, {
        filePath <- input$fileRQ$datapath
        fileText <- paste(readLines(filePath), collapse = "\n")
        fileText
    })
    output$text <- fileText
}
shinyApp(ui = ui, server = server)
库(闪亮)

ui你是如此接近。上传文件时只需更新textAreaInput:)注意,如果您不需要文本,则可以使用
oberveEvent
而不是
eventReactive

library(shiny)

ui <- fluidPage(
  titlePanel("Load Text File into textAreaInput"),
  wellPanel(
    column(12, fileInput('fileRQ',   'Load Text File')),
    fluidRow(
      textAreaInput(inputId="query", "Text Content",rows=12, width='90%', 
                    "# Default/example text. To be replaced by content of a file.")
    )
  ),
  fluidRow(
    tags$hr(),
    tags$h3("Debug"),
    verbatimTextOutput("text")    
  )
)

server <- function(input, output, session) {

  # fileText() contains the recent file text
  fileText <- eventReactive(input$fileRQ, {
    filePath <- input$fileRQ$datapath
    fileText <- paste(readLines(filePath), collapse = "\n")

    # update text area with file content
    updateTextAreaInput(session, "query", value = fileText)

    # return the text to be displayed in text Outputs
    return(fileText)
  })

  output$text <- renderPrint({ fileText() })    
}
shinyApp(ui = ui, server = server)
库(闪亮)

ui你是如此接近。上传文件时只需更新textAreaInput:)注意,如果您不需要文本,则可以使用
oberveEvent
而不是
eventReactive

library(shiny)

ui <- fluidPage(
  titlePanel("Load Text File into textAreaInput"),
  wellPanel(
    column(12, fileInput('fileRQ',   'Load Text File')),
    fluidRow(
      textAreaInput(inputId="query", "Text Content",rows=12, width='90%', 
                    "# Default/example text. To be replaced by content of a file.")
    )
  ),
  fluidRow(
    tags$hr(),
    tags$h3("Debug"),
    verbatimTextOutput("text")    
  )
)

server <- function(input, output, session) {

  # fileText() contains the recent file text
  fileText <- eventReactive(input$fileRQ, {
    filePath <- input$fileRQ$datapath
    fileText <- paste(readLines(filePath), collapse = "\n")

    # update text area with file content
    updateTextAreaInput(session, "query", value = fileText)

    # return the text to be displayed in text Outputs
    return(fileText)
  })

  output$text <- renderPrint({ fileText() })    
}
shinyApp(ui = ui, server = server)
库(闪亮)

太棒了!这很有魅力。文本将作为查询字符串传递,因此我将在(非精简)应用程序中测试该文本,并从那里开始。非常感谢。我现在可以在上面做很多不同的东西。太棒了!这很有魅力。文本将作为查询字符串传递,因此我将在(非精简)应用程序中测试该文本,并从那里开始。非常感谢。我现在可以在上面构建很多不同的东西。