1

I am running the current version of RStudio, R, and all R packages.

In the sample code below, my goal is to set the maximum value for the xcol and ycol so they are limited to the number of columns in the dataframe that is being plotted. The code below results in the error "object 'input' not found." I suspect the problem may be that I am making the widgets in the ui dependent, but that is a guess on my part. Is that the problem, and is there a strategy that I can use to get around it.

I reviewed posts that contained the same error, but couldn't find anything that answered my question (or didn't recognize when it was answered.) The closest posts to my issue were: R Shiny error: object input not found; R Shiny renderImage() does not recognize object 'input'; Error in eval: object 'input' not found in R Shiny app; Conditional initial values in shiny UI?

Here is some reproducible code with random data.

library(tidyverse)
library(cluster)
library(vegan)
library(shiny)

dta <- rnorm(100, mean = 0, sd = 1)
mat <- matrix(dta, nrow = 10)
dm <- daisy(mat, metric = "euclidean") %>% as.matrix()

server <- function(input, output) {
    output$plot <- renderPlot({
        nmds <- metaMDS(dm, distance = "euclidean", k = input$dim, trymax = 2000, autotransform = FALSE, noshare = FALSE, wascores = FALSE)
        df <- nmds$points %>% as.data.frame()
        plot(df[,input$xcol], df[,input$ycol])
    }, height = 500, width = 500)
}

ui <- fluidPage(
        sidebarLayout(
        sidebarPanel(
            numericInput("dim", "Number of dimensions", value = 2, min = 2, max = 12),
            numericInput("xcol", "X column", value = 1, min = 1, max = input$dim),
            numericInput("ycol", "Y column", value = 2, min = 1, max = input$dim)
        ),
            mainPanel(
                plotOutput("plot")
        )
      )
    )
Community
  • 1
  • 1

1 Answers1

0

You can use updateNumericInput to modify the UI from the server:

# Modify ui to use initial max
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput("dim", "Number of dimensions", value = 2, min = 2, max = 12),
      numericInput("xcol", "X column", value = 1, min = 1, max = 2),
      numericInput("ycol", "Y column", value = 2, min = 1, max = 2)
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)


# Modify server to update max when dim changes 
# (notice the session parameter needed for updateNumericInput)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    if(input$xcol>input$dim)
      updateNumericInput(session, "xcol", value=input$dim, max=input$dim)
    else
      updateNumericInput(session, "xcol", max=input$dim)
    if(input$ycol>input$dim)
      updateNumericInput(session, "ycol", value=input$dim, max=input$dim)
    else
      updateNumericInput(session, "ycol", max=input$dim)
    nmds <- metaMDS(dm, distance = "euclidean", k = input$dim, trymax = 2000, autotransform = FALSE, noshare = FALSE, wascores = FALSE)
    df <- nmds$points %>% as.data.frame()
    plot(df[,min(input$dim,input$xcol)], df[,min(input$dim,input$ycol)])
  }, height = 500, width = 500)
}
HubertL
  • 19,246
  • 3
  • 32
  • 51
  • I edited to take into account the decreasing of dim while xcol or ycol were at the maximum – HubertL Jan 13 '17 at 02:10
  • Thank you, I really appreciate the help. For some reason it didn't work the first few times, but now it is working wonderfully and I'm starting to understand the logic. – user3550400 Jan 13 '17 at 03:55