0
votes

I am building a Shiny and the end result will be a graph with "Depth" on the x-axis and "Values" on the y-axis. The user will upload their own dataframe so I have included two inputs that will change based on the dataframe. There is a checkboxGroupInput("elements", label = "Select Elements", choices = NULL) and then there is selectInput("depth_col", "Depth Column:", choices = NULL) and both of these have choices fed by the same observeEvent in my server.R to read the column names of the uploaded dataframe. My problem comes when I try to plot this. In my test dataset, I know the "Depth" column is actually called Depth so:

output$Gamma <- renderPlot({
    input$slice
    
    if(input$slice == 0){
      return()
    }
    
    inFile <- inFile()
    Element <- input$elements
    
    inFile %>%
      select(c(Depth, all_of(Element))) %>%
      pivot_longer(-Depth, names_to = "Elements", values_to = "Values") %>%
      ggplot(aes(x = Depth, y = Values, colour = Elements)) +
      geom_path()
  })

produces the desired plot (input$slice is the result of clicking the "Select" button. enter image description here

Notice that it is only producing lines for the two selected elements. However, when I try to adjust the code to use input$depth_col (maybe in a different dataset the "Depth" column is called Deep, for example):

output$Gamma <- renderPlot({
    
    input$slice
    
    if(input$slice == 0){
      return()
    }
    
    inFile <- inFile()
    Deep <- input$depth_col
    Element <- input$elements
    
    inFile %>%
      select(c(Deep, all_of(Element))) %>%
      pivot_longer(-Deep, names_to = "Elements", values_to = "Values") %>%
      ggplot(aes(x = Deep, y = Values, colour = Elements)) +
      geom_path()
  })

it is treating Deep as a character instead of reading the values in the column and plotting a straight line. enter image description here

Essentially, the value chosen in the "Depth Column" should be assigned to the x-axis and the "Select Elements" on the y-axis. The other two inputs will be used later on but shouldn't have any bearing at this point.

Thank you to anyone who can show me where I'm going wrong here, Jeremy

1
Please post a full MRE with some sample data or built in dataset, such as, mtcars, iris, etc., so that someone can help you. - YBS
Sorry about that @YBS, I will certainly remember that for next time. - JJGabe

1 Answers

2
votes

You need to tell R to treat the string in input$depth_col as an object name (only where necessary, dplyr::select for example accepts strings). You can do this using !! sym(input$depth_col).

output$Gamma <- renderPlot({
    input$slice
    
    if(input$slice == 0){
      return()
    }
    
    inFile <- inFile()
    Element <- input$elements
    
    inFile %>%
      select(all_of(c(input$depth, Element))) %>%
      pivot_longer(!! sym(input$depth_col), names_to = "Elements", values_to = "Values") %>%
      ggplot(aes(x = !! sym(input$depth_col), y = Values, colour = Elements)) +
      geom_path()
  })