0

I am working on a database update form, modifying code from the following tutorial (it is a great tutorial by the way). https://ipub.com/shiny-crud-app/

One of the modifications that I have made is to include a responses table for each Database, the user selects the database from the selectInput. I am having a problem with the update and delete functions I will use the update function to illustrate.

This code works.

UpdateData <- function(data) {
    data <- CastData(data)

    TechSurveyResponses[row.names(eval(parse(text = paste(input$DatabaseName, "Responses", sep = "")))) == row.names(data), ] <<- data
}

when I try to make the dataframe name variable like this:

UpdateData <- function(data) {
    data <- CastData(data)

    eval(parse(text = paste(input$DatabaseName, "Responses", sep = "")))[row.names(eval(parse(text = paste(input$DatabaseName, "Responses", sep = "")))) == row.names(data), ] <<- data
}

I get this error.

Warning in file(filename, "r") :
  cannot open file 'TechSurveyResponses': No such file or directory
Warning: Error in file: cannot open the connection

In programming languages that I am used to, I would just "house" the dataframe in a variable which links to the dataframe, in r the <- assignment of variables creates a whole new dataframe but I want to update the currently selected dataframe (which is going to be named a different name every time the user selects a different database). These are not predefined names either, datasets will be uploaded dynamically.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Ryan
  • 1
  • Ugh, using `eval()` is usually a very bad idea and not necessary at all. The tutorial doesn't seem to use it. You should try to include a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in the question itself. That will make it easier to help you. You really shouldn't have a bunch of similarly named objects in your environment. If they are related, store them all in a named list. That will make things much easier. – MrFlick Feb 28 '17 at 05:46
  • See this answers for a better practice: http://stackoverflow.com/a/24376207/2372064 – MrFlick Feb 28 '17 at 05:52
  • Thanks for your comments, I think the named list is a decent approach, I am going to put this on the shelf for now. I actually don't know why everyone hates eval, it is one of my favorite features when doing shinyapps. The foundations for the interactivity is usually strings being passed from textInput, selectInput controls but most commands don't accept string input. So simple solution, use eval(parse. In some other circumstancs eval(parse doesn't work but there is usually an alternative, like aes_string() for ggplot etc... Thanks, I appreciate your time. – Ryan Mar 02 '17 at 19:10

0 Answers0