Dynamically set colnames from character string in flextable












4















The flextable help details describes adding new headers as manually typing each existing header name against each new header name, like this:



library(flextable)
ft_1 <- regulartable(head(iris))
ft_1 <- set_header_labels(ft_1, Sepal.Length = "SL",
Sepal.Width = "SW", Petal.Length = "PL",
Petal.Width = "PW"
)
ft_1


How can I just add all new header names from a character string such as



(names2<-c('SL','SW','PL','PW','SPECIES'))
[1] "SL" "SW" "PL" "PW" "SPECIES"


?



So far I have managed a very hacky solution:



names(names2)<-names(ft_1$header$dataset[1,])

ft_1$header$dataset[1,]<-names2









share|improve this question

























  • What about changing the colnames in the dataset before converting it to a flextable?

    – RAB
    Nov 20 '18 at 5:17






  • 1





    @user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.

    – J.Con
    Nov 20 '18 at 5:19











  • Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :P

    – RAB
    Nov 20 '18 at 5:31











  • @user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.

    – J.Con
    Nov 20 '18 at 5:34






  • 1





    :) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.

    – David Gohel
    Nov 20 '18 at 8:04
















4















The flextable help details describes adding new headers as manually typing each existing header name against each new header name, like this:



library(flextable)
ft_1 <- regulartable(head(iris))
ft_1 <- set_header_labels(ft_1, Sepal.Length = "SL",
Sepal.Width = "SW", Petal.Length = "PL",
Petal.Width = "PW"
)
ft_1


How can I just add all new header names from a character string such as



(names2<-c('SL','SW','PL','PW','SPECIES'))
[1] "SL" "SW" "PL" "PW" "SPECIES"


?



So far I have managed a very hacky solution:



names(names2)<-names(ft_1$header$dataset[1,])

ft_1$header$dataset[1,]<-names2









share|improve this question

























  • What about changing the colnames in the dataset before converting it to a flextable?

    – RAB
    Nov 20 '18 at 5:17






  • 1





    @user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.

    – J.Con
    Nov 20 '18 at 5:19











  • Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :P

    – RAB
    Nov 20 '18 at 5:31











  • @user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.

    – J.Con
    Nov 20 '18 at 5:34






  • 1





    :) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.

    – David Gohel
    Nov 20 '18 at 8:04














4












4








4








The flextable help details describes adding new headers as manually typing each existing header name against each new header name, like this:



library(flextable)
ft_1 <- regulartable(head(iris))
ft_1 <- set_header_labels(ft_1, Sepal.Length = "SL",
Sepal.Width = "SW", Petal.Length = "PL",
Petal.Width = "PW"
)
ft_1


How can I just add all new header names from a character string such as



(names2<-c('SL','SW','PL','PW','SPECIES'))
[1] "SL" "SW" "PL" "PW" "SPECIES"


?



So far I have managed a very hacky solution:



names(names2)<-names(ft_1$header$dataset[1,])

ft_1$header$dataset[1,]<-names2









share|improve this question
















The flextable help details describes adding new headers as manually typing each existing header name against each new header name, like this:



library(flextable)
ft_1 <- regulartable(head(iris))
ft_1 <- set_header_labels(ft_1, Sepal.Length = "SL",
Sepal.Width = "SW", Petal.Length = "PL",
Petal.Width = "PW"
)
ft_1


How can I just add all new header names from a character string such as



(names2<-c('SL','SW','PL','PW','SPECIES'))
[1] "SL" "SW" "PL" "PW" "SPECIES"


?



So far I have managed a very hacky solution:



names(names2)<-names(ft_1$header$dataset[1,])

ft_1$header$dataset[1,]<-names2






r flextable officer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 8:04









David Gohel

2,6152615




2,6152615










asked Nov 20 '18 at 3:54









J.ConJ.Con

1,83111228




1,83111228













  • What about changing the colnames in the dataset before converting it to a flextable?

    – RAB
    Nov 20 '18 at 5:17






  • 1





    @user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.

    – J.Con
    Nov 20 '18 at 5:19











  • Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :P

    – RAB
    Nov 20 '18 at 5:31











  • @user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.

    – J.Con
    Nov 20 '18 at 5:34






  • 1





    :) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.

    – David Gohel
    Nov 20 '18 at 8:04



















  • What about changing the colnames in the dataset before converting it to a flextable?

    – RAB
    Nov 20 '18 at 5:17






  • 1





    @user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.

    – J.Con
    Nov 20 '18 at 5:19











  • Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :P

    – RAB
    Nov 20 '18 at 5:31











  • @user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.

    – J.Con
    Nov 20 '18 at 5:34






  • 1





    :) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.

    – David Gohel
    Nov 20 '18 at 8:04

















What about changing the colnames in the dataset before converting it to a flextable?

– RAB
Nov 20 '18 at 5:17





What about changing the colnames in the dataset before converting it to a flextable?

– RAB
Nov 20 '18 at 5:17




1




1





@user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.

– J.Con
Nov 20 '18 at 5:19





@user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.

– J.Con
Nov 20 '18 at 5:19













Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :P

– RAB
Nov 20 '18 at 5:31





Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :P

– RAB
Nov 20 '18 at 5:31













@user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.

– J.Con
Nov 20 '18 at 5:34





@user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.

– J.Con
Nov 20 '18 at 5:34




1




1





:) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.

– David Gohel
Nov 20 '18 at 8:04





:) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.

– David Gohel
Nov 20 '18 at 8:04












2 Answers
2






active

oldest

votes


















3














set_header_labels is made for minor modifications of header values. You can use set_header_df if you want to map df column names with a set of one or more new header rows: https://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-table



library(flextable)

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ) )
ft <- set_header_df(x = ft, mapping = data.frame(keys = names1, values = names2, stringsAsFactors = FALSE),
key = "keys" )

# the following call is needed as header formats have been
# replaced by vanilla formats when set_header_df() has been called
ft <- theme_booktabs(ft)
ft


enter image description here






share|improve this answer
























  • Thanks for a prompt answer and a great package!

    – J.Con
    Nov 20 '18 at 21:41



















1














OK, after a couple edits I hope this works for you.



Your method seems plenty fine as well.



names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ),
col_keys = names1 )

oldHeaders <- names1
newHeaders <- names2

headerlist <- setNames(as.list(newHeaders),
oldHeaders)

ft <- do.call(set_header_labels, c(list(x = ft, top = F), headerlist))
ft





share|improve this answer


























  • Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

    – J.Con
    Nov 20 '18 at 21:42











  • No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

    – hmhensen
    Nov 20 '18 at 23:04






  • 1





    This answer helped me a lot. I recommend using the tidyverse package with flextable.

    – J.Con
    Nov 20 '18 at 23:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385979%2fdynamically-set-colnames-from-character-string-in-flextable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














set_header_labels is made for minor modifications of header values. You can use set_header_df if you want to map df column names with a set of one or more new header rows: https://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-table



library(flextable)

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ) )
ft <- set_header_df(x = ft, mapping = data.frame(keys = names1, values = names2, stringsAsFactors = FALSE),
key = "keys" )

# the following call is needed as header formats have been
# replaced by vanilla formats when set_header_df() has been called
ft <- theme_booktabs(ft)
ft


enter image description here






share|improve this answer
























  • Thanks for a prompt answer and a great package!

    – J.Con
    Nov 20 '18 at 21:41
















3














set_header_labels is made for minor modifications of header values. You can use set_header_df if you want to map df column names with a set of one or more new header rows: https://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-table



library(flextable)

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ) )
ft <- set_header_df(x = ft, mapping = data.frame(keys = names1, values = names2, stringsAsFactors = FALSE),
key = "keys" )

# the following call is needed as header formats have been
# replaced by vanilla formats when set_header_df() has been called
ft <- theme_booktabs(ft)
ft


enter image description here






share|improve this answer
























  • Thanks for a prompt answer and a great package!

    – J.Con
    Nov 20 '18 at 21:41














3












3








3







set_header_labels is made for minor modifications of header values. You can use set_header_df if you want to map df column names with a set of one or more new header rows: https://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-table



library(flextable)

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ) )
ft <- set_header_df(x = ft, mapping = data.frame(keys = names1, values = names2, stringsAsFactors = FALSE),
key = "keys" )

# the following call is needed as header formats have been
# replaced by vanilla formats when set_header_df() has been called
ft <- theme_booktabs(ft)
ft


enter image description here






share|improve this answer













set_header_labels is made for minor modifications of header values. You can use set_header_df if you want to map df column names with a set of one or more new header rows: https://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-table



library(flextable)

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ) )
ft <- set_header_df(x = ft, mapping = data.frame(keys = names1, values = names2, stringsAsFactors = FALSE),
key = "keys" )

# the following call is needed as header formats have been
# replaced by vanilla formats when set_header_df() has been called
ft <- theme_booktabs(ft)
ft


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 8:02









David GohelDavid Gohel

2,6152615




2,6152615













  • Thanks for a prompt answer and a great package!

    – J.Con
    Nov 20 '18 at 21:41



















  • Thanks for a prompt answer and a great package!

    – J.Con
    Nov 20 '18 at 21:41

















Thanks for a prompt answer and a great package!

– J.Con
Nov 20 '18 at 21:41





Thanks for a prompt answer and a great package!

– J.Con
Nov 20 '18 at 21:41













1














OK, after a couple edits I hope this works for you.



Your method seems plenty fine as well.



names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ),
col_keys = names1 )

oldHeaders <- names1
newHeaders <- names2

headerlist <- setNames(as.list(newHeaders),
oldHeaders)

ft <- do.call(set_header_labels, c(list(x = ft, top = F), headerlist))
ft





share|improve this answer


























  • Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

    – J.Con
    Nov 20 '18 at 21:42











  • No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

    – hmhensen
    Nov 20 '18 at 23:04






  • 1





    This answer helped me a lot. I recommend using the tidyverse package with flextable.

    – J.Con
    Nov 20 '18 at 23:08
















1














OK, after a couple edits I hope this works for you.



Your method seems plenty fine as well.



names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ),
col_keys = names1 )

oldHeaders <- names1
newHeaders <- names2

headerlist <- setNames(as.list(newHeaders),
oldHeaders)

ft <- do.call(set_header_labels, c(list(x = ft, top = F), headerlist))
ft





share|improve this answer


























  • Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

    – J.Con
    Nov 20 '18 at 21:42











  • No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

    – hmhensen
    Nov 20 '18 at 23:04






  • 1





    This answer helped me a lot. I recommend using the tidyverse package with flextable.

    – J.Con
    Nov 20 '18 at 23:08














1












1








1







OK, after a couple edits I hope this works for you.



Your method seems plenty fine as well.



names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ),
col_keys = names1 )

oldHeaders <- names1
newHeaders <- names2

headerlist <- setNames(as.list(newHeaders),
oldHeaders)

ft <- do.call(set_header_labels, c(list(x = ft, top = F), headerlist))
ft





share|improve this answer















OK, after a couple edits I hope this works for you.



Your method seems plenty fine as well.



names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ),
col_keys = names1 )

oldHeaders <- names1
newHeaders <- names2

headerlist <- setNames(as.list(newHeaders),
oldHeaders)

ft <- do.call(set_header_labels, c(list(x = ft, top = F), headerlist))
ft






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 7:04

























answered Nov 20 '18 at 6:35









hmhensenhmhensen

606416




606416













  • Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

    – J.Con
    Nov 20 '18 at 21:42











  • No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

    – hmhensen
    Nov 20 '18 at 23:04






  • 1





    This answer helped me a lot. I recommend using the tidyverse package with flextable.

    – J.Con
    Nov 20 '18 at 23:08



















  • Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

    – J.Con
    Nov 20 '18 at 21:42











  • No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

    – hmhensen
    Nov 20 '18 at 23:04






  • 1





    This answer helped me a lot. I recommend using the tidyverse package with flextable.

    – J.Con
    Nov 20 '18 at 23:08

















Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

– J.Con
Nov 20 '18 at 21:42





Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.

– J.Con
Nov 20 '18 at 21:42













No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

– hmhensen
Nov 20 '18 at 23:04





No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.

– hmhensen
Nov 20 '18 at 23:04




1




1





This answer helped me a lot. I recommend using the tidyverse package with flextable.

– J.Con
Nov 20 '18 at 23:08





This answer helped me a lot. I recommend using the tidyverse package with flextable.

– J.Con
Nov 20 '18 at 23:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385979%2fdynamically-set-colnames-from-character-string-in-flextable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$