RStudio Server + Shiny - [No stack trace available]

Viewed 868

I need help, I'm trying to learn shiny and I have following problem: I don't recieve any stack trace at all. Even if I'm replicating the example from the book:

https://mastering-shiny.org/action-workflow.html#tracebacks-in-shiny

My Shiny code from the book just to test the stack trace:

library(shiny)
   
   h <- function(x) x * 2
   
   ui <- fluidPage(
     selectInput("n", "N", 1:10),
     plotOutput("plot")
   )
   server <- function(input, output, session) {
     output$plot <- renderPlot({
       n <- h(input$n)
       plot(head(cars, n))
     }, res = 96)
   }
   shinyApp(ui, server)

The information I'm receiving is:

Listening on http://127.0.0.1:7840
Warning: Error in *: non-numeric argument to binary operator
  [No stack trace available]

R studio Server is Running on: 10.4.1.252:8787

My guess would be something connected with Shiny is listening on the local IP address (127.0.0.1) and App is running on the remote server and I need to be connected to VPN to connect with that machine (and with the RStudio Server itself)

2 Answers

For the traceback, just add traceback() into the server.R

library(shiny)

h <- function(x) x * 2

ui <- fluidPage(
  selectInput("n", "N", 1:10),
  plotOutput("plot")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    n <- h(input$n)
    plot(head(cars, n))
  }, res = 96)
  traceback()
}
shinyApp(ui, server)

##########################

13: execCallbacks(timeoutSecs, all, loop$id)
12: run_now(timeoutMs/1000, all = FALSE)
11: service(timeout)
10: serviceApp()
9: ..stacktracefloor..(serviceApp())
8: withCallingHandlers(expr, error = doCaptureStack)
7: domain$wrapSync(expr)
6: promises::with_promise_domain(createStackTracePromiseDomain(), 
       expr)
5: captureStackTraces({
       while (!.globals$stopped) {
           ..stacktracefloor..(serviceApp())
       }
   })
4: ..stacktraceoff..(captureStackTraces({
       while (!.globals$stopped) {
           ..stacktracefloor..(serviceApp())
       }
   }))
3: runApp(x)
2: print.shiny.appobj(x)
1: (function (x, ...) 
   UseMethod("print"))(x)
Warning: Error in *: non-numeric argument to binary operator
  [No stack trace available]

The error already gives you the problem:

selectInput creates a character class input as default (this might have changed over time)

there are two workarounds:

  1. use as.numeric()
  2. use numericInput()
library(shiny)

h <- function(x) x * 2

ui <- fluidPage(
  selectInput("n", "N", 1:10),
  numericInput("n2", "Num input",1,min=1,max=10, step = 1),  # solution 2
  plotOutput("plot"),
  textOutput("c1"),
  textOutput("c2")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    n <- h(as.numeric(input$n))  # solution 1
    plot(head(cars, n))
  }, res = 96)
  # explanation:
  output$c1<-renderText({class(input$n)})
  output$c2<-renderText({class(as.numeric(input$n))})
  output$c3<-renderText({class(input$n3)})
}
shinyApp(ui, server)


Related