Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access/use R console when running a shiny app

Tags:

r

console

shiny

Does anybody know if one is able to access the R console when running a shiny app? (running the shiny application in background would also be helpful, if that's possible)

I need this for manipulating objects in .GlobalEnv which are used in the shiny application and this has to be done using the command line.

When starting the app the console is buzy. Is there a possibility to access the console from within the application?

Thanks in advance!

like image 240
tnaake Avatar asked Jun 03 '14 16:06

tnaake


People also ask

Can you run Shiny app without R?

By default, shiny runs with the option "window" inside R-Studio, which without R-Studio won't work.


1 Answers

R (and shiny) run single-threaded. This thread is used by the shiny application so you cannot interact with R whenever the app is running. If you want to run interactive commands during a shiny session you need to put a browser() inside your application like mentioned by @eric-canton.

A very simple application could look like this

library(shiny)

d <- data.frame(1:10, 1:10)

ui <-  fluidPage(
  actionButton("browser", "Trigger browser()"),
  actionButton("reload", "Reload Plot"),
  plotOutput("plot")
)


server <- function(input, output, session) {
  observeEvent(input$browser, {
    browser()
    1 + 1
  })

  output$plot <- renderPlot({
    input$reload
    plot(d)
  })
}

shinyApp(server = server, ui = ui)

Some comments about the code

  • I put 1 + 1 after the browser() command because setting browser() as the last argument tends to stop the interactive terminal unexpectedly in my experience
  • You need some shiny event to trigger the redrawing of the plot, because d is not a reactive value
  • If you are on the console you need to assign a new value to d by using the <<- operator because d lives outside the function you are calling:
Browse[2]> d <<- data.frame(x = 1:200, y = 200:1)
  • You can jump out of interactive console and resume the app by entering c and hitting Enter
like image 96
hundertdrei Avatar answered Oct 18 '22 18:10

hundertdrei