Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating and displaying graphs in a loop with dyndoc

    Hi,

    I try to generate standard graphs with every numeric variable in my dataset and create a nice looking data report with dyndoc. I am not figuring out if creating graphs in a loop with dyndoc let me insert these graphs (Stata 15). My code looks currently like:

    Code:
    <<dd_version: 1>>
    
    # Distribution of Variables in data set
    
    <<dd_do: quietly>>
    
    clear
    set more off
    global p_data "...\2019_Datensaetze\"
    global p_graphs "...\201910_Betriebspanel_Datencheck\graphs\"
    
    
    use myfile_ext.dta
    
    foreach x of varlist * {
     
     confirm numeric variable `x'
     if !_rc {
      
      capture drop unique
      egen unique = group(`x')
      quietly: summarize unique
      
      if `r(max)' < 10{
       di "Variable: `x'"
    
       graph bar , over(`x') blabel(bar, format(%4.1f)) intensity(25) ///
       ytitle("in Prozent (`x')", size(small))
      
       graph save "$p_graphs\g_`x'.gph", replace
      
      * NOT WORKING:
      * <<dd_graph: sav("g_`x'.svg") alt("scatter mpg weight") replace height(400)>>
      
      }
      
      else{
      }
     }
    }
    
    <</dd_do>>
    <<dd_graph: sav("g_`x'.svg") alt("scatter mpg weight") replace height(400)>>
    only works outside dd_do and cannot be included in the loop....

    What I have tried so far:
    • saving Graphs in loop and using them later - is not working
    • Looping the dyndoc.txt file itselve. That works but creates instead of one html several html files and "append" is not working with dyndoc
    • use putdocx instaed: that works actually, but putdocx gives me trouble with other output I want to create. I prefer very much dyndoc….
    Any suggestions? Is it a question of the stata version maybe and already solved in stata 16?

    Thankyou very much in advance,


    Marieke
    Last edited by Marieke Volkert; 18 Dec 2019, 10:43.

  • #2
    Hi Marieke,

    I realize this thread has been dormant for quite a long time but ran into a similar issue recently.

    The best way I could find to get this to work is to capture a set of graphs individually, record them in a file in markdown format, call the markdown command in the <<dd_do>> block and <<dd_include>> the .html formatted file to display in the original.

    the below is a reproducible example (assumes you have a directory called /gphs in the current working directory where .svg graphs can be saved (using Stata v16.1).

    Code:
    <<dd_version:2>>
    <<dd_do:quietly>>
    sysuse auto
    
    file open hist_catch using "examp_hists.txt", write
    
    foreach hst_var of varlist price-foreign { 
        histogram `hst_var'
        graph export ./gphs/`hst_var'.svg, replace
        file write hist_catch "![`hst_var'](gphs/`hst_var'.svg)" _newline                                
    }
    
    file close hist_catch
    
    markdown examp_hists.txt, saving(examp_hists.html)                    
    <</dd_do>>
    
    <<dd_include: examp_hists.html>>
    There may well be a more elegant way to do this but the above approach is scalable and it works.

    I've attached an example of a .docx format file that this command can create when called with dyndoc ... , docx

    - joe
    Attached Files
    Joseph Nicholas Luchman, Ph.D., PStat® (American Statistical Association)
    ----
    Research Fellow
    Fors Marsh

    ----
    Version 18.0 MP

    Comment


    • #3
      Hi Marieke,

      I realize this thread has been dormant for quite a long time but ran into a similar issue recently.

      The best way I could find to get this to work is to capture a set of graphs individually, record them in a file in markdown format, call the markdown command in the <<dd_do>> block and <<dd_include>> the .html formatted file to display in the original.

      the below is a reproducible example (assumes you have a directory called /gphs in the current working directory where .svg graphs can be saved (using Stata v16.1).

      Code:
      <<dd_version:2>>
      <<dd_do:quietly>>
      sysuse auto
      
      file open hist_catch using "examp_hists.txt", write
      
      foreach hst_var of varlist price-foreign { 
          histogram `hst_var'
          graph export ./gphs/`hst_var'.svg, replace
          file write hist_catch "![`hst_var'](gphs/`hst_var'.svg)" _newline                                
      }
      
      file close hist_catch
      
      markdown examp_hists.txt, saving(examp_hists.html)                    
      <</dd_do>>
      
      <<dd_include: examp_hists.html>>
      There may well be a more elegant way to do this but the above approach is scalable and it works.

      I've attached an example of a .docx format file that this command can create when called with dyndoc ... , docx.

      - joe
      Attached Files
      Joseph Nicholas Luchman, Ph.D., PStat® (American Statistical Association)
      ----
      Research Fellow
      Fors Marsh

      ----
      Version 18.0 MP

      Comment

      Working...
      X