How to automate the creation of histograms in R, saving the result in a list of hist() objects?












0















I have a code to process data, and I need to save some histograms that I will use in a Shiny application.



What I need is basically automate the creation of histograms, saving the result in a list of hist() objects, that I will save in an RDS file and then simply call in my Shiny app.



The code below shows the output that I want, but the variables names are hard coded, and it's not viable for me.



# Create data for this example 
list_dfs <- list(
dfA = data.frame(Var1 = rnorm(100), Date1 = rep(1:50, 2), Var2 = rnorm(100)*.55),
dfB = data.frame(Var3 = rnorm(100), Date2 = rep(1:50, 2), Var4 = rnorm(100)*.55),
dfC = data.frame(Var5 = rnorm(100), Date3 = rep(1:50, 2), Var6 = rnorm(100)*.55)
)

# Part that I want to automate
list_plots <- list(
dfA = NULL,
dfB = NULL,
dfC = NULL
)

list_plots$dfA <- lapply(list_dfs[[1]], function(x){hist(x, plot = FALSE)})
list_plots$dfB <- lapply(list_dfs[[2]], function(x){hist(x, plot = FALSE)})
list_plots$dfC <- lapply(list_dfs[[3]], function(x){hist(x, plot = FALSE)})

# Desired output - Histograms saved in a list
list_plots$dfA$Var1 %>% plot
list_plots$dfA$Date1 %>% plot
list_plots$dfA$Var2 %>% plot

list_plots$dfB$Var3 %>% plot
list_plots$dfB$Date2 %>% plot
list_plots$dfB$Var4 %>% plot

list_plots$dfC$Var5 %>% plot
list_plots$dfC$Date3 %>% plot
list_plots$dfC$Var6 %>% plot


Thanks in advance.



Wlademir.










share|improve this question





























    0















    I have a code to process data, and I need to save some histograms that I will use in a Shiny application.



    What I need is basically automate the creation of histograms, saving the result in a list of hist() objects, that I will save in an RDS file and then simply call in my Shiny app.



    The code below shows the output that I want, but the variables names are hard coded, and it's not viable for me.



    # Create data for this example 
    list_dfs <- list(
    dfA = data.frame(Var1 = rnorm(100), Date1 = rep(1:50, 2), Var2 = rnorm(100)*.55),
    dfB = data.frame(Var3 = rnorm(100), Date2 = rep(1:50, 2), Var4 = rnorm(100)*.55),
    dfC = data.frame(Var5 = rnorm(100), Date3 = rep(1:50, 2), Var6 = rnorm(100)*.55)
    )

    # Part that I want to automate
    list_plots <- list(
    dfA = NULL,
    dfB = NULL,
    dfC = NULL
    )

    list_plots$dfA <- lapply(list_dfs[[1]], function(x){hist(x, plot = FALSE)})
    list_plots$dfB <- lapply(list_dfs[[2]], function(x){hist(x, plot = FALSE)})
    list_plots$dfC <- lapply(list_dfs[[3]], function(x){hist(x, plot = FALSE)})

    # Desired output - Histograms saved in a list
    list_plots$dfA$Var1 %>% plot
    list_plots$dfA$Date1 %>% plot
    list_plots$dfA$Var2 %>% plot

    list_plots$dfB$Var3 %>% plot
    list_plots$dfB$Date2 %>% plot
    list_plots$dfB$Var4 %>% plot

    list_plots$dfC$Var5 %>% plot
    list_plots$dfC$Date3 %>% plot
    list_plots$dfC$Var6 %>% plot


    Thanks in advance.



    Wlademir.










    share|improve this question



























      0












      0








      0








      I have a code to process data, and I need to save some histograms that I will use in a Shiny application.



      What I need is basically automate the creation of histograms, saving the result in a list of hist() objects, that I will save in an RDS file and then simply call in my Shiny app.



      The code below shows the output that I want, but the variables names are hard coded, and it's not viable for me.



      # Create data for this example 
      list_dfs <- list(
      dfA = data.frame(Var1 = rnorm(100), Date1 = rep(1:50, 2), Var2 = rnorm(100)*.55),
      dfB = data.frame(Var3 = rnorm(100), Date2 = rep(1:50, 2), Var4 = rnorm(100)*.55),
      dfC = data.frame(Var5 = rnorm(100), Date3 = rep(1:50, 2), Var6 = rnorm(100)*.55)
      )

      # Part that I want to automate
      list_plots <- list(
      dfA = NULL,
      dfB = NULL,
      dfC = NULL
      )

      list_plots$dfA <- lapply(list_dfs[[1]], function(x){hist(x, plot = FALSE)})
      list_plots$dfB <- lapply(list_dfs[[2]], function(x){hist(x, plot = FALSE)})
      list_plots$dfC <- lapply(list_dfs[[3]], function(x){hist(x, plot = FALSE)})

      # Desired output - Histograms saved in a list
      list_plots$dfA$Var1 %>% plot
      list_plots$dfA$Date1 %>% plot
      list_plots$dfA$Var2 %>% plot

      list_plots$dfB$Var3 %>% plot
      list_plots$dfB$Date2 %>% plot
      list_plots$dfB$Var4 %>% plot

      list_plots$dfC$Var5 %>% plot
      list_plots$dfC$Date3 %>% plot
      list_plots$dfC$Var6 %>% plot


      Thanks in advance.



      Wlademir.










      share|improve this question
















      I have a code to process data, and I need to save some histograms that I will use in a Shiny application.



      What I need is basically automate the creation of histograms, saving the result in a list of hist() objects, that I will save in an RDS file and then simply call in my Shiny app.



      The code below shows the output that I want, but the variables names are hard coded, and it's not viable for me.



      # Create data for this example 
      list_dfs <- list(
      dfA = data.frame(Var1 = rnorm(100), Date1 = rep(1:50, 2), Var2 = rnorm(100)*.55),
      dfB = data.frame(Var3 = rnorm(100), Date2 = rep(1:50, 2), Var4 = rnorm(100)*.55),
      dfC = data.frame(Var5 = rnorm(100), Date3 = rep(1:50, 2), Var6 = rnorm(100)*.55)
      )

      # Part that I want to automate
      list_plots <- list(
      dfA = NULL,
      dfB = NULL,
      dfC = NULL
      )

      list_plots$dfA <- lapply(list_dfs[[1]], function(x){hist(x, plot = FALSE)})
      list_plots$dfB <- lapply(list_dfs[[2]], function(x){hist(x, plot = FALSE)})
      list_plots$dfC <- lapply(list_dfs[[3]], function(x){hist(x, plot = FALSE)})

      # Desired output - Histograms saved in a list
      list_plots$dfA$Var1 %>% plot
      list_plots$dfA$Date1 %>% plot
      list_plots$dfA$Var2 %>% plot

      list_plots$dfB$Var3 %>% plot
      list_plots$dfB$Date2 %>% plot
      list_plots$dfB$Var4 %>% plot

      list_plots$dfC$Var5 %>% plot
      list_plots$dfC$Date3 %>% plot
      list_plots$dfC$Var6 %>% plot


      Thanks in advance.



      Wlademir.







      r shiny dplyr histogram tibble






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 16:19







      Wlademir Ribeiro Prates

















      asked Nov 22 '18 at 16:04









      Wlademir Ribeiro PratesWlademir Ribeiro Prates

      16912




      16912
























          1 Answer
          1






          active

          oldest

          votes


















          1














          res <- lapply(list_dfs, function(z){
          ll <- lapply(z, function(x){
          hist(x, plot = FALSE)
          })
          names(ll) <- names(z)
          return(ll)
          })
          names(res) <- names(list_dfs)


          You may wan't to modify object naming to your liking, and not necessarily nest the plots.






          share|improve this answer
























          • Thanks! It's exactly what I need!

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 16:30






          • 1





            This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

            – Ista
            Nov 22 '18 at 16:56






          • 1





            Yeah, pardon the style, this indeed can be golfed down if there is a need for it

            – Nutle
            Nov 22 '18 at 18:35











          • Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 19:01











          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%2f53434676%2fhow-to-automate-the-creation-of-histograms-in-r-saving-the-result-in-a-list-of%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          res <- lapply(list_dfs, function(z){
          ll <- lapply(z, function(x){
          hist(x, plot = FALSE)
          })
          names(ll) <- names(z)
          return(ll)
          })
          names(res) <- names(list_dfs)


          You may wan't to modify object naming to your liking, and not necessarily nest the plots.






          share|improve this answer
























          • Thanks! It's exactly what I need!

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 16:30






          • 1





            This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

            – Ista
            Nov 22 '18 at 16:56






          • 1





            Yeah, pardon the style, this indeed can be golfed down if there is a need for it

            – Nutle
            Nov 22 '18 at 18:35











          • Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 19:01
















          1














          res <- lapply(list_dfs, function(z){
          ll <- lapply(z, function(x){
          hist(x, plot = FALSE)
          })
          names(ll) <- names(z)
          return(ll)
          })
          names(res) <- names(list_dfs)


          You may wan't to modify object naming to your liking, and not necessarily nest the plots.






          share|improve this answer
























          • Thanks! It's exactly what I need!

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 16:30






          • 1





            This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

            – Ista
            Nov 22 '18 at 16:56






          • 1





            Yeah, pardon the style, this indeed can be golfed down if there is a need for it

            – Nutle
            Nov 22 '18 at 18:35











          • Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 19:01














          1












          1








          1







          res <- lapply(list_dfs, function(z){
          ll <- lapply(z, function(x){
          hist(x, plot = FALSE)
          })
          names(ll) <- names(z)
          return(ll)
          })
          names(res) <- names(list_dfs)


          You may wan't to modify object naming to your liking, and not necessarily nest the plots.






          share|improve this answer













          res <- lapply(list_dfs, function(z){
          ll <- lapply(z, function(x){
          hist(x, plot = FALSE)
          })
          names(ll) <- names(z)
          return(ll)
          })
          names(res) <- names(list_dfs)


          You may wan't to modify object naming to your liking, and not necessarily nest the plots.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 16:24









          NutleNutle

          308215




          308215













          • Thanks! It's exactly what I need!

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 16:30






          • 1





            This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

            – Ista
            Nov 22 '18 at 16:56






          • 1





            Yeah, pardon the style, this indeed can be golfed down if there is a need for it

            – Nutle
            Nov 22 '18 at 18:35











          • Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 19:01



















          • Thanks! It's exactly what I need!

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 16:30






          • 1





            This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

            – Ista
            Nov 22 '18 at 16:56






          • 1





            Yeah, pardon the style, this indeed can be golfed down if there is a need for it

            – Nutle
            Nov 22 '18 at 18:35











          • Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

            – Wlademir Ribeiro Prates
            Nov 22 '18 at 19:01

















          Thanks! It's exactly what I need!

          – Wlademir Ribeiro Prates
          Nov 22 '18 at 16:30





          Thanks! It's exactly what I need!

          – Wlademir Ribeiro Prates
          Nov 22 '18 at 16:30




          1




          1





          This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

          – Ista
          Nov 22 '18 at 16:56





          This can be simplified to res <- lapply(list_dfs, function(z) lapply(z, hist, plot = FALSE))

          – Ista
          Nov 22 '18 at 16:56




          1




          1





          Yeah, pardon the style, this indeed can be golfed down if there is a need for it

          – Nutle
          Nov 22 '18 at 18:35





          Yeah, pardon the style, this indeed can be golfed down if there is a need for it

          – Nutle
          Nov 22 '18 at 18:35













          Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

          – Wlademir Ribeiro Prates
          Nov 22 '18 at 19:01





          Both solutions are great. I did the same for density(), then I will plot these results with highcharter package in a shiny app. The goal is that I don't need to load all the data into the shiny app. Thanks.

          – Wlademir Ribeiro Prates
          Nov 22 '18 at 19:01


















          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%2f53434676%2fhow-to-automate-the-creation-of-histograms-in-r-saving-the-result-in-a-list-of%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python