rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.87k forks source link

Error in .func() : object 'input' not found shiny #1019

Closed Stophface closed 8 years ago

Stophface commented 8 years ago

I want to plot something with renderDataTable. Thats how I send it to the ui.R

output$myTable <- renderDataTable({myTableSelection()})

Thats how the ui.R recieves it:

... tabPanel("Title", value = 4, dataTableOutput("myTable ")))), ...

Before the table is rendered, some preprocessing of the data is necessary. I keep getting this errormessage:

Error in .func() : object 'input' not found

To narrow down the error I kept commenting out lines that process the dataframe before sending it to renderDataTable. I narrowed it down to these lines:

someName <- eventReactive(input$someInputA, {
    if( (as.integer(input$someInputB) == 2) ) {
        return(someDf[someDf$someColumn %in% input$someInputA ,])

    } else {
        print("Whatever")
    }
})

Everywhere else in my code it works when I refer to a variable from the ui.R with input$whatever, but not here.

When I print the two inputs document with the code that wont execute like so

observe({
    print(input$someInputA)
    print(input$someInputB)
})

It gets printed properly.

I even tried to assign input$... to some variables in global.R but that did not help neither. Any ideas why?!

jcheng5 commented 8 years ago

Those lines need to appear somewhere inside your server function (not just be called from there). The input variable is accessed via R's usual lexical scoping mechanism, nothing more.

If that doesn't make sense to you, feel free to send me your code and I'll have a look. If you can't post a link to it publicly you can email me at joe@rstudio.com.

Stophface commented 8 years ago

@jcheng5 I even copy & pasted them already into the server.R function - that does not work. I also copy & pasted these lines into other .R files which work totally fine. Even in files where I access the same input$... variables. Thats what is driving me crazy. It seems that just in there it does not work... I basically just copy & pasted these lines from another file. But they do not work here...

I cannot give you my code, the data is sensible... :-/

jcheng5 commented 8 years ago

It makes absolutely no sense to me that these lines would work in other files. Or maybe they would work, but actually calling the eventReactive you're creating would throw. However it makes just as little sense that this would NOT work inside of shinyServer.

As a next step I'd suggest installing the latest Shiny code from GitHub, it ought to give you full stack traces for errors. Can you please try that and show us the stack trace for this error? On Thu, Nov 26, 2015 at 7:20 AM Stophface notifications@github.com wrote:

@jcheng5 https://github.com/jcheng5 I even copy & pasted them into my server.R function - that does not work. I copy & pasted these lines into other .R files which work totally fine. Even in files where I access the same input$.... Thats what is driving me crazy. It seems that just in there it does not work...

I cannot give you my code, the data is sensible... :-/

— Reply to this email directly or view it on GitHub https://github.com/rstudio/shiny/issues/1019#issuecomment-159936729.

Stophface commented 8 years ago

@jcheng5 Yep, it does not make any sense for me neither. Thats why I came here... I try to update and come back to you.

Stophface commented 8 years ago

@jcheng5 Oh dear. Solved it...

adamfedoruk commented 8 years ago

Well, @Stophface don't leave us hanging! What was the problem?

jatin69 commented 6 years ago

I am facing the same problem. Any solution ?

PeterDieter commented 5 years ago

I also have this problem. @Stophface How did you solve it?

jcheng5 commented 5 years ago

@PeterDieter Can you provide a reproducible example?

PeterDieter commented 5 years ago

I solved it. The problem was that I had input elements inside a function. I thought it wont be a problem as the function was inside a reactive event. So instead of calling the input elements inside the function they are now an argument of that function and it works fine.

DmitriyKandaurov commented 5 years ago

@PeterDieter Sorry, didn't understand. Have the same problem with observeEvent(), it doesn't "see" input object, should I pass it to observeEvent() or not, could you give working example... One more thing, when i compile the whole app in console it works, but, when I do it with Run App button Rstudio returns:

> Warning: Error in observeEventExpr: object 'input' not found 58: observeEventExpr 1: runApp

and stops. The whole App (it needs AUTHENTICATION.csv):

rm(list=ls())
library(shiny)
library(shinyWidgets)
library(xlsx)

PASSWORDS <- read.csv("AUTHENTICATION.csv", header=T, sep=";", encoding = "UTF-8", stringsAsFactors = F)

ui = fluidPage(
  actionButton("play",label = "play"),
  verbatimTextOutput("dataInfo")
)

server = function (input, output) {

  user <- reactiveValues(login = NULL, regs = NULL)

  autModal <- function (failed_aut = FALSE) {
    modalDialog(
      textInput("login_aut", "1", placeholder = 'latin'),
      textInput("password_aut", "2", placeholder = 'latin'),
      if (failed_aut)
        div(tags$b("4", style = "color: red;")),
      footer = tagList(
        modalButton("cancel"),
        actionButton("reg", "registration"),
        actionButton("ok", "ok")
      )
    )
  }

  regModal <- function (failed_regi = FALSE) {
    modalDialog(
      textInput("fio_reg", "Name and surname", placeholder = 'latin'),
      textInput("login_reg", "Login", placeholder = 'latin'),
      textInput("password_reg", "Password", placeholder = 'latin'),
      textInput("group_reg", "Group", placeholder = 'latin'),
      textInput("mail_reg", "E-mail", placeholder = 'ivanov@mail.ru'),
      if (failed_regi) 
        div(tags$b("hren", style = "color: red;")),
      footer = tagList(
        modalButton("cancel"),
        actionButton("regi", "register")
      )
    )
  }

  observeEvent(input$play, {
    showModal(autModal())
  })

  observeEvent(input$ok, {
    if ( !is.null(input$login_aut) && (input$login_aut %in% PASSWORDS$login) &&
         (input$password_aut == PASSWORDS$password[which(PASSWORDS$login==input$login_aut)]) ) 
    {
      user$login <- input$login_aut
      removeModal()
    } else {
      showModal(autModal(failed_aut = TRUE))
    }
  })

  observeEvent(input$reg, {
    removeModal()
    showModal(regModal())
    observeEvent(input$regi, {
      if (!is.null(input$fio_reg) &&
          !is.null(input$login_reg) &&
          !is.null(input$password_reg) &&
          !is.null(input$group_reg) &&
          !is.null(input$mail_reg) &&
          !(input$mail_reg %in% PASSWORDS$mail) &&
          !(input$login_reg %in% PASSWORDS$login))
      { 
        user$regs <- c(input$login_reg, input$password_reg, input$group_reg, input$mail_reg, input$fio_reg)
        PASSWORDS <<- rbind(PASSWORDS, user$regs)
        file <- paste(getwd(), "/AUTHENTICATION.csv", sep="")
        write.csv2(PASSWORDS, file, fileEncoding = "UTF-8", row.names=F)
        removeModal()
      } else {
        showModal(regModal(failed_regi = TRUE))
      }
    })
  })
  removeModal()

  output$dataInfo <- renderPrint({
    if (is.null(user$login))
      "No data selected"
    else
      user$regs
  })

}

shinyApp(ui = ui, server = server)
amberlittle commented 4 years ago

For me, it was literally because I had an extra comma after my InputSelection() function, before the start of the dashboard body