研发组织模式:如何避免命名绘图文件?

发布于 2024-12-18 19:23:07 字数 2906 浏览 2 评论 0 原文

当我将 SweaveR 一起使用时,我可以通过简单地将绘图命令包含在代码块中(例如 <> ...@Sweave 驱动程序会自动生成编号绘图文件,例如 fig1.pdffig2.pdf 等。

但是在 org-mode,似乎我需要在标题中使用 :file [...].pdf 显式命名图形文件,例如

#+attr_latex: width=8cm placement=[htbp]
#+begin_src R :results output graphics :exports results :file fig.pdf                                                                                                                                                                                                 
    require(ggplot2)                                                                                                                           
    a <- rnorm(100)                                                                                                                            
    b <- 2*a + rnorm(100)                                                                                                                      
    d <- data.frame(a,b)                                                                                                                       
    ggplot(d,aes(a,b)) + geom_point()                                                                                                          
#+end_src                                     

是否有某种方法可以避免显式命名绘图文件,以及组织模式乳胶导出引擎是否生成这些文件名?

更新:我在此处包含了 G. Jay Kerns 指出的解决方案以方便参考:您所需要做的就是在标头中包含一个生成临时文件的 emacs-lisp 函数,例如 <代码>:文件(org-babel-temp-file“./figure-”“.pdf”)。这会在当前目录中创建一个临时图形文件(因为 ./)。如果您希望临时图形文件位于全局临时目录(由变量 org-babel-temporary-directory 定义)中,则只需输入“.figure”:

#+attr_latex: width=8cm placement=[htbp]
#+begin_src R :results output graphics :exports results :file (org-babel-temp-file "./figure-" ".pdf")                                                                                                                                                                                                 
    require(ggplot2)                                                                                                                           
    a <- rnorm(100)                                                                                                                            
    b <- 2*a + rnorm(100)                                                                                                                      
    d <- data.frame(a,b)                                                                                                                       
    ggplot(d,aes(a,b)) + geom_point()                                                                                                          
#+end_src                                     

When I use Sweave with R, I can avoid explicitly naming the plot files by simply enclosing the plot commands within a code-chunk like <<fig=TRUE>> ... @. The Sweave driver automatically generates numbered plot files like fig1.pdf, fig2.pdf, etc.

However in org-mode, it seems like I need to explicitly name the figure file using a :file [...].pdf in the header, e.g.

#+attr_latex: width=8cm placement=[htbp]
#+begin_src R :results output graphics :exports results :file fig.pdf                                                                                                                                                                                                 
    require(ggplot2)                                                                                                                           
    a <- rnorm(100)                                                                                                                            
    b <- 2*a + rnorm(100)                                                                                                                      
    d <- data.frame(a,b)                                                                                                                       
    ggplot(d,aes(a,b)) + geom_point()                                                                                                          
#+end_src                                     

Is there some way to avoid explicitly naming the plot file, and have the org-mode latex export engine generate these file-names?

Update: I'm including the solution that G. Jay Kerns pointed to here for easy reference: all you need to do is include a temp-file-generating emacs-lisp function in the header, like :file (org-babel-temp-file "./figure-" ".pdf"). This creates a temp figure-file in the current directory (because of the ./). If you want the temp figure-file in a global temp directory (defined by the variable org-babel-temporary-directory), then just say ".figure":

#+attr_latex: width=8cm placement=[htbp]
#+begin_src R :results output graphics :exports results :file (org-babel-temp-file "./figure-" ".pdf")                                                                                                                                                                                                 
    require(ggplot2)                                                                                                                           
    a <- rnorm(100)                                                                                                                            
    b <- 2*a + rnorm(100)                                                                                                                      
    d <- data.frame(a,b)                                                                                                                       
    ggplot(d,aes(a,b)) + geom_point()                                                                                                          
#+end_src                                     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

執念 2024-12-25 19:23:07

很好的问题,九月份的组织模式邮件列表中也出现了一个类似的问题(加上一些额外的内容)。最初的问题是这里,以及一个草图可能的解决方案在于线程的最终消息 这里(参见#1,其他项目是关于其他事情)。

Great question, and a similar one (plus some extra stuff) came up on the Org-mode mailing list back in September. The original question is here, and a sketch of a possible solution lies in the final message of the thread here (see #1, the other items are about other things).

小矜持 2024-12-25 19:23:07

这是使用增量器的另一种方法,因为使用

(org-babel-temp-file "./figure-" ".pdf")

似乎对我不起作用:

(setq i 0)
#+begin_src R :file (concat "f" (number-to-string (incf i)) ".pdf") :results output graphics :exports results
(plot (rnorm(10))
#+end_src

( setq i) 每个会话只需调用一次即可定义变量;然后可以将其删除。
绘图将保存到您的 default-directory 中,并在使用 (org-latex 将 .org 文件导出到 .tex 时自动显示-导出为乳胶)

Here's another approach using an incrementor, as the use of

(org-babel-temp-file "./figure-" ".pdf")

didn't seem to work for me:

(setq i 0)
#+begin_src R :file (concat "f" (number-to-string (incf i)) ".pdf") :results output graphics :exports results
(plot (rnorm(10))
#+end_src

(setq i) only needs to be called once per session to define the variable; it can then be erased.
The plots are saved to your default-directory and appear automatically when exporting the .org file to .tex using (org-latex-export-as-latex).

开始看清了 2024-12-25 19:23:07

我改进了 [dardisco] 的方法,使行为真正自动化。基本上,下面的代码包含一个创建 Lisp 变量的块,然后将该变量提供给所有图形块。

该 elisp 块应放置在文件的开头(至少在任何图形块之前)。

#+name: fignumber 
#+begin_src emacs-lisp :results value silent :exports none :session 
(setq fignumber 0)
#+end_src

#+BEGIN_SRC R :results output graphics file :file (concat "figure" (number-to-string (incf fignumber)) ".pdf") :exports results :session
x <- 1:100
y <- 1:100
plot(x,y)
#+END_SRC


I improved the approach of [dardisco] to make the behavior truly automatic. Basically, the code below includes a block that creates a lisp variable that is then fed to all figure blocks.

This elisp block should be placed at the beginning of your file (before any figure block, at least).

#+name: fignumber 
#+begin_src emacs-lisp :results value silent :exports none :session 
(setq fignumber 0)
#+end_src

#+BEGIN_SRC R :results output graphics file :file (concat "figure" (number-to-string (incf fignumber)) ".pdf") :exports results :session
x <- 1:100
y <- 1:100
plot(x,y)
#+END_SRC


瑾兮 2024-12-25 19:23:07

我在下面做了一个 lisp 函数。
该函数有 2 个参数 DIRNAME 和 FNAME“fname”。
该函数生成一个路径DIRNAME/FNAME##.png,
其中 ## 是目录中的序号。
例如,如果有fig1.png、fig2.png、fig4.png和
如果您将Fig 作为FNAME,该函数将返回fig3.png(缺少数字)。
如果有fig1.png、fig2.png、fig3.png,则返回fig4.png。
如果没有文件,则返回Fig1.png。

(defun search-file-name (dirname fname) 
  "This function generates a path DIRNAME/FNAME##.png,
   where ## is a sequencial number in the directory.
   For example, if there are fig1.png fig2.png fig4.png and 
   you give fig as FNAME, this function returns fig3.png(missing number).
   If there are fig1.png fig2.png fig3.png, this returns fig4.png.
   If there are no files, this returns fig1.png."

  (setq string (directory-files dirname nil (concat fname "[0-9]+\\.png") t))
  (if (not string) (concat dirname fname "1.png")
    (progn
      (setq number-list
            (mapcar '(lambda (x)
                       (string-match (concat fname "\\([0-9]+\\)\\.png") x)
                       (match-string 1 x))
                    string))
      (setq w1 (sort (mapcar 'parse-integer number-list) '<))

      (setq missing nil)
      (loop for j from 1 to (length w1) do
            (if (not (equal j (elt w1 (- j 1))))
                (progn (setq missing j) (return missing))))
      (if (not missing) (setq missing (+ 1 (car (last w1)))))
      (concat dirname fname (number-to-string missing) ".png"))))

  (setq missing nil)
  (loop for j from 1 to (length w1) do
        (if (not (equal j (elt w1 (- j 1))))
            (progn (setq missing j) (return missing))))
  (if (not missing) (setq missing (+ 1 (car (last w1)))))
  (concat dirname fname (number-to-string missing) ".png"))))

I made a lisp function below.
This function has 2 arguments DIRNAME and FNAME"fname".
This function generates a path DIRNAME/FNAME##.png,
where ## is a sequencial number in the directory.
For example, if there are fig1.png fig2.png fig4.png and
you give fig as FNAME, this function returns fig3.png(missing number).
If there are fig1.png fig2.png fig3.png, this returns fig4.png.
If there are no files, this returns fig1.png.

(defun search-file-name (dirname fname) 
  "This function generates a path DIRNAME/FNAME##.png,
   where ## is a sequencial number in the directory.
   For example, if there are fig1.png fig2.png fig4.png and 
   you give fig as FNAME, this function returns fig3.png(missing number).
   If there are fig1.png fig2.png fig3.png, this returns fig4.png.
   If there are no files, this returns fig1.png."

  (setq string (directory-files dirname nil (concat fname "[0-9]+\\.png") t))
  (if (not string) (concat dirname fname "1.png")
    (progn
      (setq number-list
            (mapcar '(lambda (x)
                       (string-match (concat fname "\\([0-9]+\\)\\.png") x)
                       (match-string 1 x))
                    string))
      (setq w1 (sort (mapcar 'parse-integer number-list) '<))

      (setq missing nil)
      (loop for j from 1 to (length w1) do
            (if (not (equal j (elt w1 (- j 1))))
                (progn (setq missing j) (return missing))))
      (if (not missing) (setq missing (+ 1 (car (last w1)))))
      (concat dirname fname (number-to-string missing) ".png"))))

  (setq missing nil)
  (loop for j from 1 to (length w1) do
        (if (not (equal j (elt w1 (- j 1))))
            (progn (setq missing j) (return missing))))
  (if (not missing) (setq missing (+ 1 (car (last w1)))))
  (concat dirname fname (number-to-string missing) ".png"))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文