我正在尝试设置一个Shiny应用程序,并在MASS包中的menarche数据集上运行以下代码:
##ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Probability of menarche among adolescent girls in Warsaw, 1965"),
sidebarLayout(
sidebarPanel(
sliderInput("men_Age", "What is the age of menarche?",
9, 18, value = 12),
submitButton("Submit")
),
mainPanel(
plot("plot1"),
h3("Probability of menarche:"),
textOutput("pred1")
)
)
))
##server.R
library(shiny)
library(MASS)
data(menarche)
shinyServer(function(input, output) {
mprob <- glm(cbind(Menarche, Total-Menarche) ~ Age,
family = binomial(link = probit), data = menarche)
modelPred <- reactive({
menInput <- input$men_Age
x <- predict(mprob, newdata = data.frame(Age = menInput))
exp(x)/(1+exp(x))
})
output$plot1 <- renderPlot({
menInput <- input$men_Age
plot(menarche$Age, menarche$Menarche / menarche$Total,
xlab = "Age of menarche onset", ylab = "Pr(Menarche)",
bty = "n", pch = 16, xlim = c(9, 18), ylim = c(0, 1))
lines(menarche$Age, mprob$fitted, type = "l", col = "blue", lwd=2)
points(menInput, modelPred(), pch = 16, col = "blue", cex = 2)
})
output$pred1 <- renderText({
modelPred()
})
})
我已单独运行绘图代码并看到它有效。但是,当我尝试运行该应用程序时,我收到的错误是&#34;需要有限的&#39; ylim&#39;值&#34 ;.我设置了有限的ylim值,所以我不知道发生了什么。我读过其他一些提到NA值的帖子,但是月经初潮数据集没有任何NA值。看起来他们根据这个输出被强迫:
Warning in xy.coords(x, y, xlabel, ylabel, log) :
NAs introduced by coercion
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning: Error in plot.window: need finite 'ylim' values
Stack trace (innermost first):
57: plot.window
56: localWindow
55: plot.default
54: plot
53: tag
52: tags$div
51: div
50: mainPanel
49: sidebarLayout
48: tag
47: tags$div
46: div
45: tagList
44: attachDependencies
43: bootstrapPage
42: fluidPage
41: shinyUI
1: runApp
Error : need finite 'ylim' values
但是从ui和服务器文件中删除对绘图的任何引用都可以让应用程序顺利运行。有谁知道我可能缺少什么?