运行需要非交互式输入的R程序包

时间:2018-12-15 11:36:51

标签: r

在R编程方面,我还是一个新手,所以如果听起来很明显或被误导了,请原谅我。

我正在使用一个名为bcrm的R程序包(该程序在进行癌症临床试验的剂量递增方面做得很聪明),当我交互运行它时,它要求我通过终端输入。

我希望能够非交互式地运行它。我有什么办法可以编写一个脚本,该脚本不仅包括调用bcrm软件包的命令,还包括对它随后提出的问题的答案?

编辑2018年12月21日:这是要求我提供交互式输入的代码。我很想在代码的最后一位之后(或者可能在DOS批处理脚本中)放置一些代码,以提供输入,包括输入一系列数字。

library(bcrm)

dose.levels <- c(1, 2, 3, 4)
prior.tox <- c(0.05, 0.1, 0.2, 0.3)
cohort.size <- 3
target.tox <- 0.33
max.size <- 6
prior.mean <- c(-0.5, 0.01)
prior.vcm <- matrix(c(0.5, 0.3, 0.3, 2), ncol=2)
prior.dist <- list(4, prior.mean, prior.vcm) 

tox.seq <- c(0, 0, 0)
dose.seq <- c(1, 1, 1)

mydata <- data.frame(patient = 1:3, dose=dose.seq, tox=tox.seq)




crm<-bcrm(dose = dose.levels,               # Dose levels
          p.tox0 = prior.tox,               # Prior probabilities of DLT
          target.tox = target.tox,          # Target tox level
          cohort = cohort.size,             # Cohort size
          stop = list(nmax = max.size),     # Stopping criteria
          ff = "logit2",                    # Model
          prior.alpha = prior.dist,         # Prior distribution on model parameter
          sdose.calculate = "median",       # How to calculate dose labels
          pointest = "plugin",              # How we will estimate DLT risks
          data = mydata,                    # Data so far
          simulate = FALSE,                 # Simulate lots of trials?
          method="rjags",                   # Calculation method
          truep = prior.tox,                # True probabilities, assume same as prior
          plot = TRUE)                      # Plot trial data as we go

2 个答案:

答案 0 :(得分:1)

查看返回值的数据结构,如果您将simulate = True和nsims = 1设置为

,则可以提取与设置simulate = false相同的输出值。

例如 设置模拟=假给出:

<Modal
    isOpen={this.state.modalIsOpen}
    onAfterOpen={this.afterOpenModal}
    onRequestClose={this.closeModal}
    style={customStyles}
    contentLabel='Movies modal'
>
    {
        //Would like to print relative data here
    }
    <h2 ref={subtitle => this.subtitle = subtitle}>TITLE GOES HERE</h2>
    <div>
        <p>Id: {this.state.movie.id}</p>
        <h5 className='modalRelease'>Released: {this.state.movie.release}</h5>
        <h5 className='modalVote'>Rating: {this.state.movie.rating}</h5>
    </div>
    <button className='modalClose' onClick={this.closeModal}>X</button>
</Modal>

设置Simulator = true和nsims = 1会给出:

crm$ndose[[1]][[1]] # returns [1] 2

与以前一样返回2,所有其他值也相同。

答案 1 :(得分:0)

也许有人证明我错了(我希望如此),但是:

尝试找到一些解决方法后,我认为在不更改程序包源代码(或找到大手法)的情况下,无法自动注入答案。

背景

  1. 软件包bcrm使用标准R的readline函数接受交互式输入(请参见源代码,例如函数crm.interactivehttps://github.com/cran/bcrm/blob/master/R/bcrm_0.4.7.R

  2. readline仅在交互模式下有效(请参阅帮助):

      

    在非交互式使用中,结果就像响应是RETURN并且值为“”。

  3. 如果我尝试在启用了“交互”模式(R --interactive < your_code.R)的(Linux)命令行中执行您的代码,则必须使用输入重定向来提供R代码,并且因此无法重定向输入本身(我猜想它会被忽略,因为readline不会读stdin,而是读console)。

  4. Rscript --interactive -e 'source("your_code.R")也不起作用(不接受“ --interactive”参数)。

因此,可能的解决方案需要更改软件包的源代码,以提供使用readline支持“交互式”答案e的替代方法。 G。来自文件或可选参数(请与作者联系以要求更改此内容)。

由于源代码包含在一个文件中(请参见https://github.com/cran/bcrm/blob/master/R/bcrm_0.4.7.R),所以我想您可以在应用了一些更改后,例如将其作为源文件。 g。

  1. readline()替换对my.readline()的每次呼叫
  2. 通过为每个新调用返回一个字符串“行”来实现函数my.readline()(您可以从字符向量中获取值并记住最后一个被调用值的索引)。

示例:

cur.answer = 0
answers    = c("0", "1", "0")

my.readline <- function() {

  cur.answer <<- cur.answer + 1  # use "global" variable!

  if (cur.answer <= length(answers))
    return (answers[cur.answer])

  return ("")  # default
}

my.readline()
# [1] "0"
my.readline()
# [1] "1"
my.readline()
# [1] "0"
my.readline()
# [1] ""
相关问题