Database Connection UI not showing when called inside a function

I am using RStudio Server and ODBC to connect to a redshift database. I can connect easily using:

  conn <- dbConnect(odbc::odbc(), Driver="redshift", 
                        Server = SERVER_URL,
                        Port = "5439",
                        Database = DB_NAME,
                        PWD = PASSWORD,
                        UID = CREDENTIALS,
                        timeout = 10,
                        Trusted_Connection = "True")

When connected in shows up in the pane/sidebar "connections" where I have an UI to look through the database. That is exactly what i want.

The problem is that if i call the same code inside a function, then I get the database connection but no UI?!? How do i get the UI to appear when calling this code from inside a function?

C

onnection_odbc_profile <- function(INPUT){
    conn <- dbConnect(odbc::odbc(), Driver="redshift", 
                        Server = SERVER_URL,
                        Port = "5439",
                        Database = DB_NAME,
                        PWD = PASSWORD,
                        UID = CREDENTIALS,
                        timeout = 10,
                        Trusted_Connection = "True")
     return(conn)
}


    
      

I think the issue is that the connection pane only gets updated when the code is run at top-level. Is there any way to force a line of code in a function to run at top-level (or directly in the console). I have not been able to find a solution online and i think its a bug if its not possible to set up the odbc database UI just because the connection is made inside a function...

I don't have your database to test with. (so its non trivial for me to see how to use code to add a connection to Rstudio IDE).
But here is an example of running arbitrary code in the global environment.

function_definer <- function(){
  fnc <- function(x){x*x}
  fnc(2)
  
}

function_definer()
# 4
fnc(2)
# error : could not find function fnc

function_definer2 <- function(){
  with(.GlobalEnv,{
  fnc <- function(x){x*x}
  fnc(2)
  })
}
function_definer2()
fnc(2)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.