如何在R函数中自动赋值给menu()?

时间:2017-01-08 12:49:56

标签: r

如下所示我输入代码:

dtype <- menu(c("continuous", "binary", "ordinal", "quantal", 
                "continuous, clustered", "quantal, clustered", "continuous, summary data", 
                "quantal, CxT", "other"), title = "\nWhat type of response data do you want to consider?")
3
dtype

和赋值1到dtype,但它不能在R函数中工作。

test <- function(type){
   dtype <- menu(c("continuous", "binary", "ordinal", "quantal", 
                   "continuous, clustered", "quantal, clustered", "continuous, summary data", 
                   "quantal, CxT", "other"), title = "\nWhat type of response data do you want to consider?")
   type
   return(dtype)
 }
test(type=1)

我将类型更改为eval(表达式(类型)),也不起作用。 有谁知道如何修改此代码以获得正确的结果?

这是一个例子:

rm(list=ls())

library("tcltk")
library("gWidgets")
library("digest")
library("assertive")
library("gWidgetstcltk")
library("proast38.9")

# data
jia_data <-list(dose = c(0.94,3.35,7.08,15.41),
                N = c(305,576,548,256),
                effect = c(32,106,193,156))


# transform format
data_jia_gui<-list(data = as.data.frame(jia_data),varnames = c("dose","N","effect"),nvar =3,
                   dtype= c(1,1,1))

f.proast(data_jia_gui)
4 # datatype
1 # independent variable
2 # model selection
3 # dependent variable
2 # weight
0 # corvariable
3 # BMDtype
0.10  #  BMR
1  #  
1 #plot
"hjy.txt"  #store file name
14  #exit PROAST
hjy <-read.csv2("hjy.txt",header = T , sep = "\t",skip = 2,nrows = 10)

2 个答案:

答案 0 :(得分:1)

您可以覆盖menu()函数以更改程序包的行为。您自己的menu()函数应该自动选择。但是,这是一个非常hacky的解决方案。如果其他人看到你这样做,他们会因为你不是一个优秀的程序员而责怪你。因此,更好的选择是为您正在使用的功能找到替代方案。例如,切换到另一个包或深入了解proast包及其源代码。

如果你真的想要覆盖菜单功能,你可以在这个例子中获取战利品:

function_that_uses_menu <- function() {
  # do something
  choice <- menu(c("one", "two"))
  # do other things
  return(choice)
}

automated_menu <- function(choices, graphics = FALSE, title = NULL) {
  return("one")
}

print(function_that_uses_menu())

function_env <- environment(menu)
original <- get("menu", envir = function_env)
assign("menu", automated_menu, envir = function_env)

print(function_that_uses_menu())
assign("menu", original, envir = function_env)

在此示例中,menu()函数被覆盖,以便function_that_uses_menu()使用您自己定义的menu()函数。如果function_that_uses_menu()在另一个包中(在这种情况下),则应首先解除其绑定,然后再进行绑定。

unlockBinding("menu", function_env)
assign("menu", automated_menu, envir = function_env)

您可能需要阅读Wickham's Advanced R中的this blog postthis chapter

答案 1 :(得分:0)

rm(list=ls())

library("tcltk")
library("gWidgets")
library("digest")
library("assertive")
library("gWidgetstcltk")
library("proast38.9")

# data
jia_data <-list(dose = c(0.94,3.35,7.08,15.41),
                N = c(305,576,548,256),
                effect = c(32,106,193,156))


# transform format
data_jia_gui<-list(data = as.data.frame(jia_data),varnames = c("dose","N","effect"),nvar =3,
                   dtype= c(1,1,1))

f.proast(data_jia_gui)
4 # datatype
1 # independent variable
2 # model selection
3 # dependent variable
2 # weight
0 # corvariable
3 # BMDtype
0.10  #  BMR
1  #  
1 #plot
"hjy.txt"  #store file name
14  #exit PROAST
hjy <-read.csv2("hjy.txt",header = T , sep = "\t",skip = 2,nrows = 10)