2017-06-15 28 views
0

我正在構建一個Shiny應用程序,該應用程序將使用1)在文本區域字段中提供的默認內容,以形成將由應用程序執行的查詢或2)允許用戶上傳來自文件的文本的查詢,然後使用該上傳的內容(現在在textArea中)作爲要執行的查詢。除了我無法將文本文件加載到textAreaInput字段之外,所有的部分都正在工作。我已經嘗試使用updateTextAreaInput。很少有例子存在,我一直沒有成功。RShiny:如何通過fileInput更新文件內容的textAreaInput?

以下是一些成功允許用戶選擇文本文件的示例代碼。選擇後,文本文件的內容顯示在應用程序的「調試」部分。如何使用updateTextAreaInput或其他方式將此內容放入textAreaInput(或其他可編輯的文本字段,如textArea)中?注意我正在使用輸出$ text而不是輸出$ 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) 

回答

1

你太親近了。當文件上傳時只需更新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) 

enter image description here

+0

真棒!這就像一個魅力。該文本將作爲查詢字符串傳遞,因此我將在(未剝離)應用程序中測試下一個並從那裏開始。非常感謝。現在我可以在此基礎上構建很多不同的東西。 – Tim

相關問題