Access/use R console when running a shiny app

后端 未结 3 638
我寻月下人不归
我寻月下人不归 2021-02-19 04:06

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)

相关标签:
3条回答
  • 2021-02-19 04:25

    Can't you use the <<- global assignment operator? I'm not sure how complicated the variable you need to set is, but say you just need to change some variable t to 5.

    We could make a textBoxInput that changes the input$new_t variable. Then have an observer:

    observeEvent(input$new_t, t <<- input$new_t)
    

    Then, when input$new_t changes, the global variable t changes.

    Edit: Another option is to put a browser() in the object that accesses the variable you need to change, right before the variable is accessed.

    0 讨论(0)
  • 2021-02-19 04:46

    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
    0 讨论(0)
  • 2021-02-19 04:47

    Well, I had the similar doubt! In my case, the solution was to create my own Shiny server: https://github.com/rstudio/shiny-server

    On the one hand, I have my app inside RStudio IDE when I want to change or to test new elements. Indeed this is the testing version. To run the application you execute runApp(dir), each time the port changes.

    On the other hand, I have the stable version inside the Shiny server. This is a helpful way to connect from different devices and have a operative version while you are doing some changes. The application is running all of time, you have to configurate your port inside this file: /etc/shiny-server/shiny-server.conf.

    If you need more information about Shiny server, consult this website: https://rstudio.github.io/shiny-server/latest/#configuration-settings

    0 讨论(0)
提交回复
热议问题