CallumH
CallumH

Reputation: 779

Error: invalid first argument with R Shiny plot

I have written an R script to train myself and others on Shiny, with R.

One can select a dataset and plot an 'x' and 'y' variable on a base plot. There are a couple of other user defined arguments also. It all works, however it also kicks 'Error: invalid first argument', which can be seen on the 'Plot' tab (on the shiny dashboard).

I have included a 'Submit' button to pause the process down and you can see the error sign clearly, with out the submit button the the error flashes briefly, disappears and then everything works.

Additional information in the console suggests it may have something to do with a 'get' command, but I can't see what it may refer to further, and what to do about it.

Any ideas welcome, thanks.

The 2 shiny files =

ui.R

library(shiny)
data_sets = c("iris", "mtcars", "trees")

shinyUI(fluidPage(

  titlePanel(h1("Plotting Playaround")),

    sidebarLayout(

    sidebarPanel(

      selectInput("var_data", "Select a dataset to plot up!", choices = data_sets),
      br(),
      uiOutput("x_var"),
      br(),
      uiOutput("y_var"),
      br(),
      br(),
      selectInput("plt_pts", "What sorta plot points do ya want?", 
              choices = c("points" = "p" ,
                          "lines" = "l" ,
                          "both" = "b" ,
                          "lines_only" = "c" ,
                          "overplotted" = 'o' ,
                          "hist_like" = 'h' ,
                          "stairs" = "s" ,
                          "alt_stairs"= "S",
                          "no_plot" = "n" )),
      radioButtons("plt_col", "Choose a colour!", 
               choices = c("Red",
                           "Green",
                           "Blue")),
      submitButton("Submit!")

    ),

    mainPanel(

      tabsetPanel(type = 'tab',
        tabPanel("Plot", plotOutput("p")),
        tabPanel("Summary", verbatimTextOutput("sum"))

      ) # tabsetPanel
      ) # mainPanel
)))

server.R

library(shiny)
shinyServer(function(input, output){

  # reactive variables
  data_var = reactive({
    switch (input$var_data,
      "iris" = names(iris),
      "mtcars" = names(mtcars),
      "trees" = names(trees)
    )
  })

 my_data = reactive({
    switch (input$var_data,
        "iris" = iris,
        "mtcars" = mtcars,
        "trees" = trees
   )
  })

    pltpts = reactive({
    as.character(input$plt_pts)
  })

  pltcol = reactive({
    input$plt_col
  })

  # outputs
  output$x_var = renderUI({
    selectInput("variablex", "Select the 'X' variable!", choices = data_var())
  })

  output$y_var = renderUI({
selectInput("variabley", "select the 'Y' variable", choices = data_var())

})

  output$p = renderPlot({
    attach(get(input$var_data))
    plot(x = get(input$variablex), 
         y = get(input$variabley), 
         xlab = input$variablex, 
         ylab = input$variabley, 
         type = pltpts(),
         col = pltcol())
  })

  output$sum = renderPrint({
    summary(my_data())
  })
})

Upvotes: 3

Views: 3621

Answers (1)

Pork Chop
Pork Chop

Reputation: 29407

Since you're creating selectInput dynamically you need to check for NULL in your renderPlot. Like so:

output$p = renderPlot({   
    if(is.null(input$variablex) || is.null(input$variabley)){return()}

    attach(get(input$var_data))
    plot(x = get(input$variablex), 
         y = get(input$variabley), 
         xlab = input$variablex, 
         ylab = input$variabley, 
         type = pltpts(),
         col = pltcol())
  })

Upvotes: 3

Related Questions