r语言 - 使用对话框错误地执行错误语句的函数



我正在编写一个函数,该函数将R会话连接到许多Oracle数据库之一。在此函数中,用户必须始终输入其用户名和密码,但可能不需要输入主机名/端口号,具体取决于这些参数是否已编程到函数中。如果函数中不包含主机/端口,则系统将提示用户手动输入这些参数。

无论出于何种原因,我的函数错误地执行了错误的语句并强制用户输入主机名/端口号,尽管他们使用的是预编程的数据库。有谁知道为什么会这样?现在就给我发疯...

有关示例代码,请参见下文。出于某种原因,唯一正确执行的是最后一个数据库 (DB4(。

library(svDialogs)
library(getPass)
db_function <- function(database) {
login_1 <- dlg_input(message = paste("Input", database, "Username:"), Sys.info()["login_1"])$res
login_2 <- getPass(msg = paste("Input", database, "Password:"))
if(database == "DB1") {
host <- "host1"
port <- 9999
}
if(database == "DB2") {
host <- "host2"
port <- 9999
}
if(database == "DB3") {
host <- "host3"
port <- 9999
}
if(database == "DB4") {
host <- "host4"
port <- 9999
}
else {
host <- dlg_input(message = paste("Input", database, "Host Name:"),
Sys.info()["host"])$res
port <- dlg_input(message = paste("Input", database, "Port Number:"),
Sys.info()["port"])$res
}
connect_string <- paste(
"(DESCRIPTION=",
"(ADDRESS=(PROTOCOL=tcp)(HOST=", host, ")(PORT=", port, "))",
"(CONNECT_DATA=(SERVICE_NAME=", database, ")))", sep = "")
}
## executes incorrectly (programmed but still prompted to input manually)    
db_function("DB3")
## executes correctly (programmed and not prompted to input manually)
db_function("DB4")
## executes correctly (not programmed, so prompted to input manually)
db_function("DB5")

非常感谢任何人对此可以提供的任何帮助!

如前所述,这应该可以解决它

library(svDialogs)
library(getPass)
db_function <- function(database) {
login_1 <- dlg_input(message = paste("Input", database, "Username:"), Sys.info()["login_1"])$res
login_2 <- getPass(msg = paste("Input", database, "Password:"))
if(database == "DB1") {
host <- "host1"
port <- 9999
} else
if(database == "DB2") {
host <- "host2"
port <- 9999
} else
if(database == "DB3") {
host <- "host3"
port <- 9999
} else
if(database == "DB4") {
host <- "host4"
port <- 9999
}
else {
host <- dlg_input(message = paste("Input", database, "Host Name:"),
Sys.info()["host"])$res
port <- dlg_input(message = paste("Input", database, "Port Number:"),
Sys.info()["port"])$res
}
connect_string <- paste(
"(DESCRIPTION=",
"(ADDRESS=(PROTOCOL=tcp)(HOST=", host, ")(PORT=", port, "))",
"(CONNECT_DATA=(SERVICE_NAME=", database, ")))", sep = "")
}
## executes incorrectly (programmed but still prompted to input manually)    
db_function("DB3")
## executes correctly (programmed and not prompted to input manually)
db_function("DB4")
## executes correctly (not programmed, so prompted to input manually)
db_function("DB5")

相关内容

  • 没有找到相关文章