Home > Mobile >  R shiny application stops execution of code after closing
R shiny application stops execution of code after closing

Time:12-21

This Shiny UI is placed within a larger code piece. When I run my code the execution of code after the application stops sometime after closing not always in the same place and without error. The code without the application runs fine and does not stop. Is there a way I need to be calling a shiny app within my R code?

Version info:

version
               _                                
platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32                  
status                                          
major          4                                
minor          2.2                              
year           2022                             
month          10                               
day            31                               
svn rev        83211                            
language       R                                
version.string R version 4.2.2 (2022-10-31 ucrt)
nickname       Innocent and Trusting      

The code example:

commonnames <- c("aaaple", "peear", "banana","pare","kiwwi","bannnana")
vector <-  c("apple", "pear", "banana","orange","kiwi","mango","grapes")

library(shiny)
# Define UI ----
ui <- fluidPage(
  titlePanel("Brands To Match To"),
  sidebarLayout(
    sidebarPanel(
      width = 10,
      hr("Common User Entered Words:"),
      hr(),
      x <- (paste(commonnames, collapse = ", ")),
      hr(),
      textInput("word1", vector[1]),
      textInput("word2", vector[2]),
      textInput("word3", vector[3]),
      textInput("word4", vector[4]),
      textInput("word5", vector[5]),
      textInput("word6", vector[6]),
      textInput("word7", vector[7]),
    ),
    mainPanel(
      width = 10,
      actionButton("save", "Save")
    )
  )
)

# Define server logic ----
server <- function(input, output, session) {
  words <- reactiveValues(words = list())

  observeEvent(input$save, {
    words$words <- list(
      word1 = input$word1,
      word2 = input$word2,
      word3 = input$word3,
      word4 = input$word4,
      word5 = input$word5,
      word6 = input$word6,
      word7 = input$word7
    )
  })

  observe({
    assign("words", words$words, envir = .GlobalEnv)
  })

  session$onSessionEnded(function() {
    stopApp()
  })
}

# Run the application ----
shinyApp(ui, server)


#Additional R code below here after exiting application does not run

CodePudding user response:

Try this (hijacked your save button):

commonnames <- c("aaaple", "peear", "banana","pare","kiwwi","bannnana")
vector <-  c("apple", "pear", "banana","orange","kiwi","mango","grapes")

library(shiny)
# Define UI ----
ui <- fluidPage(
  titlePanel("Brands To Match To"),
  sidebarLayout(
    sidebarPanel(
      width = 10,
      hr("Common User Entered Words:"),
      hr(),
      x <- (paste(commonnames, collapse = ", ")),
      hr(),
      textInput("word1", vector[1]),
      textInput("word2", vector[2]),
      textInput("word3", vector[3]),
      textInput("word4", vector[4]),
      textInput("word5", vector[5]),
      textInput("word6", vector[6]),
      textInput("word7", vector[7]),
    ),
    mainPanel(
      width = 10,
      actionButton("save", "Save")
    )
  )
)

# Define server logic ----
server <- function(input, output, session) {
  words <- reactiveValues(words = list())
  
  observeEvent(input$save, {
    stopApp()
  })
  
  observe({
    assign("words", list(
      word1 = input$word1,
      word2 = input$word2,
      word3 = input$word3,
      word4 = input$word4,
      word5 = input$word5,
      word6 = input$word6,
      word7 = input$word7
    ), envir = .GlobalEnv)
  })
  
  session$onSessionEnded(function() {
    stopApp()
  })
}

# Run the application ----
shinyApp(ui, server)


# code after shiny
234567
words
  • Related