Reputation: 21
I am creating a appshiny to analyze a statistical model. In it the user places the data set and the model is returned. I would like to implement the removal of non-significant variables, but for that I need the variable names of my file to return to selectInput, is it possible to do this?
library(shiny)
ui <- fluidPage(
titlePanel("-"),
sidebarLayout(
sidebarPanel(
fileInput(inputId="arquivo", "Selecione o arquivo",accept = ".csv"),
selectInput(inputId = "insi", "Selecione as variaveis não significativas", choices = names(k) ,multiple = TRUE )
),
mainPanel(
tabsetPanel(type = "tab",
tabPanel("Dados",tableOutput("dados")),
tabPanel("Ajuste", verbatimTextOutput("model"))
)
)
)
)
server <- function(input, output) {
output$dados <- renderTable({
if(is.null(input$arquivo)) return(NULL)
k <- read.csv(input$arquivo$datapath, header = TRUE, stringsAsFactors = FALSE)
if(input$header==FALSE)
{
return(k)
}
if (input$header==TRUE)
{
return(head(k))
}
})
output$model <- renderPrint({
if(is.null(input$arquivo)) return(NULL)
k <- read.csv(input$arquivo$datapath, header = TRUE, stringsAsFactors = FALSE)
v <- ncol(k)
y <- k[, 1]
h <- k[, -1]
mData <- data.frame(vY = y, mX = h)
colnames(mData) <- c("vY", paste("VX", 2:v, sep = ""))
mod <- glm(vY ~ ., data = mData, family = binomial)
print(summary(mod))
})
}
shinyApp(ui = ui, server = server)
Upvotes: 2
Views: 29
Reputation: 2725
See my comment above and this first answer is not ideal but should work for your purposes (see below for improved answer):
library(shiny)
ui <- fluidPage(
titlePanel("-"),
sidebarLayout(
sidebarPanel(
fileInput(inputId="arquivo", "Selecione o arquivo",accept = ".csv"),
selectInput(inputId = "insi", "Selecione as variaveis não significativas", choices = "" ,multiple = TRUE )
),
mainPanel(
tabsetPanel(type = "tab",
tabPanel("Dados",tableOutput("dados")),
tabPanel("Ajuste", verbatimTextOutput("model"))
)
)
)
)
server <- function(input, output, session) {
output$dados <- renderTable({
if(is.null(input$arquivo)) return(NULL)
k <- read.csv(input$arquivo$datapath, header = TRUE, stringsAsFactors = FALSE)
updateSelectInput(session = session, inputId = "insi", choices = names(k))
return(k)
#if(input$header==FALSE)
#{
# return(k)
#}
#if (input$header==TRUE)
#{
# return(head(k))
#}
})
output$model <- renderPrint({
if(is.null(input$arquivo)) return(NULL)
k <- read.csv(input$arquivo$datapath, header = TRUE, stringsAsFactors = FALSE)
v <- ncol(k)
y <- k[, 1]
h <- k[, -1]
mData <- data.frame(vY = y, mX = h)
colnames(mData) <- c("vY", paste("VX", 2:v, sep = ""))
mod <- glm(vY ~ ., data = mData, family = binomial)
print(summary(mod))
})
}
shinyApp(ui = ui, server = server)
Update - improved answer
library(shiny)
ui <- fluidPage(
titlePanel("-"),
sidebarLayout(
sidebarPanel(
fileInput(inputId="arquivo", "Selecione o arquivo",accept = ".csv"),
selectInput(inputId = "insi", "Selecione as variaveis não significativas", choices = "" ,multiple = TRUE )
),
mainPanel(
tabsetPanel(type = "tab",
tabPanel("Dados",tableOutput("dados")),
tabPanel("Ajuste", verbatimTextOutput("model"))
)
)
)
)
server <- function(input, output, session) {
k <- reactive({
if(is.null(input$arquivo)) return(NULL)
k <- read.csv(input$arquivo$datapath, header = TRUE, stringsAsFactors = FALSE)
updateSelectInput(session = session, inputId = "insi", choices = names(k))
return(k)
})
output$dados <- renderTable({
req(k())
})
output$model <- renderPrint({
k <- req(k())
v <- ncol(k)
y <- k[, 1]
h <- k[, -1]
mData <- data.frame(vY = y, mX = h)
colnames(mData) <- c("vY", paste("VX", 2:v, sep = ""))
mod <- glm(vY ~ ., data = mData, family = binomial)
print(summary(mod))
})
}
shinyApp(ui = ui, server = server)
The main edits:
choices = ""
in the ui and then using updateSelectInput()
once names(k)
exists.session
to server.req()
.Please ask if anything needs further explaining.
Upvotes: 2