2015-01-04 8 views
8

मैं चमकदार के साथ प्रयोग कर रहा हूं और मुझे यह पसंद है। मैंने एक छोटा सा एप्लीकेशन बनाया जहां छात्र एक सीएसवी फ़ाइल अपलोड करते हैं और फिर एक निर्भर चर और स्वतंत्र चर का चयन करते हैं और फिर आर एक रैखिक प्रतिगमन की गणना करता है। यह बढ़िया काम करता है।चमकदार: फ़ाइल अपलोड होने के बाद ही बटन दिखाएं

http://carlosq.shinyapps.io/Regresion

[आप this file उपयोग कर सकते हैं यह परीक्षण करने के लिए अगर आप चाहते हैं: मैं इसे अपलोड पर है। "बियर" निर्भर चर और "आईडी" को छोड़कर चर के बाकी है स्वतंत्र]

हैं यहाँ server.R है:

# server.R 
library(shiny) 

shinyServer(function(input, output) { 

    filedata <- reactive({ 
    infile <- input$file1 
    if (is.null(infile)){ 
     return(NULL)  
    } 
    read.csv(infile$datapath) 
    }) 

    output$dependent <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("dependent","Select ONE variable as dependent variable from:",items) 
    }) 


    output$independents <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE) 
    }) 


    output$contents <- renderPrint({ 
    input$action 
    isolate({ 
     df <- filedata() 
     if (is.null(df)) return(NULL) 
     fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))) 
     summary(lm(fmla,data=df)) 
    }) 
    }) 

}) 

और यहाँ है ui.R:

# ui.R 
library(shiny) 

shinyUI(fluidPage(
    titlePanel("Multiple Linear Regression"), 
    sidebarLayout(
    sidebarPanel(
     fileInput('file1', 'Choose CSV File', 
       accept=c('text/csv', 
         'text/comma-separated-values,text/plain', 
         '.csv')), 

     tags$hr(), 
     uiOutput("dependent"), 
     uiOutput("independents"), 
     tags$hr(), 
     actionButton("action", "Press after reading file and selecting variables") 

    ), 
    mainPanel(
     verbatimTextOutput('contents') 
    ) 
) 
)) 

मेरा सवाल है: मैं एक सफल अपलोडिंग पर सशर्त "बटन पढ़ने के बाद प्रेस और चर का चयन" बटन की उपस्थिति बनाना चाहता हूं।

मैं सुझाव यहां निहित का उपयोग करने की कोशिश की है:

Make conditionalPanel depend on files uploaded with fileInput

लेकिन मैं यह काम नहीं कर सकता।

मैं किसी भी मदद की सराहना करता हूं।

उत्तर

7

इस कोड को मुझे

के लिए काम किया ui.R

# ui.R 
library(shiny) 

shinyUI(fluidPage(
    titlePanel("Multiple Linear Regression"), 
    sidebarLayout(
    sidebarPanel(
     fileInput('file1', 'Choose CSV File', 
       accept=c('text/csv', 
         'text/comma-separated-values,text/plain', 
         '.csv')), 

     tags$hr(), 
     uiOutput("dependent"), 
     uiOutput("independents"), 
     tags$hr(), 
     uiOutput('ui.action') # instead of conditionalPanel 
    ), 
    mainPanel(
     verbatimTextOutput('contents') 
    ) 
) 
)) 

server.R

# server.R 
library(shiny) 

shinyServer(function(input, output) { 

    filedata <- reactive({ 
    infile <- input$file1 
    if (is.null(infile)){ 
     return(NULL)  
    } 
    read.csv(infile$datapath) 
    }) 

    output$dependent <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("dependent","Select ONE variable as dependent variable from:",items) 
    }) 


    output$independents <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE) 
    }) 


    output$contents <- renderPrint({ 
    input$action 
    isolate({ 
     df <- filedata() 
     if (is.null(df)) return(NULL) 
     fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))) 
     summary(lm(fmla,data=df)) 
    }) 
    }) 


    output$ui.action <- renderUI({ 
    if (is.null(input$file1)) return() 
    actionButton("action", "Press after reading file and selecting variables") 
    }) 

}) 
+0

धन्यवाद मारत। मैंने आपके समाधान की कोशिश की। यह बटन गायब हो जाता है ... और यह अच्छा है। लेकिन यह फ़ाइल अपलोड करने के बाद प्रकट नहीं होता है। मैं आपके सर्वर को asumme.R फ़ाइल में एक पंक्ति शामिल है जो जांचता है कि फ़ाइल सफलतापूर्वक अपलोड की गई है या नहीं। – user23438

+0

@ user23438, मैं 'conditionPanel' का उपयोग करके समाधान प्राप्त करने में सक्षम नहीं था, क्योंकि मुझे नहीं पता था कि' स्थिति 'को सही तरीके से कैसे सेट अप किया जाए। मैंने जवाब संपादित किया, जो अब uiOutput पर आधारित है। –

+0

फिर से धन्यवाद आपके समय के लिए मारत। आपका नया समाधान मुझे समाधान के करीब ले जाता है। अब बटन सही पल में दिखाई देता है, लेकिन यह मेरे आउटपुट $ सामग्री के साथ एक समस्या पैदा करता है। इससे पहले कि मेरे पास "पृथक ({})" में लिपटे इस खंड में कोड था जो इनपुट $ एक्शन द्वारा सक्रिय किया गया था। अब इनपुट $ एक्शन चला गया है क्योंकि बटन चला गया है। मैं अलग से छुटकारा पा सकता हूं लेकिन फिर जब तक सही चर का चयन नहीं किया जाता है तब तक यह कचरा प्रिंट करता है। – user23438

9

यहाँ working ShinyApp और दोनों ui.R और server.R के अंतिम संस्करण आधारित है मारत द्वारा प्रदान किए गए सभी सुझावों पर।

पहले ui.R

# ui.R 

library(shiny) 

shinyUI(fluidPage(
    titlePanel("Multiple Linear Regression with R/Shiny"), 
    sidebarLayout(
    sidebarPanel(
     p("Please upload a CSV formatted file with your data."), 
     fileInput('file1', label='Click button below to select the file in your computer.', 
       accept=c('text/csv', 
         'text/comma-separated-values,text/plain', 
         '.csv')), 

     tags$hr(), 
     uiOutput("dependent"), 
     uiOutput("independents"), 
     tags$hr(), 
     uiOutput('ui.action') # instead of conditionalPanel 
    ), 
    mainPanel(
     p("Here's the output from your regression:"), 
     verbatimTextOutput('contents') 
    ) 
) 
)) 

और server.R

# server.R 

library(shiny) 

shinyServer(function(input, output) { 

    filedata <- reactive({ 
    infile <- input$file1 
    if (is.null(infile)){ 
     return(NULL)  
    } 
    read.csv(infile$datapath) 
    }) 

    output$ui.action <- renderUI({ 
    if (is.null(filedata())) return() 
    actionButton("action", "Run regression") 
    }) 

    output$dependent <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("dependent","Now select ONE variable as dependent variable from:",items) 
    }) 


    output$independents <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("independents","Also select ONE or MANY independent variables in the box below. You can change your selection several times:",items,multiple=TRUE) 
    }) 


    output$contents <- renderPrint({ 
    if (is.null(input$action)) return() 
    if (input$action==0) return() 
    isolate({ 
     df <- filedata() 
     if (is.null(df)) return(NULL) 
     fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))) 
     summary(lm(fmla,data=df)) 
    }) 
    }) 


}) 

एक बार फिर आपकी मदद के मरात के लिए धन्यवाद।

+0

आपका स्वागत है! –

संबंधित मुद्दे

 संबंधित मुद्दे