R:使用csv文件作为cbind格式

时间:2017-07-20 20:23:38

标签: r csv shiny

这涉及Shiny应用程序我有这个代码: 数据模型是:



2	3	4	4	2
6	2	3	1	0
4	1	1	0	4
3	2	2	3	5
2	3	5	4	0
1	2	2	2	1
5	5	0	3	4




>   #This function is repsonsible for loading in the selected file  
> filedata_DM <- reactive({
>     infile <- input$datafile
>     if (is.null(infile)) {
>       # User has not uploaded a file yet
>       return(NULL)
>     }
>     read.csv(infile$datapath,header=FALSE)   })
>      performanceMatrix <- cbind(
>     c(-120.0,-150.0,-100.0,-60,-30.0,-80,-45.0),
>     c(-284.0,-269.0,-413.0,-596,-1321.0,-734,-982.0),             
>     c(5.0,2.0,4.0,6,8.0,5,7.0),                   
>     c(3.5,4.5,5.5,8,7.5,4,8.5),       
>     c(18.0,24.0,17.0,20,16.0,21,13.0)   )




# results
  output$filetable_result <- renderPrint({
    Electre_tri(performanceMatrix,
                        alternatives,
                        profiles,
                        profiles_names,
                        criteria,
                        minmaxcriteria,
                        criteriaWeights,
                        IndifferenceThresholds,
                        PreferenceThresholds,
                        VetoThresholds,
                        lambda=NULL)

  })

我从闪亮的app中输入读取csv文件,例如`fileInput(&#34; file&#34;,&#34; File&#34;),

现在我想在cbind函数中使用csv数据(表)而不是给定数字!

非常感谢帮助。 `

2 个答案:

答案 0 :(得分:0)

这对我有用:

library(shiny)

ui <- fluidPage(
   sidebarLayout(
     shiny::fileInput("datafile", 'Choose CSV File'),
      mainPanel(
         tableOutput("filetable_result")
      )
   )
)

server <- function(input, output) {

  filedata_DM <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    read.csv(infile$datapath,header=FALSE)   
  })

  output$filetable_result <- renderTable(filedata_DM())
}

# Run the application 
shinyApp(ui = ui, server = server)

您需要访问使用filedata_DM()阅读的数据,因为它是被动的。因此,您不应将其放在变量中并将此变量用作数据框,因为reactive()会返回一个不是数据框的对象。

答案 1 :(得分:0)

谢谢你们!最后,它使用此代码:

  #This function is repsonsible for loading in the selected file
  filedata_DM <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    read.csv(infile$datapath,header=TRUE)
  })


  # statistics
  output$filetable_result <- renderPrint({
    data2=filedata_DM()
    performanceMatrix <- cbind(
      c(data2$v1),
      c(data2$v2),              
      c(data2$v3),                  
      c(data2$v4),      
      c(data2$v5)
    )
    Electre_tri(performanceMatrix,
                        alternatives,
                        profiles,
                        profiles_names,
                        criteria,
                        minmaxcriteria,
                        criteriaWeights,
                        IndifferenceThresholds,
                        PreferenceThresholds,
                        VetoThresholds,
                        lambda=NULL)

  })