Access RStudio R console when running a shiny gadget/app

Dear all,

We are developing an integration to improve code quality with AI using a Shiny prototype that reads and analyzes console history.
I’ve written an RStudio addin that launches a Shiny gadget in the RStudio Viewer. However, once the addin is running, my R console is locked until I close it. Here’s a minimal example of my code:

library(rstudioapi)
library(shiny)

# Simplified function to fetch history
get_r_history <- function() {
  # Simulates fetching history (real function fetches R or terminal history)
  return(c("plot(1:10)", "summary(mtcars)", "lm(y ~ x, data = df)"))
}

# Simplified addin UI and server
run_addin <- function() {
  history <- get_r_history()

  ui <- fluidPage(
    titlePanel("History Selector"),
    selectInput("selected_line", "Select a line:", choices = history),
    actionButton("insert", "Insert into Script")
  )

  server <- function(input, output, session) {
    observeEvent(input$insert, {
      selected_line <- input$selected_line
      if (!is.null(selected_line)) {
        context <- rstudioapi::getActiveDocumentContext()
        if (!is.null(context)) {
          rstudioapi::insertText(location = context$selection[[1]]$range, text = selected_line)
        }
      }
    })
  }

  shiny::runGadget(ui, server, viewer = shiny::paneViewer())
}

run_addin()

When the gadget is displayed in the RStudio Viewer, I cannot execute code in the console simultaneously. Is there a way to keep the gadget in the RStudio Viewer but still have access to the console? I have seen that by using browserViewer() to open it externally might work, but I specifically want to display it in the RStudio Viewer.
I have tried to convert it to a shiny app and run it as a background job, but doing so does not allow the app to access the r environment and thus the history and ability to interact with the documents opened in RStudio.
I am not fully confident about shiny since it is my first experience with it, so I don't fully understand the differences between a shiny gadget and an app.
Any suggestions or workarounds would be greatly appreciated.

I left an answer here:

Will do now, thank you

I could not replicate your example in my script and while the app is run in background I am not able to access either active documents nor the R history

For me it works just fine:

screen