Announcement

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

  • Local variables in dyndoc not expressed correctly in HTML code

    Dear all,

    I'm trying to put several graphs for several cohorts into cohort-specific html files using dyndoc. The problem I encounter is that while Stata handels the local variables inside the loop correctly, those are expressed literally in the html output instead of with the values that the locals take. I have made a toy-code for the auto data that reflects what I'm doing (much shorter, same issue in output though).

    Code:
    forval x = 3/5 {
        dyndoc ../../test.txt `x', saving(../Output/test_r`x'.html) nomsg replace
    }
    where x are cohorts over which I'm looping in my code, but here for the sake of the example the repair records.

    Inside test.txt I'm (among other things) generating various graphs. (Those have "fixed" names.) I then want to display these in the file I'm generating with dyndoc.
    The code in test.txt is given (in a short version by):

    Code:
    <<dd_version: 1>>
    
    A Test
    =====
    <<dd_do: nocom noout>>
    sysuse auto, clear
    keep if rep == `1'
    scatter mpg weight, mcolor(blue%50) name(scatter9, replace)
    <</dd_do>>
    
    This is a scatterplot.
    
    <<dd_graph: gr(scatter9) saving(../Output/scatter`1'_9.svg) replace>>
    This works fine to the point that "sequenceplot_c3_9.svg" etc. are saved in the corresponding directory. However, in the html files the graphs are referenced as <img src="scatter_c%601'_9.svg"> and hence won't be shown when one looks at the html files.

  • #2
    Welcome to Statalist. Thank you for the well-presented reproducible example of your problem.

    I believe your problem is an extension of a general principle - not stated in the dyndoc documentation - that Stata does not expand macros that appear in dynamic tags. (The seeming exception is that <<dd_display>> passes its arguments back to Stata which then can interpret scalars, local macros, and the like.)

    I tried hacks using <<dd_include>> and <<dd_skip_if>> to no avail. Below is an old-time hack, of using Stata to write Stata code to be run. It works. It is ugly. I would not be at all unhappy if someone else found a better way.
    Code:
    forval x = 3/5 {
        file open inc using test.txt, write text replace
        file write inc _newline `"<<dd_version: 1>>"'
        file write inc _newline `"A Test"'
        file write inc _newline `"====="'
        file write inc _newline `"<<dd_do: nocom noout>>"'
        file write inc _newline `"sysuse auto, clear"'
        file write inc _newline `"keep if rep78 == `x'"'
        file write inc _newline `"scatter mpg weight, mcolor(blue%50) name(scatter9, replace)"'
        file write inc _newline `"<</dd_do>>"'
        file write inc _newline `"<<dd_graph: gr(scatter9) saving(scatter`x'_9.svg) replace>>"'
        file write inc _newline
        file close inc
        dyndoc test.txt, saving(test_r`x'.html) nomsg replace
    }
    Code:
    . dir
    
    total 96
    -rw-r--r--  1 lisowskiw  staff  10133 May  7 16:34 scatter3_9.svg
    -rw-r--r--  1 lisowskiw  staff   7785 May  7 16:34 scatter4_9.svg
    -rw-r--r--  1 lisowskiw  staff   6861 May  7 16:34 scatter5_9.svg
    -rw-r--r--  1 lisowskiw  staff    222 May  7 16:34 test.txt
    -rw-r--r--  1 lisowskiw  staff     78 May  7 16:34 test_r3.html
    -rw-r--r--  1 lisowskiw  staff     78 May  7 16:34 test_r4.html
    -rw-r--r--  1 lisowskiw  staff     78 May  7 16:34 test_r5.html
    
    . type test.txt
    
    <<dd_version: 1>>
    A Test
    =====
    <<dd_do: nocom noout>>
    sysuse auto, clear
    keep if rep78 == 5
    scatter mpg weight, mcolor(blue%50) name(scatter9, replace)
    <</dd_do>>
    <<dd_graph: gr(scatter9) saving(scatter5_9.svg) replace>>
    
    . type test_r3.html
    <h1><a href="#a-test" id="a-test">A Test</a></h1>
    <img src="scatter3_9.svg" >
    
    . type test_r4.html
    <h1><a href="#a-test" id="a-test">A Test</a></h1>
    <img src="scatter4_9.svg" >
    
    . type test_r5.html
    <h1><a href="#a-test" id="a-test">A Test</a></h1>
    <img src="scatter5_9.svg" >
    Last edited by William Lisowski; 07 May 2019, 14:39.

    Comment


    • #3
      Thanks William Lisowski

      I hope there's a "nice" solution out there, somewhere otherwise I'll try your "ugly" one - it does still beat having to manually fix files after all

      Comment


      • #4
        Well, I think a "nicer" solution is a reasonable request. I'll look into adding macro expansion in saving() within dd_graph tag and filename in dd_include tag.

        Comment


        • #5
          Thank you Hua Peng (StataCorp) - I'm looking forward to the expansion.

          Comment


          • #6
            My pride forces me to post the following alternative to my post #2. It takes an approach inspired by the thought of having to "manually fix" files and automates the fixing.

            This is test.txt
            Code:
            <<dd_version: 1>>
            
            A Test
            =====
            <<dd_do: nocom noout>>
            sysuse auto, clear
            keep if rep78 == `1'
            scatter mpg weight, mcolor(blue%50) name(scatter9, replace)
            <</dd_do>>
            
            This is a scatterplot.
            
            <<dd_graph: gr(scatter9) saving(GNXL.svg) replace>>
            and this is the corresponding do-file
            Code:
            forval x = 3/5 {
                dyndoc test.txt `x', saving(GNXL.html) nomsg replace
                shell cp GNXL.svg scatter`x'_9.svg // Unix/macOS shell command
                quietly filefilter GNXL.html test_r`x'.html , from("GNXL") to("scatter`x'_9") replace
            }
            and these are the results
            Code:
            . dir
            
            total 112
            -rw-r--r--  1 lisowskiw  staff    102 May  8 08:19 GNXL.html
            -rw-r--r--  1 lisowskiw  staff   6861 May  8 08:19 GNXL.svg
            -rw-r--r--  1 lisowskiw  staff  10133 May  8 08:19 scatter3_9.svg
            -rw-r--r--  1 lisowskiw  staff   7785 May  8 08:19 scatter4_9.svg
            -rw-r--r--  1 lisowskiw  staff   6861 May  8 08:19 scatter5_9.svg
            -rw-r--r--  1 lisowskiw  staff    243 May  8 08:18 test.txt
            -rw-r--r--  1 lisowskiw  staff    108 May  8 08:19 test_r3.html
            -rw-r--r--  1 lisowskiw  staff    108 May  8 08:19 test_r4.html
            -rw-r--r--  1 lisowskiw  staff    108 May  8 08:19 test_r5.html
            
            . type test_r3.html
            <h1><a href="#a-test" id="a-test">A Test</a></h1>
            <p>This is a scatterplot.</p>
            <img src="scatter3_9.svg" >
            
            . type test_r4.html
            <h1><a href="#a-test" id="a-test">A Test</a></h1>
            <p>This is a scatterplot.</p>
            <img src="scatter4_9.svg" >
            
            . type test_r5.html
            <h1><a href="#a-test" id="a-test">A Test</a></h1>
            <p>This is a scatterplot.</p>
            <img src="scatter5_9.svg" >
            That more closely meets my standards of unembarassing code, and more importantly is easier to write and maintain.

            Comment

            Working...
            X