我是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
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")
我server
看起来像这样
server <- function(input, output) {
output$input_file <- renderTable({
file_to_read = input$file1
if (is.null(file_to_read)) {
return()
}
read.table(file_to_read$datapath, sep = ',', header = FALSE)
})
output$input_file2 <- renderTable({
file_to_read = input$file2
if (is.null(file_to_read)) {
return()
}
read.table(file_to_read$datapath, sep = ',', header = FALSE)
})
output$oput <- renderTable({
switch(input$ops,
"addtion" = input$file1 + input$file2,
"subtraction" = input$file1 - input$file2,
"multiplication" = input$file1 * input$file2,
"division" = input$file1 / input$file2)
})
}
如何解决此问题,如果此错误已解决,程序将运行吗?
您的input$file1
和input$file2
将包含文件名和路径,但不包含数据本身(它与一起在其他位置读取read.table
)。因此,除非您向矩阵运算提供数据,否则矩阵运算将不起作用。
我建议考虑使用reactive
表达式从csv文件访问数据。尽管许多教程将演示您所拥有的内容(直接在中读取数据output
renderTable
,但这对于在其他上下文中使用数据可能不太灵活。
在下面,反应式表达式data1
将读取存储在中的csv文件input$file1
。然后,要访问数据,只需将其称为data1()
。这既包括output$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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句