R函数;如果未给出输入,则要求输入

标签 r

我正在尝试在 R 中编写一个将两个输入作为字符串的函数。如果没有设置输入,它会要求输入,然后继续执行该功能。

Input < - function(j,k){
   if ((j==j)&&(k==k)){
      j <- readline(prompt="Enter Input 1: ")
      k <- readline(prompt="Enter Input 2: ")
      Input(j,k)
   }else if ((j=="<string here>")&&(k=="<string here>")){
      ....
   }
}

最佳答案

我认为构建您的方法的更好方法是使用可选参数并在继续之前进行测试以查看它们是否为非空,尽管诚然您发布的问题非常模糊:

Input < - function(j=NA, k=NA) {
  if (is.na(j) | is.na(k)){
    j <- readline(prompt="Enter Input 1: ")
    k <- readline(prompt="Enter Input 2: ")
    Input(j, k)
  } else if ((j == "<string here>") & (k == "<string here>")) {
    ....
  }
}

关于R函数;如果未给出输入,则要求输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158361/

相关文章:

r - 在 R 中构造距离矩阵,但来自多个输入矩阵

r - 在 Shiny 中使用 googlevis 绘制动态 UI

r - 使用 devtools::install_github() 安装 R 包时无法锁定目录

json - 用 jsonlite 或其他东西从 R 编码一个 JSON 表达式

html - 将 Markdown 另存为交互式 HTML

r - 选择要在 data.table 中保留/删除的组

r - 在Rmarkdown V2给定的DocumentClass

r - Inner_join 有两个条件和区间内的区间条件

r - Plotly with R ggplot 隐私

r - ggplot2中的四分位距