在Shiny App中找不到MySQL表('对象'未找到错误,但MySQL中存在表)

时间:2015-11-17 22:05:16

标签: mysql r shiny rmysql

我有一个Shiny应用程序应该在表单中收集数据并将其附加到MySQL中的数据表test。我正在使用RMySQL移植到数据库中。该应用程序还应显示数据框。我使用'save'和'load'函数来实现这一点,我认为连接是有效的,但由于某种原因,在部署应用程序时找不到表'object'。

这是错误: 的 # decide which function to use to save based on storage type get_save_fxn <- function(type) { fxn <- sprintf("save_data_%s", type) fxn } save_data <- function(data, type) { fxn <- get_save_fxn(type) do.call(fxn, list(data)) } # decide which function to use to load based on storage type get_load_fxn <- function(type) { fxn <- sprintf("load_data_%s", type) fxn } load_data <- function(type) { fxn <- get_load_fxn(type) data <- do.call(fxn, list()) # Just for a nicer UI, if there is no data, construct an empty # dataframe so that the colnames will still be shown if (nrow(data) == 0) { data <- matrix(nrow = 0, ncol = length(fields_all), dimnames = list(list(), fields_all)) %>% data.frame } data %>% dplyr::arrange(desc(timestamp)) } load_data_mysql <- function() { on.exit(dbDisconnect(db)) db <- dbConnect(RMySQL::MySQL(),user="name", password="pass", host="host", dbname="bk") query <- sprintf("SELECT * FROM %s", test) data <- dbGetQuery(db, query) } save_data_mysql <- function(data) { on.exit(dbDisconnect(db)) db <- dbConnect(RMySQL::MySQL(),user="name", password="pass", host="host", dbname="bk") query <- sprintf("INSERT INTO %s (%s) VALUES ('%s')", test, paste(names(data), collapse = ", "), paste(data, collapse = "', '") ) dbGetQuery(db, query) }

当我删除加载和/或保存功能时,错误消失。

这是我的global.R文件的一部分。查询是在用户输入定义要获取的数据类型之后运行的函数。在这个应用程序中,数据类型是MySQL数据库中的数据。

 # Enable the Submit button when all mandatory fields are filled out
 observe({
   fields_filled <-
     fields_mandatory %>%
     sapply(function(x) !is.null(input[[x]]) && input[[x]] != "") %>%
     all

   shinyjs::toggleState("submit", fields_filled)
 })

 # Gather all the form inputs
 form_data <- reactive({
   sapply(fields_all, function(x) x = input[[x]])
 })

 # When the Submit button is clicked 
 observeEvent(input$submit, {
   # Update the timestamp field to be the current time
   updateTextInput(session, "timestamp", value = get_time_epoch())

   # User-experience stuff
   shinyjs::disable("submit")
   shinyjs::show("submitMsg")
   shinyjs::hide("error")
   on.exit({
     shinyjs::enable("submit")
     shinyjs::hide("submitMsg")
   })

   # Save the data (show an error message in case of error)
   tryCatch({
     save_data(form_data(), input$storage)
     shinyjs::reset("form")
     updateTabsetPanel(session, "mainTabs", "viewTab")
   },
   error = function(err) {
     shinyjs::text("errorMsg", err$message)
     shinyjs::show(id = "error", anim = TRUE, animType = "fade")      
     shinyjs::logjs(err)
   })
 })

 # Update the responses whenever a new submission is made or the
 # storage type is changed
 responses_data <- reactive({
   input$submit
   load_data(input$storage)
 })

 # Show the responses in a table
 output$responsesTable <- DT::renderDataTable(
   DT::datatable(
     responses_data(),
     rownames = FALSE,
     options = list(searching = FALSE, lengthChange = FALSE, scrollX = TRUE)
   )
 )

 # Allow user to download responses
 output$downloadBtn <- downloadHandler(
   filename = function() { 
     paste0(TEST_INPUT, "_", input$storage, "_", get_time_human(), '.csv')
   },
   content = function(file) {
     write.csv(responses_data(), file, row.names = FALSE)
   }
 )

这是我的服务器.R

idea {
  module {
     scopes.TEST.minus += ['org.foo:bar:0.9-oldversion']
  }
}

0 个答案:

没有答案