如何解决在带有用户选择的列的闪亮应用程序中使用自动绘图
我正在制作一个闪亮的应用程序,允许用户上传 CSV,然后选择自变量和因变量。现在我可以上传文件,选择变量并运行回归分析。但是,我被困在将 lm
对象传递给 autoplot
然后在新选项卡中通过 autoplotly
使其交互的步骤。如何通过在闪亮的应用中使用用户选择的变量来创建交互式回归图?
界面
ui = navbarPage(tabPanel("Regression Analysis",dataTableOutput('mytable'),sidebarLayout(
sidebarPanel(width=3,fileInput("file1","Please choose a CSV file",multiple = T,accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),tags$hr(),checkBoxInput("header","Header",TRUE),radioButtons("sep","Separator",choices = c(Comma = ",",Semicolon = ";",Tab = "\t"),selected = ","),radioButtons("quote","Quote",choices = c(None = "","Double Quote" = '"',"Single Quote" = "'"),selected = '"'),radioButtons("disp","display",choices = c(Head = "head",All = "all"),selected = "head")
),mainPanel(
tableOutput("contents"),actionButton("choice","Define Regression Variables"),selectInput("independent","Independent Variables:",choices = NULL,multiple = T),uIoUtput("dependent1"),#tableOutput("Table_selected.col"),verbatimtextoutput("regTab")
)
),tabPanel("Plots",icon = icon("chart-area"),plotlyOutput(outputId = "RegPlots"))
)
服务器
server = function(input,output,session) {
mydf <- reactive({
# input$file1 will be NULL initially. After the user selects
# and uploads a file,head of that data file by default,# or all rows if selected,will be shown.
req(input$file1)
df = read.csv(input$file1$datapath,header = input$header,sep = input$sep,quote = input$quote)
if(input$disp == "head") {
return(head(df))
}
else {
return(df)
}
})
output$contents = renderTable({
req(mydf())
mydf()
})
# Code for allowing the user to select the variables/columns of interest
info <- eventReactive(input$choice,{
req(mydf())
f <- mydf()
f
})
observeEvent(input$choice,{ ## to update only when you click on the actionButton
req(info())
updateSelectInput(session,"independent","Please Select independent Variable(s):",choices = names(info()) )
})
# output$Table_selected.col <- renderTable({
# input$choice
# req(info(),input$columns)
# f = info()
# f = subset(f,select = input$columns) #subsetting takes place here
# head(f)
# })
output$dependent1 = renderUI({
req(mydf(),input$independent)
radioButtons("dependent1","Select a dependent Variable:",choices=names(mydf())[!names(mydf()) %in% input$independent])
})
### need to build your formuila correctly; It will work with multiple independent variables
### model <- reactive({lm(reformulate(input$IndVar,input$DepVar),data = RegData)})
runRegression <- reactive({
req(mydf(),input$independent,input$dependent1)
a = lm(reformulate(input$independent,input$dependent1),data=mydf())
a
# multinom(reformulate(input$independent,data=mydf()) ### mulitnomial from nnet package
})
output$regTab = renderPrint({
req(runRegression())
if(!is.null(input$independent)){
summary(runRegression())
} else {
print(data.frame(Warning="Please select Model Parameters."))
}
})
}
output$RegPlots = renderPlotly({
req(runRegression())
# Plot the residuals
lm.plot.rsd = autoplot(a,label.size = 3,which = 1) +
theme_bw()
autoplotly(lm.plot.rsd +
ggplot2::ggtitle("Residuals vs Fitted"))
})
shinyApp(ui,server)
Error in : Objects of type function not supported by autoplot.
解决方法
试试这个
output$RegPlots = renderPlot({
req(runRegression())
# Plot the residuals
a = runRegression()
ggplot(a,aes(x = .fitted,y = .resid)) +
geom_point() +
geom_smooth(method = loess,formula = y ~ x) +
labs(title="Residuals vs Fitted")
})
如果你愿意,你可以尝试其他情节。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。