简体   繁体   English

使用'else'语句进行deparse()

[英]deparse() with 'else' statement

I am trying to write a shiny app which prints my function code and saves it as a .R file which the I can run to get an desired output. 我正在尝试编写一个闪亮的应用程序,该应用程序可以打印我的功能代码并将其保存为.R文件,我可以运行该文件以获取所需的输出。

To get the function code I was using deparse . 为了获得功能代码,我使用了deparse It was getting the job until recently, when one of my function had if else statement. 直到最近,我的功能之一就是if else语句时,它才开始工作。 deparse tends to separate else and } and hence if I run the code in the .R file I generate gives an error: deparse倾向于将else}分开,因此如果我在.R文件中运行代码,则会生成错误:

Error: unexpected 'else' in "else"

Is there a way to work around this issue? 有没有解决此问题的方法?

My sample code is as follows: 我的示例代码如下:

 MyFunction <- function(dat){
    if(any(dat == 1)){
      print("1 is observed")
    }else{
      print("1 is not present")
    }

  }

  func.string <- deparse(MyFunction)
  codeOfInterest <-func.string[3:8] 
  write(codeOfInterest, "Code.R")

You could use 你可以用

func.string <- capture.output(print(MyFunction))

This is the approach I take in the docstring package to gather the source code to be able to detect and extract any documentation comments. 这是我在docstring软件包中采用的方法,用于收集源代码以能够检测和提取任何文档注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM