For Loop in RShiny: Warning: "Error in names: 0 arguments passed to ... which requires 1"

Viewed 78

using RShiny/SQL for the first time so I'm sure I'm misunderstanding a fundament but none of the documentation is helping. I'm trying to:
(1) let the user choose which SQL table to load in
(2) submit a string of characters (protein names)
(3) return which protein names are in the chosen table

I've managed the first 2 fine but on the for loop I get '0 arguments passed to 'names' which requires one' and I'm not sure why. My code:

UI:

>library(shiny)

ui <- fluidPage(

  titlePanel("TBD"),

  sidebarLayout(

    sidebarPanel(

      selectInput("variable", "variable:",
                  list("Knoener" = "Knoener",
                       "Liz" = "Liz", 
                       "Kula" = "Kula")),
      actionButton("button1", "Click Me"),
      textInput("names","Enter protein symbols"),
      actionButton("button2", "Click Me"),
      actionButton("button3", "Let's go!")
    ),

    mainPanel(
      textOutput("text1"),
      textOutput("text2"),
      textOutput("text3")

    )
))

SERVER:

library("shiny")
library("DBI")
library("dplyr")
library("dbplyr")
library('pool')

loadData <- function(table) {
  db <- dbConnect(MySQL(), dbname = "knoenerdb", host = "localhost", 
                  user = "root", 
                  password = "blahblah")
  query <- sprintf("SELECT * FROM %s", table)
  chosendata <- dbGetQuery(db, query)
  dbDisconnect(db)
}

server <- function(input, output) {

    chosendata <- observeEvent(input$button1, {
      loadData(input$variable)
      output$text1 <- renderText({paste("input is",input$variable)})})

    names <- observeEvent( input$button2, {
        names <- unlist(strsplit(input$names, ", "))
        output$text2 <- renderText({paste("names are",names)})
    })

     observeEvent( input$button3, {
       for(i in 1:length(names())){
          if(names()[i] %in% chosendata()$proteins){
             updated = c(updated,names()[i])
          } else
             updated = c(updated, "NULL")
        }

     output$text3 <- renderText({paste("matches are",updated)})
    }) 

}

Thanks for the help!

1 Answers

My guess would be that you have some names clashing. Consider renaming text input names to something unique.

> names()
Error in names() : 0 arguments passed to 'names' which requires 1
Related