Dynamically adding column names in R Shiny

Dynamically adding column names in R Shiny



Quite New to R here and not an experienced coder. I want to create a simple function to upload an external file via R shiny, but the file will not have fixed number of columns and may or may not have a column name.



In the case where the raw file does not have a header, I wish to force the name of the first column to be "Date" and the remaining columns 2,3,4...,n to be "Investment 1","Investment 2","Investment 3",...,"Investment n-1" respectively



Here's my current code, server side,


server <- function(input, output) {

rawdata <- reactive(
file_to_read = input$file
if(is.null(file_to_read))
return()

data <- read.table(file_to_read$datapath, sep = input$sep, header =
input$dataheader)
)


addcolumn <- reactive(
if(input$dataheader = FALSE)
paste("Date",colnames(rawdata()[,1]))
for (i in 2:ncol(rawdata()))
paste("Investment " + i, colnames(rawdata()[,i]))


)

output$datatable <- renderTable(
If(input$dataheader = FALSE)
addcolumn(rawdata())
else
rawdata()

)



and ui side,


dashboardBody(
tabItems(
tabItem(
tabName = "import",
fluidRow(
box(
title = "Instructions",
solidHeader = TRUE,
width = 12,
status = "warning",
height = 120,
textOutput("instructionsImport")
),
box(
solidHeader = FALSE,
width = 3,
status = "primary",
fileInput("file","Choose a file to upload"),
radioButtons("sep","Separator",choices = c(Comma = ",", Space = " ",Period = ".", Tilde = "~", minus = "-")),
checkboxInput("dataheader","File has header?")
),
box(
title = "Uploaded Data",
solidHeader = TRUE,
width = 9,
status = "primary",
tableOutput("datatable")
)
)
),



Ideally, I would like the operation of adding column name to be done once after import and the resulting data table created instead of making a reactive function for adding column.



Thanks





Hi, welcome to SO! Your question is legit, but I would suggest to focus on reading and renaming part, shiny has nothing to do with that. You might want to restructure your question, remove shiny part, rename and add examples data (file w/wo header)
– PoGibas
Aug 27 at 7:16


shiny





Hi, thanks so much for the tips! It's my first time asking on SO, but I'll be more clear and succinct next time!
– ThirawatT
Aug 28 at 8:13




1 Answer
1



Just change your server.R code to that :


server.R


server <- function(input, output)

rawdata <- reactive(
file_to_read = input$file
if(is.null(file_to_read))
return()

data <- read.table(file_to_read$datapath, sep = input$sep, header =
input$dataheader)
if(!input$dataheader)
colnames(data)<-c("Date",paste("Investment",1:(ncol(data)-1)))

return(data)
)


output$datatable <- renderTable(
rawdata()
)





Thanks! Works like a charm
– ThirawatT
Aug 28 at 8:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Malaysia to Papua New Guinea by motorcycle?

List of Bhojpuri films

PHP code is not being executed, instead code shows on the page