2016-12-14 24 views
1

私はRShinyでアプリケーションを開発しようとしています。 私の目的: ロジスティック回帰を行い、出力を表示できるアプリケーションです。Rによるロジスティック回帰Shiny

ステップ:CSV(第一TAB)

  • ユーザーをアップロードします

    1. ユーザーは、(第二TAB)を他の変数を選択します(第二TAB)
    2. ユーザーの独立変数を選択し
    3. Mainpanelで2番目のTABにロジスティック回帰の概要が表示されます。 回帰。

    マイコード:

    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 Upoload部分ティルは完了です。 glm()関数reqのような問題に直面していた問題:glm(var 1〜var 2 + var 3 + var 4、family = binomial、data = df)

    var 2+ varのようなチェックボックスの値はどうすれば使用できますか3 ..? 私は最後の1週間からShiny Rを使用しています。私が見つけることができない簡単な解決策があるかもしれません。私はあなただけそれを完了するために、行のカップルを必要とそれを実行しようとしていたものを理解したら、事前

  • +0

    予備回答がありましたが、変更しました。これはおそらくあなたが望むものです。 –

    答えて

    2

    おかげであなたは、非常に近かったです。これは、光沢のあるフレームにデータフレームをロードし、そのフレームの列をglmに選択し、それを実行するという素晴らしい例です。

    glmに文字列変数を使用するには、as.forumla関数を使用します。エリック・グリーンの例http://stackoverflow.com/questions/17024685/how-to-use-a-character-string-in-formulaを参照してください

    私はあなたのプログラムの周り混乱し、これが動作するようになった - 私はRANDUデータセット(write.csv(randu,"randu.csv")使用 - それは仕事に0と1の間の値を持つ列を必要とする点に注意してください。

    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) 
    

    ここでは、データがrandu.csv enter image description here

    からロードされ、ここで設定可能glmです:

    enter image description here

    +0

    あなたのコードとガイダンスのためにありがとうマイク...出力$レンダリンセクションセクションにいくつかの行を追加しました........ 'if(is.null(input $ other_var_select)){"あなたは独立したもの以外の一つの可変altleast "他 { F <-data() ライブラリ(キャレット) 形態<} - はsprintf(" %S〜%のS」入力の$ ind_var_select、paste0(入力の$ other_var_select、崩壊= "+")) プリント(形) logreg <-glm(as.formula(フォーム)、家族=二項()、データ= F) プリント(要約(logreg)) } ' – Subhasish1315

    +0

    @MikeWise ..私はここに小さな質問があります... logregはロジスティック回帰出力を保持します..私は別の出力関数で使うことができますか変数の重要性のためにvarimpのような別のプロパティを表示することができますか?ロジスティック回帰を再構築する必要がありますか? – Subhasish1315

    +1

    "<< - "演算子を使ってグローバルにするのが便利です。その後、それを再利用することができます。 –

    関連する問題