Rmarkdown: Why graphics inside loop skips pages?












1















I'm writing a report in Rmarkdown and I found some trouble plotting
ggplot2 graphics inside a for loop. The problem is that I'm getting a page skip between graphics in my PDF output.



My code works perfectly and I already tested a few alternatives:
I plotted two copies of some graphic outside any loop and the problem was solved. Also, printing some random message at the beggining and end of a iteration suggests that when the loop variable is incremented there is an automatic page break. Does anyone know how to solve this problem?



Thanks in advance



EDIT: ADDING CODE



library(dplyr)
library(ggwordcloud)

for(i in 1:7){
aux <- arrange(as.data.frame(extrai_palavras(dados[i, ], "count")), desc(Freq))
med <- mean(aux$Freq)
sd <- sd(aux$Freq)
aux <- aux %>% filter(Freq - med > 2 * sd)
ggwordcloud2(aux) %>% print()
}


I can't upload my data here, but it is a data frame with two colums, one the first is a word token and the second is the frequency of the word (Freq).



EDIT 2: REPRODUCIBLE EXAMPLE



Here is an reproducible example that makes absolutely no sense but produces my problem when a pdf is generated from it.



library(tidyverse)
treshold <- c(0, 10, 20, 30, 40)
for(i in 1:(length(treshold)-1)){
plot <- mtcars%>%
filter(mtcars$mpg > treshold[i] && mtcars$mpg < treshold[i+1]) %>%
ggplot(aes(x = disp, y = hp, fill = as.factor(cyl)))+
xlab("") +
ylab("Total de ocorr?ncias") +
ggtitle("Palavras mais frequentes") +
coord_flip() +
geom_point()
print(plot)
}









share|improve this question




















  • 1





    It's easier to help if you can post a reproducible example

    – Tung
    Jan 1 at 5:53






  • 1





    Hi Ariel your code works perfectly but without seeing your code or data, it's close to impossible for someone to help. Could you add sample data,code and expected/current output?

    – NelsonGon
    Jan 1 at 6:32











  • Note that we don't need your actual data, you can make up an example or use a built-in dataset.

    – Axeman
    Jan 1 at 6:52











  • Please check what the aux looks like by adding print(aux) at the end of your for loop. Maybe aux is empty or not the proper type for ggwordcloud2 after a series of operation.

    – Darren Tsai
    Jan 1 at 9:03











  • @Ariel: it's a good start but you need to post the .Rmd file as well to make it fully reproducible

    – Tung
    Jan 1 at 19:03
















1















I'm writing a report in Rmarkdown and I found some trouble plotting
ggplot2 graphics inside a for loop. The problem is that I'm getting a page skip between graphics in my PDF output.



My code works perfectly and I already tested a few alternatives:
I plotted two copies of some graphic outside any loop and the problem was solved. Also, printing some random message at the beggining and end of a iteration suggests that when the loop variable is incremented there is an automatic page break. Does anyone know how to solve this problem?



Thanks in advance



EDIT: ADDING CODE



library(dplyr)
library(ggwordcloud)

for(i in 1:7){
aux <- arrange(as.data.frame(extrai_palavras(dados[i, ], "count")), desc(Freq))
med <- mean(aux$Freq)
sd <- sd(aux$Freq)
aux <- aux %>% filter(Freq - med > 2 * sd)
ggwordcloud2(aux) %>% print()
}


I can't upload my data here, but it is a data frame with two colums, one the first is a word token and the second is the frequency of the word (Freq).



EDIT 2: REPRODUCIBLE EXAMPLE



Here is an reproducible example that makes absolutely no sense but produces my problem when a pdf is generated from it.



library(tidyverse)
treshold <- c(0, 10, 20, 30, 40)
for(i in 1:(length(treshold)-1)){
plot <- mtcars%>%
filter(mtcars$mpg > treshold[i] && mtcars$mpg < treshold[i+1]) %>%
ggplot(aes(x = disp, y = hp, fill = as.factor(cyl)))+
xlab("") +
ylab("Total de ocorr?ncias") +
ggtitle("Palavras mais frequentes") +
coord_flip() +
geom_point()
print(plot)
}









share|improve this question




















  • 1





    It's easier to help if you can post a reproducible example

    – Tung
    Jan 1 at 5:53






  • 1





    Hi Ariel your code works perfectly but without seeing your code or data, it's close to impossible for someone to help. Could you add sample data,code and expected/current output?

    – NelsonGon
    Jan 1 at 6:32











  • Note that we don't need your actual data, you can make up an example or use a built-in dataset.

    – Axeman
    Jan 1 at 6:52











  • Please check what the aux looks like by adding print(aux) at the end of your for loop. Maybe aux is empty or not the proper type for ggwordcloud2 after a series of operation.

    – Darren Tsai
    Jan 1 at 9:03











  • @Ariel: it's a good start but you need to post the .Rmd file as well to make it fully reproducible

    – Tung
    Jan 1 at 19:03














1












1








1








I'm writing a report in Rmarkdown and I found some trouble plotting
ggplot2 graphics inside a for loop. The problem is that I'm getting a page skip between graphics in my PDF output.



My code works perfectly and I already tested a few alternatives:
I plotted two copies of some graphic outside any loop and the problem was solved. Also, printing some random message at the beggining and end of a iteration suggests that when the loop variable is incremented there is an automatic page break. Does anyone know how to solve this problem?



Thanks in advance



EDIT: ADDING CODE



library(dplyr)
library(ggwordcloud)

for(i in 1:7){
aux <- arrange(as.data.frame(extrai_palavras(dados[i, ], "count")), desc(Freq))
med <- mean(aux$Freq)
sd <- sd(aux$Freq)
aux <- aux %>% filter(Freq - med > 2 * sd)
ggwordcloud2(aux) %>% print()
}


I can't upload my data here, but it is a data frame with two colums, one the first is a word token and the second is the frequency of the word (Freq).



EDIT 2: REPRODUCIBLE EXAMPLE



Here is an reproducible example that makes absolutely no sense but produces my problem when a pdf is generated from it.



library(tidyverse)
treshold <- c(0, 10, 20, 30, 40)
for(i in 1:(length(treshold)-1)){
plot <- mtcars%>%
filter(mtcars$mpg > treshold[i] && mtcars$mpg < treshold[i+1]) %>%
ggplot(aes(x = disp, y = hp, fill = as.factor(cyl)))+
xlab("") +
ylab("Total de ocorr?ncias") +
ggtitle("Palavras mais frequentes") +
coord_flip() +
geom_point()
print(plot)
}









share|improve this question
















I'm writing a report in Rmarkdown and I found some trouble plotting
ggplot2 graphics inside a for loop. The problem is that I'm getting a page skip between graphics in my PDF output.



My code works perfectly and I already tested a few alternatives:
I plotted two copies of some graphic outside any loop and the problem was solved. Also, printing some random message at the beggining and end of a iteration suggests that when the loop variable is incremented there is an automatic page break. Does anyone know how to solve this problem?



Thanks in advance



EDIT: ADDING CODE



library(dplyr)
library(ggwordcloud)

for(i in 1:7){
aux <- arrange(as.data.frame(extrai_palavras(dados[i, ], "count")), desc(Freq))
med <- mean(aux$Freq)
sd <- sd(aux$Freq)
aux <- aux %>% filter(Freq - med > 2 * sd)
ggwordcloud2(aux) %>% print()
}


I can't upload my data here, but it is a data frame with two colums, one the first is a word token and the second is the frequency of the word (Freq).



EDIT 2: REPRODUCIBLE EXAMPLE



Here is an reproducible example that makes absolutely no sense but produces my problem when a pdf is generated from it.



library(tidyverse)
treshold <- c(0, 10, 20, 30, 40)
for(i in 1:(length(treshold)-1)){
plot <- mtcars%>%
filter(mtcars$mpg > treshold[i] && mtcars$mpg < treshold[i+1]) %>%
ggplot(aes(x = disp, y = hp, fill = as.factor(cyl)))+
xlab("") +
ylab("Total de ocorr?ncias") +
ggtitle("Palavras mais frequentes") +
coord_flip() +
geom_point()
print(plot)
}






r for-loop ggplot2 markdown r-markdown






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:42









Waylan

12.3k32860




12.3k32860










asked Jan 1 at 5:16









Ariel SerranoniAriel Serranoni

63




63








  • 1





    It's easier to help if you can post a reproducible example

    – Tung
    Jan 1 at 5:53






  • 1





    Hi Ariel your code works perfectly but without seeing your code or data, it's close to impossible for someone to help. Could you add sample data,code and expected/current output?

    – NelsonGon
    Jan 1 at 6:32











  • Note that we don't need your actual data, you can make up an example or use a built-in dataset.

    – Axeman
    Jan 1 at 6:52











  • Please check what the aux looks like by adding print(aux) at the end of your for loop. Maybe aux is empty or not the proper type for ggwordcloud2 after a series of operation.

    – Darren Tsai
    Jan 1 at 9:03











  • @Ariel: it's a good start but you need to post the .Rmd file as well to make it fully reproducible

    – Tung
    Jan 1 at 19:03














  • 1





    It's easier to help if you can post a reproducible example

    – Tung
    Jan 1 at 5:53






  • 1





    Hi Ariel your code works perfectly but without seeing your code or data, it's close to impossible for someone to help. Could you add sample data,code and expected/current output?

    – NelsonGon
    Jan 1 at 6:32











  • Note that we don't need your actual data, you can make up an example or use a built-in dataset.

    – Axeman
    Jan 1 at 6:52











  • Please check what the aux looks like by adding print(aux) at the end of your for loop. Maybe aux is empty or not the proper type for ggwordcloud2 after a series of operation.

    – Darren Tsai
    Jan 1 at 9:03











  • @Ariel: it's a good start but you need to post the .Rmd file as well to make it fully reproducible

    – Tung
    Jan 1 at 19:03








1




1





It's easier to help if you can post a reproducible example

– Tung
Jan 1 at 5:53





It's easier to help if you can post a reproducible example

– Tung
Jan 1 at 5:53




1




1





Hi Ariel your code works perfectly but without seeing your code or data, it's close to impossible for someone to help. Could you add sample data,code and expected/current output?

– NelsonGon
Jan 1 at 6:32





Hi Ariel your code works perfectly but without seeing your code or data, it's close to impossible for someone to help. Could you add sample data,code and expected/current output?

– NelsonGon
Jan 1 at 6:32













Note that we don't need your actual data, you can make up an example or use a built-in dataset.

– Axeman
Jan 1 at 6:52





Note that we don't need your actual data, you can make up an example or use a built-in dataset.

– Axeman
Jan 1 at 6:52













Please check what the aux looks like by adding print(aux) at the end of your for loop. Maybe aux is empty or not the proper type for ggwordcloud2 after a series of operation.

– Darren Tsai
Jan 1 at 9:03





Please check what the aux looks like by adding print(aux) at the end of your for loop. Maybe aux is empty or not the proper type for ggwordcloud2 after a series of operation.

– Darren Tsai
Jan 1 at 9:03













@Ariel: it's a good start but you need to post the .Rmd file as well to make it fully reproducible

– Tung
Jan 1 at 19:03





@Ariel: it's a good start but you need to post the .Rmd file as well to make it fully reproducible

– Tung
Jan 1 at 19:03












0






active

oldest

votes











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%2f53993151%2frmarkdown-why-graphics-inside-loop-skips-pages%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53993151%2frmarkdown-why-graphics-inside-loop-skips-pages%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter