【问题标题】:Object Not found error in my Shiny server.R code在我的 Shiny server.R 代码中找不到对象错误
【发布时间】:2015-04-08 01:22:42
【问题描述】:

我是 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 })

})

【问题讨论】:

  • 您可能需要将您的反应式分配给一个对象,然后在您的renderTable 中调用它,例如x &lt;- reactive({ ...... }) 在第 1 行,然后是 output$text1 &lt;- renderTable({ x() }) - 可能有帮助吗?
  • file3renderTable({file3}) 中使用时超出范围

标签: r shiny shiny-server


【解决方案1】:

我是根据提供的代码来回答的,因为您引用了input$xx,我假设您有一个 ui.R 文件:-)。此外,如果您的文件是上传的文件,您需要使用shiny::observeEvent 处理它们,否则input$file1input$file2 将始终为NULL

您还应确保将带有inputID = "text1" 的 UI 对象定义为 dataTable 输出而不是 textOutput。所以是这样的:shiny::dataTableOutput("text1")。这将确保您的输入被正确解析。

我还修复了您的一些样式,以使您的代码更具可读性。请参阅谷歌 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多