how to resolve the expression in the ggplot r shiny

I am trying to pass a expression into the ggplot of r shiny app and generate the graph, however it seems not working . Any suggestion is appreciated

in the code below, i am trying to render a plot where i am using the input$xaxis and input$yaxis, while i pass the input$xaxis which is a character needs to be considered as a variable in aes(), so instead of using the aes() i am using the aes_string(). Now, i am trying to pass an expression so that i can reorder the input$xaxis with a variable. but its not working.

    output$plot2 <- renderPlot({
      req(input$para, input$yaxis)
      if (input$xaxis=='ady') {
        xaxis <- expr('ady')
      } else if (input$xaxis=='visit') {
        xaxis <- parse_expr("reorder(input$xaxis, 'visitnum')")
      } else if (input$xaxis=='avisit') {
        xaxis <- expr(reorder(input$xaxis, 'avisitn'))
      }
      ggplot(advs %>% filter(usubjid==input$subj & param==input$para), aes_string(x=eval_tidy(xaxis),y=input$yaxis, group='atpt')) + 
        geom_line(aes(color=atpt)) +
        ggtitle(paste(input$para, 'Line Plot'))
    })

I think there is a way to use the expressions in r shiny. here is the updated code which worked.
you can check the #updated code comment

    output$plot2 <- renderPlot({
      req(input$para, input$yaxis)
      if (input$xaxis=='ady') {
        xaxis <- parse_expr('.data[[input$xaxis]]') #updated code 
      } else if (input$xaxis=='visit') {
        xaxis <- parse_expr("reorder(.data[[input$xaxis]], visitnum)") #updated code 
      } else if (input$xaxis=='avisit') {
        xaxis <- parse_expr('reorder(.data[[input$xaxis]], avisitn)') #updated code 
      }
      ggplot(advs %>% filter(usubjid==input$subj & param==input$para), aes(x=eval_tidy(xaxis),y=.data[[input$yaxis]], #updated code 
group=atpt)) + 
        geom_line(aes(color=atpt)) +
        ggtitle(paste(input$para, 'Line Plot'))
    })
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.