-3

I am new to Shiny Notebooks in R. I'm just tinkering around trying to learn. I am trying to get a ggplot to look right in the HTML document output, but I cannot get the scaling correct. If I run the same ggplot in the Shiny notebook without using input variables it looks the way I would expect. Why does this happen?

The follow code produces an output that is unusable:

```{r selectInput for iris database}

selectInput("x_axis", "X-Axis",
            choices = names(iris))
selectInput(inputId = "y_axis", label = "Y-Axis",
            choices = names(iris))

renderPlot({
  ggplot(iris, aes(input$x_axis, input$y_axis, colour = Species)) +
      geom_point()
})

The following code works correctly:

##GGPLOT Example
```{r}

ggplot(iris, aes(Petal.Length, Sepal.Length, colour = Species)) + 
                xlim(0,10) +
                ylim(0,10) +
                geom_point()
```
0

2 Answers 2

1

You shouldn't be providing options to aes using [, [[ or $ as ggplot expects bare variable names within aes. For this instance aes_string is provided where you can provides the aes a string values, which works well with selectInput from shiny.

The chunk below should work when rendered in a notebook with runtime: shiny

```{r selectInput for iris database, echo = FALSE, message = FALSE}
library(tidyverse)
library(shiny)

selectInput("x_axis", "X-Axis",
            choices = names(iris))

selectInput(inputId = "y_axis", label = "Y-Axis",
            choices = names(iris))

renderPlot({
  ggplot(iris, aes_string(input$x_axis, input$y_axis, colour = "Species")) +
      geom_point()
})
```
Sign up to request clarification or add additional context in comments.

Comments

0

You need to call the data into your aes() block, not just the header name. When you call 'input$x_axis' it reads it as Sepal.Length, but does not pull the data in. I've included an example below:

renderPlot({
  library(tidyverse)
  data = iris %>%
    select(Species, input$x_axis, input$y_axis)

  ggplot(data, aes(x = data[,2], y = data[,3], colour = Species)) +
      geom_point()
})

Side note: I'd suggest you also clean up the dropdown lists so one cannot select two of the same and also cannot select Species..

1 Comment

Unfortunately, this is not a good general strategy as facetting may create big problems and data may end up in the wrong facet. Use the appropriate non-standard evaluation, or use aes_ or aes_string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.