我正在尝试制作一个相当简单的闪亮应用程序,其中用户上传数据集,选择结果变量和因变量,然后能够执行简单的线性回归并显示该回归的结果表。
我对此有疑问,不确定是什么问题。尝试显示表格时,我收到一条错误消息。
代码如下:
library(shiny)
library(tidyverse)
title<- titlePanel("Team Allocation Data Cleaning")
ui_upload <- sidebarLayout(
sidebarPanel(
fileInput("file", "Data", buttonLabel = "Upload..."),
textInput("delim", "Delimiter (leave blank to guess)", ""),
numericInput("skip", "Rows to skip", 0, min = 0),
numericInput("rows", "Rows to preview", 5, min = 1),
selectInput("outcome", "Select the Outcome Variable", choices = "no choices available"),
checkboxGroupInput("dependent", "Select the Dependent Variable(s)", choices="no choices available")
),
mainPanel(
actionButton("analysis", "Analyze"),
tabsetPanel(
tabPanel("Original Data", tableOutput("preview1")),
tabPanel("Selected Data", tableOutput("preview2")),
tabPanel("Model Results", verbatimTextOutput("modelsummary"))
)
)
)
ui <- fluidPage(
title,
ui_upload
)
server <- function(input, output, session) {
# Upload ---------------------------------------------------------------
raw <- reactive({
req(input$file)
delim <- if (input$delim == "") NULL else input$delim
vroom::vroom(input$file$datapath, delim = delim, skip = input$skip)
})
#Preview the Data--------------------------------------------------------
output$preview1 <- renderTable(head(raw(), input$rows))
#Determine the outcome variable
observe({
updateSelectInput(session, "outcome", "Select the Outcome Variable:", choices = colnames(raw()))
updateCheckboxGroupInput(session, "dependent", "Select the Dependent Variable(s)", choices = colnames(raw()))
})
selected<- reactive({
#we are going to select the two dataframes and then piece them back togeth... that is the plan at least
outcome_col<- select(raw(), input$outcome)
dependent_col <- select(raw(), input$dependent)
data<- cbind(outcome_col, dependent_col)
#remove the nas from the new data
data<- na.omit(data)
return(data)
})
output$preview2<- renderTable(head(selected(), input$rows))
mod<- eventReactive(input$analysis, {
lm(as.formula(paste(colnames(selected())[1], "~",
paste(colnames(selected())[c(2:ncol(selected))], collapse = "+"),
sep = "")), data = selected())
})
output$modelsummary<- renderPrint({
summary(mod())
})
}
# Run the application
shinyApp(ui = ui, server = server)
我一直在使用csv形式的基本的泰坦尼克号数据集进行测试(仅选择数字变量)。旁注,我知道结果是没有意义的,因为数据集的结果是分类,因此我应该使用逻辑模型,但是出于这个问题的目的应该没关系。
我不确定问题是什么,任何澄清/解决方案将非常有帮助!