Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-矩阵上二元运算符的非数值参数_R_Matrix_Input_Shiny - Fatal编程技术网

R-矩阵上二元运算符的非数值参数

R-矩阵上二元运算符的非数值参数,r,matrix,input,shiny,R,Matrix,Input,Shiny,我对r(以及一般的编程)非常陌生,我们被要求使用r制作一个web应用程序,计划是从不同的输入文件制作一个矩阵运算计算器,(第一个.cvs文件包含矩阵1,第二个.cvs文件包含矩阵2),但错误不断出现 Listening on http://127.0.0.1:3420 Warning: Error in FUN: non-numeric argument to binary operator 99: eval 98: eval 97: Ops.data.frame 96: rende

我对r(以及一般的编程)非常陌生,我们被要求使用r制作一个web应用程序,计划是从不同的输入文件制作一个矩阵运算计算器,(第一个.cvs文件包含矩阵1,第二个.cvs文件包含矩阵2),但错误不断出现

 Listening on http://127.0.0.1:3420
 Warning: Error in FUN: non-numeric argument to binary operator
 99: eval
 98: eval
 97: Ops.data.frame
 96: renderTable [C:/Users/Acer/Desktop/FirstWebApp/app (1).R#45]
 95: func
 82: origRenderFunc
 81: output$oput
  1: runApp
这是我的用户界面

     ui <- fluidPage(
  titlePanel("Multiple file uploads"),
  sidebarLayout(
    sidebarPanel(
     fileInput("file1",
               label="Upload CSVs here"),
     fileInput("file2", "upload file here"),
     selectInput("ops", "Select Operation",
                 choices = c("addition","subtraction","multiplication","division"))
     
  ),
  mainPanel(
     tableOutput("input_file"),
     tableOutput("input_file2"),
     tableOutput("oput")

如何修复此错误?如果此错误已修复,程序是否会运行?

您的
输入$file1
输入$file2
将包含文件名和路径,但不包含数据本身(它将通过
读取表
在其他位置读取)。因此,除非你提供数据,否则矩阵运算不会起作用

<>我建议考虑使用<代码> Actudio< /Calp>表达式来访问CSV文件中的数据。虽然许多教程将准确地演示您所拥有的内容(在
输出中直接读取数据
可渲染
),但在其他上下文中使用数据时,这可能不够灵活

下面,反应式表达式
data1
将读取存储在
input$file1
中的csv文件。然后,要访问数据,只需将其称为
data1()
。这包括
输出$table1
(仅在表格中显示数据),以及
output$table3
以显示矩阵运算的结果。请注意,在下面的示例中,我将
tableOutput
重命名为“table1”、“table2”和“table3”

我希望这能澄清一些事情——让我知道任何问题

library(shiny)

ui <- fluidPage(
  titlePanel("Multiple file uploads"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", label = "Upload CSV 1"),
      fileInput("file2", label = "Upload CSV 2"),
      selectInput("ops", "Select Operation",
                  choices = c("addition", "subtraction", "multiplication", "division"))
    ),
    mainPanel(
      tableOutput("table1"),
      tableOutput("table2"),
      tableOutput("table3")
    )
  )
)

server <- function(input, output) {
  
  data1 <- reactive({
    file_to_read =  input$file1
    if (is.null(file_to_read)) {
      return()
    }
    read.table(file_to_read$datapath, sep = ',', header = FALSE)
  })
  
  output$table1 <- renderTable({
    data1()
  })
  
  data2 <- reactive({
    file_to_read =  input$file2
    if (is.null(file_to_read)) {
      return()
    }
    read.table(file_to_read$datapath, sep = ',', header = FALSE)
  })
  
  output$table2 <- renderTable({
    data2()
  })
  
  output$table3 <- renderTable({
    switch(input$ops,
           "addition" = data1() + data2(),
           "subtraction" = data1() - data2(),
           "multiplication" = data1() * data2(),
           "division" = data1() / data2())
  })
}

shinyApp(ui, server)
库(闪亮)
用户界面
library(shiny)

ui <- fluidPage(
  titlePanel("Multiple file uploads"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", label = "Upload CSV 1"),
      fileInput("file2", label = "Upload CSV 2"),
      selectInput("ops", "Select Operation",
                  choices = c("addition", "subtraction", "multiplication", "division"))
    ),
    mainPanel(
      tableOutput("table1"),
      tableOutput("table2"),
      tableOutput("table3")
    )
  )
)

server <- function(input, output) {
  
  data1 <- reactive({
    file_to_read =  input$file1
    if (is.null(file_to_read)) {
      return()
    }
    read.table(file_to_read$datapath, sep = ',', header = FALSE)
  })
  
  output$table1 <- renderTable({
    data1()
  })
  
  data2 <- reactive({
    file_to_read =  input$file2
    if (is.null(file_to_read)) {
      return()
    }
    read.table(file_to_read$datapath, sep = ',', header = FALSE)
  })
  
  output$table2 <- renderTable({
    data2()
  })
  
  output$table3 <- renderTable({
    switch(input$ops,
           "addition" = data1() + data2(),
           "subtraction" = data1() - data2(),
           "multiplication" = data1() * data2(),
           "division" = data1() / data2())
  })
}

shinyApp(ui, server)