R Shiny Error: Object Input Not Found

R Shiny error: object input not found

The problem is with ggplot evaluating the variable in the context of the data frame provided, spend in your case. What you want is:

ggplot(spend, aes_string(x = "Country", y = input$split))

So your working server.R code is:

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

spend <- data.frame(Country = c("Turk", "Turk", "Saudi", "Saudi", "Ger", "Ger"),
Action.Obligation = c(120,-345,565,-454, 343,-565),
Action_Absolute_Value = c(120,345,565,454,343,565))

output$SpendChart <- renderPlot({

ggplot(spend, aes_string(x = "Country", y = input$split)) +
geom_bar(stat = "identity")

})

})

Obviously, you can replace the spend df with your CSV import.

R Shiny, Object 'input' not found

As @r2evans says, Your code which creates dt is outside the server function, so it's in the global environment. The variable input doesn't exist there. So, put the code from options(stringsAsFactors = FALSE) to dt$content <- as.character(dt$schedule.item.type) inside the server function. That will give you access to input.

That answers your question, but you will have more to do:

  • input is accessible only within an active context, so you'll have
    to wrap your initialisation code in observe({}) or similar.
  • You'll need to prevent renderTimevis() from erroring with incorrect input. Something like:
    output$timeline <- renderTimevis({
req(input$apikey1, input$userkey1)
if (is.data.frame(dt)) timevis(dt)
})
  • You might consider using placeholder= rather than value= in your
    textInputs to provide initial guidance to your users.
  • You'll need
    to modify your initialisation code to prevent errors on start up due
    to missing values. Something like:
observe({
req(input$apikey1, input$userkey2)
<... your initialisation code ...>
})

At least gets your app's GUI to display.

R Shiny: object 'input' not found

This is due to the combination of deparse and substitute.

Lets revisit what you are doing in simpler terms:

a <- function(val) {
val <- deparse(substitute(val))
val
}

b <- function() {
c <- "someVariable"
a(c)
}

With this setup you will get:

> b()
[1] "c"

I hope you are aware of this and if not, make yourself. You basically are sending the variable name from the calling scope of b into the function scope of a. This is almost never what you want to happen in programming.

In your code sample, you are giving a variable name called input$Report.Date into the globally defined function Aging and you hope that it will be interpreted there in the right way. But of course, input is not an object in this function.

But why would it? Why do you have to interpret the variable in Aging. If you leave away the deparse(substitute(...)), you are not getting the variable name but the variable value. And nothing else is it that you want here.

I bet the recode function is to blame for you having to be aware of what is code to be interpreted and what are just values.

So the fix is to just leave away

Report.Date <- deparse(substitute(Report.Date))

Little Extra: If I were you, I would never use something like deparse and substitute unless there really is code involved that comes as plain text and must be interpreted.

Also recode seems to be so much over the line, since it just cloaks very simple modifications that you can easily do without this overcomplicated tool.

If I were you, my Aging function would look like this:

Aging <- function(data, dateColumnName, reportDate = Sys.Date()){
if(missing(dateColumnName)) stop("You forgot to specify Transaction Date")

maxDisplayYear <- year(reportDate) - 2
data$Age <- year(data[,dateColumnName])
data$Age[data$Age <= maxDisplayYear ] <- paste(maxDisplayYear, 'And Prior')

return(data)
}

and you would call it in your example with

Debtors() %>% Aging("Transaction.Date", input$Report.Date)

Using numeric input in shiny app yields a Error: object 'input' not found

Reason

The reason this is not working is because of the way gt::cols_width() evaluates is arguments. It does not know which environment to look in to find the input object.

One way to circumvent the issue is to first evaluate input$colpad and then pass the value in a way gt::cols_width() will understand.

Code

Here is one such approach where I paste together a formula and cast it as such on line 46:

library(data.table)
library(shiny)
library(gt)
library(shinyscreenshot)
select <- dplyr::select

data <- gtcars %>%
head(10) %>%
select(mfr, model, msrp)

ui <- navbarPage(
"Y u no pad??",
tabPanel("Table",
icon = icon("table"),
sidebarLayout(
sidebarPanel(
selectInput("input",
label = "Choose mfr",
choices = c("All", data$mfr)
),
numericInput("colpad", label = "First Column Padding", min = 1, max = 10000, value = 150),
screenshotButton(selector = "#table", label = "Download Png", filename = "screenshot"),
),
mainPanel(
gt_output("table")
)
)
)
)

server <- function(input, output, session) {
reactive_tab <- reactive({
d <- data
if (input$input != "All") {
d <- subset(d, cyl == input$input)
}
d
})

output$table <- render_gt(
reactive_tab() %>%
gt() %>%
cols_width(
as.formula(paste0("1 ~ ", input$colpad)) # EDIT HERE
)
)
}

shinyApp(ui, server)

Result

Sample Image

Error in eval: object 'input' not found in R Shiny app

So, As I mentioned the issue of your problem is not Rshiny. It is the usage of the expression.

What you are doing here is

expression(x*(1-(x/1000))^input$theta*0.2)

Which basically outputs the same expression without substituting input$theta, for the value 5.

What you need to do is below

 f <- as.expression(bquote(x*(1-(x/1000))^.(input$theta)*0.2))

#bquote evaluates the expression enclosed in .()

This outputs

expression(x * (1 - (x/1000))^5 * 0.2)

I hope it solved your problem

R Shiny selectInput returning error object 'input' not found

This is a problem with partialPlot. This is addressed in another question here. The only explanation is that some plot functions use some non-standard evaluation.

If you change your partDep section in server.R to the following it works:

output$partDep <- renderPlot({
do.call("partialPlot",
list(x=rfInput(), pred.data=train1, x.var = input$select_var, which.class = 1))
})


Related Topics



Leave a reply



Submit