我正在尝试在 RShiny 中开发一个应用程序。 我的目标: 一个可以进行逻辑回归并显示输出的应用程序。
步骤:
- 用户将上传 CSV(第一个选项卡)
- 用户选择自变量(第二个选项卡)
- 用户选择其他变量(第二个选项卡)
- 第二个选项卡中的主面板将显示物流摘要 回归。
我的代码:
library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
tabPanel("Data Import",sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(uiOutput("tb1"))
) ),
tabPanel("Model_dev",sidebarLayout(sidebarPanel(uiOutput("model_select"),uiOutput("var1_select"),uiOutput("rest_var_select")),mainPanel( helpText("Your Selected variables"),verbatimTextOutput("other_val_show"))))
)
server<-function(input,output) { data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
output$tb1 <- renderUI({
tableOutput("table")
})
output$model_select<-renderUI({
selectInput("modelselect","Select Algo",choices = c("Logistic_reg"="logreg","SVM"="svm"))
})
output$var1_select<-renderUI({
selectInput("ind_var_select","Select Independent Var", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
checkboxGroupInput("other_var_select","Select other Var",choices =as.list(names(data())))
})
output$other_val_show<-renderPrint({
input$other_var_select
#f<-data()
#library(caret)
#logreg<-glm(f[,1]~.,family = binomial,data=f)
#summary(logreg)
})
}
shinyApp(ui=ui,server=server)
到目前为止,CSV 上传部分已完成。 glm() 函数 req 结构面临的问题如下: glm(var 1~ var 2+var 3+ var 4,family=二项式,data=df)
如何使用 var 2+ var 3.. 等复选框值? 我从过去 1 周起就开始使用 Shiny R。所以可能有我无法发现的更简单的解决方案。
提前致谢
最佳答案
你非常接近,一旦我明白你想要做什么,只需要几行就可以完成它。这是一个很好的例子,将数据帧加载到 Shiny 中,为 glm
选择该帧中的列,然后执行它。
您可以通过使用 as.forumla
函数在 glm
中使用字符串变量。请参阅 Eric Green 的示例 http://stackoverflow.com/questions/17024685/how-to-use-a-character-string-in-formula ,
我搞乱了你的程序并让它工作 - 请注意它需要一个值在 0 和 1 之间的列才能工作 - 我使用了 randu 数据集 (write.csv(randu,"randu.csv"
):
library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
tabPanel("Data Import",
sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
radioButtons(inputId = 'sep', label = 'Separator',
choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(uiOutput("tb1"))
) ),
tabPanel("Model_dev",
sidebarLayout(sidebarPanel(
uiOutput("model_select"),
uiOutput("var1_select"),
uiOutput("rest_var_select")),
mainPanel( helpText("Your Selected variables"),
verbatimTextOutput("other_val_show"))))
)
server<-function(input,output) { data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
output$tb1 <- renderUI({
tableOutput("table")
})
output$model_select<-renderUI({
selectInput("modelselect","Select Algo",choices = c("Logistic_reg"="logreg","SVM"="svm"))
})
output$var1_select<-renderUI({
selectInput("ind_var_select","Select Independent Var", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
checkboxGroupInput("other_var_select","Select other Var",choices =as.list(names(data())))
})
output$other_val_show<-renderPrint({
input$other_var_select
input$ind_var_select
f<-data()
library(caret)
form <- sprintf("%s~%s",input$ind_var_select,paste0(input$other_var_select,collapse="+"))
print(form)
logreg <-glm(as.formula(form),family=binomial(),data=f)
print(summary(logreg))
})
}
shinyApp(ui=ui,server=server)
这是可配置的glm
:
关于r - 通过 R Shiny 进行逻辑回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141157/