首页 > 解决方案 > RStudio Server + Shiny - [没有可用的堆栈跟踪]

问题描述

我需要帮助,我正在努力学习闪亮,但我遇到了以下问题:我根本没有收到任何堆栈跟踪。即使我正在复制书中的示例:

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

我书中的闪亮代码只是为了测试堆栈跟踪:

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)

我收到的信息是:

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

R studio 服务器正在运行:10.4.1.252:8787

我的猜测是与 Shiny 连接的东西正在侦听本地 IP 地址(127.0.0.1)并且应用程序正在远程服务器上运行,我需要连接到 VPN 才能连接到该机器(以及 RStudio 服务器本身)

标签: rshiny

解决方案


该错误已经给您带来了问题:

selectInput 默认创建一个字符类输入(这可能会随着时间而改变)

有两种解决方法:

  1. 使用 as.numeric()
  2. 使用数字输入()
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)



推荐阅读