2016-10-22 96 views
0

我正在开发一个闪亮的应用程序,用户上传一个csv文件,并在动作按钮单击事件文件将被推送到MySQL数据库。我使用的3个文件ui.R,server.Rfunction.R如何访问闪亮仪表板中的function.R文件中的selectInput值

这里是我的ui.R文件看起来像

tabItem("chooseFile", 
      fluidRow(
      box(
       width = 5, status = "info",solidHeader = TRUE, 
       title = "Send Emails", 
       helpText(tags$b("Please uplaod .csv or .tsv file")), 
       tags$hr(), 
       fileInput('csv_file', 'Choose file to upload', 
         accept = c(
          'text/csv', 
          'text/comma-separated-values', 
          'text/tab-separated-values', 
          'text/plain', 
          '.csv', 
          '.tsv' 
         ) 
      ), 
       tags$hr(), 
       checkboxInput('header', 'Header', TRUE), 
       radioButtons('sep', 'Separator', 
          c(Comma=',', 
          Semicolon=';', 
          Tab='\t'), 
          ','), 
       actionButton("upload_file", tags$b("Upload File")) 


      ) 
      ) 
     ), 

这是我server.R文件

observeEvent(input$upload_file, { 
    csv <- input$csv_file 
    saveData(csv) 
    session$sendCustomMessage(type ='testmessage', 
           message = paste0("File has been successfully uploaded to database")) 

    }) 

以下是我saveData()功能看起来这是在function.R文件

saveData <- function(data) { 
    # Connect to the database 
    db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
       port = options()$mysql$port, user = options()$mysql$user, 
       password = options()$mysql$password) 
    # Construct the update query by looping over the data fields 
    dbWriteTable(db, table, input$csv, overwrite = TRUE) 

    dbDisconnect(db) 

    return(final_data) 
    } 

input$csv是我想要在数据库中上传的文件,但是当我运行这段代码时,它给了我下面的错误。

Error in dbWriteTable(db, table, input$csv_file, overwrite = TRUE) : 
object 'input' not found 

如何访问输入变量从ui.R文件中function.R文件?

+0

全球访问它从我看到你没有任何输入名为CSV。所以我想或者你应该在函数调用中使用输入$ csv_file,或者删除dbWriteTable中的输入$ part,并将csv变量传递给函数调用。 –

+0

@ValterBeaković现在我得到这个错误'dbWriteTable(db,table,data)中的错误:参数“data”丢失,没有默认' – Neil

+0

现在不在我的电脑上,今天晚些时候会看看 –

回答

1

我终于能够用下面的代码做到这一点。

data <- reactive({ 
inFile <- input$csv_file 
if (is.null(inFile)){ 
    return(NULL)} 
data <- read.csv(inFile$datapath, header = input$header, sep = input$sep, encoding = 'UTF-8') 
data 

})

我可以data()