【问题标题】:R: Use csv file as cbind format [closed]R:使用 csv 文件作为 cbind 格式 [关闭]
【发布时间】:2017-07-20 20:23:37
【问题描述】:

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

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)

  })

我从闪亮应用程序中的输入中读取 csv 文件,例如 `fileInput("file", "File"),

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

非常感谢您的帮助。 `

【问题讨论】:

  • 如果你要访问你读到的数据,我想用filedata_DM()就行了?
  • 谢谢!但不起作用!问题是如何将 csv 文件转换为 cbind 函数中呈现的形式(使用 csv 数据作为向量)
  • 由于我们没有 csv 文件的示例,因此很难判断。
  • 我编辑了问题,请看代码

标签: r csv shiny


【解决方案1】:

这对我有用:

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() 返回的对象不是数据框。

【讨论】:

    【解决方案2】:

    谢谢你们!最后,它通过使用以下代码来工作:

      #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)
    
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多