我是R编程的新手。当我执行我的闪亮应用程序代码时,我收到错误“func()中的错误:找不到对象'file3'”。有关如何解决此问题的任何建议?下面是server.R代码,我有错误:
library(shiny)
shinyServer(function(input, output) {
reactive ({
if(is.null(input$file1)) return(NULL)
fl1 <- paste("file:///",input$file1,sep='')
if(is.null(input$file2)) return(NULL)
fl2 <- paste("file:///",input$file2,sep='')
file1 <- read.table(fl1,sep=',',header=TRUE)
file2 <- read.table(fl2,sep=',',header=TRUE)
library(sqldf)
options(gsubfn.engine = "R")
file3 <- sqldf('SELECT * FROM file2 UNION ALL SELECT * FROM file1')
})
output$text1 <- renderTable({ file3 })
})
答案 0 :(得分:0)
我正在根据提供的代码进行回答,因为您引用了input$xx
,所以我假设您有一个ui.R文件:-)。另外,如果您的文件是上传的文件,则需要使用shiny::observeEvent
处理它们,否则input$file1
和input$file2
将始终为NULL
。
您还应该确保将带有inputID = "text1"
的UI对象定义为dataTable输出而不是textOutput。像这样:shiny::dataTableOutput("text1")
。这样可以确保正确解析您的输入。
我还修复了一些样式,使您的代码更具可读性。请参阅google R样式指南。 考虑到上述情况,您可以尝试以下操作:
library(shiny)
library(sqldf)
options(gsubfn.engine = "R")
shinyServer(function(input, output) {
getData <- reactive ({
if(is.null(input$file1)) return(NULL)
fl1 <- paste("file:///", input$file1, sep = '')
if(is.null(input$file2)) return(NULL)
fl2 <- paste("file:///", input$file2, sep='')
file1 <- read.table(fl1, sep = ',', header = TRUE)
file2 <- read.table(fl2, sep = ',', header = TRUE)
file3 <- sqldf('SELECT * FROM file2 UNION ALL SELECT * FROM file1')
return(file3)
})
output$text1 <- shiny::renderDataTable({ getData() })
})
我希望这会有所帮助。您可能还想尝试使用DT::renderDataTable
代替shiny::renderDataTable