저는 r (및 일반적으로 프로그래밍)을 처음 접했고 r을 사용하여 웹 앱을 만들도록 요청 받았습니다. 계획은 다른 입력 파일에서 행렬 연산 계산기를 만드는 것이 었습니다 (첫 번째 .cvs 파일에는 행렬 1, 2 .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] 삭제
몇 마디 만하겠습니다