功能不起作用
问题描述:
我已经定义了以下功能:功能不起作用
where_condition_SQL<-
function(table,selected_columns,where_conditions) {
new_table<-sqldf(sprintf("select %s from %s where %s",
as.character((selected_columns)),
deparse(substitute(table)),
as.character((where_conditions))))
return(new_table) }
select_category_amounts <- function(
input_table,selected_columns,category,category_column){
new_table<-where_condition_SQL(input_table,
selected_columns=as.character(selected_columns),
where_conditions=sprintf("%s='%s'",
as.character(category_column),
as.character(category))) return(new_table) }
当我尝试运行第二个功能:
select_category_amounts(second_table,"*","Reservas","categoria2")
那么它不承认second_table和给我以下错误:
Error in rsqlite_send_query([email protected], statement) :
no such table: input_table
我想这是关于环境的一些问题,但我没有得到重点。提前非常感谢您的帮助。
答
我会在第一个函数中使用eval
而不是deparse
,并将表名作为字符串传递。这样一来,就可以得到变量table
的内容,你的第一个功能:
where_condition_SQL<-
function(table,selected_columns,where_conditions) {
new_table<-sqldf(sprintf("select %s from %s where %s",
as.character((selected_columns)),
eval(table),
as.character((where_conditions))))
return(new_table) }
select_category_amounts("second_table","*","Reservas","categoria2")
Show Traceback
Rerun with Debug
Error in rsqlite_send_query([email protected], statement) :
no such table: second_table
随着eval
,你也可以通过表的名称作为一个对象:
second_table <- "mytable"
select_category_amounts(second_table,"*","Reservas","categoria2")
Error in rsqlite_send_query([email protected], statement) : no such table: mytable
+0
非常感谢你!有效! – Patricio
请仔细阅读[MCVE ]。 –