'Shiny render glm.fit is not working, the categorical-response cannot be truly readable
I created a shiny app for logistic regression model.
My idea is allow user define formular by typing in textInput box ex. 'Response ~ Pred1 + Pred2' and then convert to formula by function 'as.formula(paste0(input$formula))'. It works in RStudio, but not work in shinydashboard render. The error is 'y values must be 0 <= y <= 1'. Any body help?
This work well in Rstudio
s <- 'Direction ~ Lag1+ Lag2'
model <- glm(as.formula(paste0(s)),
data = Smarket,
family = binomial,
subset = train_data$id)
But in shinydashboard, the formula render from textInput box is not working. My code below
My ui.R
library(tidyverse)
library(shiny)
library(shinydashboard)
library(ISLR2)
ui <- dashboardPage(
dashboardHeader(title = 'Classification'),
dashboardSidebar(width = '275',
sidebarMenu(
menuItem('Instruction', tabName = 'instruction', icon = icon('hammer', lib = 'font-awesome')),
menuItem('Table & Correlation', tabName = 'table', icon = icon('table', lib = 'font-awesome')),
menuItem('DS', tabName = 'ds', icon = icon('desktop', lib = 'font-awesome')),
fileInput(inputId = 'f_input', label = 'Upload File'),
div(class='col-lg-12 col-md-12',
hr()),
# Correlation
uiOutput(outputId = 'corre'),
actionButton(inputId = 'button1', label = 'Correlation'),
div(class='col-lg-12 col-md-12',
hr()),
# Train-Test
sliderInput("data_split", label = "Percent Train", min = 0,
max = 100, value = 70, step = 5),
# Fit Model
textInput(inputId = 'formula',
label = 'Creat formula',
placeholder = 'y ~ X1 + X2 + ...'),
actionButton(inputId = 'button2', label = 'Run Model'),
div(class='col-lg-12 col-md-12',
hr())
)
),
dashboardBody(
tabItems(
tabItem(tabName = 'instruction'),
tabItem(tabName = 'table',
fluidRow(
DT::dataTableOutput('view_table')
),
fluidRow(
plotOutput('corr_plot')
)),
tabItem(tabName = 'ds',
fluidRow(
box(verbatimTextOutput('summary')),
box(plotOutput('conf.matrix'))
)
)
)
)
)
My server.R
server <- function(input, output, session){
### RAW MATERIALS
#upload csv file
data <- reactive({
file <- input$f_input
data <- if(!is.null(file)){
read.csv(file$datapath)
} %>%
as_tibble()%>%
mutate(id = row_number())
})
#render RAW table
output$view_table <- DT::renderDataTable(
data(),
options = (list(scrollX = TRUE))
)
###-----------------------------------------
### CORRELATION
#user select Xi to generate correlation plot
output$corre <- renderUI({
data <- data()
if(!is.null(data)){
CHOICES <- names(data)
shinyWidgets::pickerInput('var',
label = 'Select to generated correlation of variables',
choices = CHOICES,
options = list(`actions-box` = TRUE),
multiple = TRUE)
}
})
#filter table matched selection from pickerInput
df <- eventReactive(input$button1, {
data <- data()
if(!is.null(data)){
data %>%
select(dplyr::matches(input$var))
}
})
#corr plot
output$corr_plot <-renderPlot({
df() %>%
ggcorr()
})
###-----------------------------------------
### TRAIN-TEST DATA SPLIT
i <- reactive({
set.seed(112)
data() %>%
rsample::initial_split(prop = as.numeric(input$data_split)/100)
})
###-----------------------------------------
### FIT MODEL
model <- eventReactive(input$button2, {
train_data <- rsample::training(i())
test_data <- rsample::testing(i())
##Logistic Regression
glm(as.formula(paste0(input$formula)),
data = data(),
family = binomial,
subset = train_data$id) #parse index of train
})
output$summary <- renderPrint({
if(!is.null(model())){
summary(model())
}
})
}
shinyApp(ui = ui, server = server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

