【发布时间】:2023-03-13 06:54:01
【问题描述】:
我正在 R 中练习闪亮的包。我正在创建一个应用程序,用户在其中选择两个变量并确定要适合相应散点图的曲线的程度。为此,我正在使用 mtcars 数据。我使用 selectinput 命令来获取变量。我希望滑块输入命令来决定拟合曲线的程度。除了滑块输入命令之外,代码似乎都在工作。
library(shiny)
ui <- fluidPage(
headerPanel('Fitting a curve'),
sidebarPanel(
selectInput(inputId = "xcol",label = "X-Axis",choices = names(mtcars)),
selectInput(inputId = "ycol",label = "Y-Axis",choices = names(mtcars),selected = names(mtcars)[[3]]),
sliderInput(inputId = "degree",label = "Degree of fit",min = 0,max = 2,value = 0)
),
mainPanel(plotOutput("plot1"))
)
server <- function(input,output){
x <- reactive({mtcars[,input$xcol]})
y <- reactive({mtcars[,input$ycol]})
z <- renderPrint({ifelse(input$degree==0,lm(y()~),ifelse(input$degree==1,lm(y()~x()),lm(y()~x()+x()^2)))})
output$plot1 <- renderPlot({
plot(x(),y(),col = "red")
abline(z())
})
}
shinyApp(ui = ui,server = server)
很确定服务器部分的“z”行有错误。请帮助我是闪亮包的新手。
【问题讨论】:
-
您可以通过
lm(y()~1)仅使用拦截来适应