简体   繁体   中英

r shiny: create column headers with write.table in text file

I am writing a simple shiny app, where I use three numericInput s. The user inputs three numbers, and in the server side I use this input to write a .txt file using write.table . In the .txt file I want to create column headers similar to the label of every input, so the first number to be in a column with header 1st Number etc. I thought it was gonna simple, but I am keep not getting the expected result.

My code:

write.table(paste(c(input$firstNum, input$secondNum, input$thirdNum), collapse = " | "),
            filename, quote = FALSE, row.names = TRUE,
            col.names = c("1st Number", "2nd Number", "3rd Number"))

Any suggestions?

Since you are providing a single scalar ( 1|3|5 ) to write.table , it believes that you are writing a single-column table. This is obviously not what you think you are doing. The benefit of using write.table is that you give it a table-like structure ( matrix or data.frame , typically) and it'll put in the sep arator.

write.table(matrix(c(input$firstNum, input$secondNum, input$thirdNum), nr=1), 
            filename, sep='|', quote = FALSE, row.names = TRUE,
            col.names = c("1st Number", "2nd Number", "3rd Number"))

where the changes are:

  • provide a matrix instead of a pre-compacted string, and
  • add the sep='|' argument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM