我幾乎沒有使用 R-Shiny 的經驗,但我認為我可以通過一個簡單的例子來完成它。使用了 Wickham 的 Mastering Shiny 一書,但我似乎無法讓它發揮作用。
我基本上是在嘗試做一件非常簡單的事情:我想生成一個取決于(形狀)引數的圖。我的代碼如下(簡化為它的本質):
library(sn)
library(ggplot)
ui <- fluidPage(
numericInput("a", label = "alpha", min = -3, max = 3, value = 0.0),
plotOutput("plot")
)
server <-function(input, output, session){
alpha = reactive(input$a)
x <- seq(-4, 4, 0.1)
y1 <- dsn(x, 0, 1.2, alpha)
df <- data.frame(x, y1)
output$plot <-renderPlot({
ggplot(df, aes(x))
geom_line(aes(y=y1), size=1.0, color="black")
}
)
}
shinyApp(ui, server)
這是一個非常簡單的代碼,但我不斷收到此錯誤訊息
Listening on http://xxxxxxxxxxxxxxxx
Warning: Error in abs: non-numeric argument to mathematical function
50: dsn
49: server [#7]
Error in abs(alpha) : non-numeric argument to mathematical function
我一直試圖解決這個問題至少一天,但無濟于事。如果任何好心人能告訴我我做錯了什么,我將不勝感激。
謝謝
莫里斯。
uj5u.com熱心網友回復:
物件 alpha 是反應性的,所以應該用括號呼叫它,alpha()。還因為alpha是反應性的,它需要被反應性背景關系中稱為例如內部observe,reactive或render功能。
應用程式:
library(sn)
library(ggplot)
ui <- fluidPage(
numericInput("a", label = "alpha", min = -3, max = 3, value = 0.0),
plotOutput("plot")
)
server <- function(input, output, session) {
df <- reactive({
alpha <- as.numeric(input$a)
x <- seq(-4, 4, 0.1)
y1 <- dsn(x, 0, 1.2, alpha)
data.frame(x, y1)
})
output$plot <- renderPlot({
ggplot(df(), aes(x))
geom_line(aes(y = y1), size = 1.0, color = "black")
})
}
shinyApp(ui, server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403218.html
標籤:
