Unable to plot based on a reactive variable

I have a reactive data with interested variable to plot however the variable's name can vary for different data. The code has been summarized below:

server:
possible_name <- c("a", "b", "c")

variable_name <- reactive({intersect(colnames(data()), possible_name)})

output$plot <- renderPlot({ggplot(data(), aes(variable_name()))})

ui:
plotOutput("plot")

However, no plot has been showed, may I know how to solve this?

One problem is that calling only ggplot() will not produce a plot. You must add to that a layer to define how the data are displayed. For example

ggplot(data(), aes(x, y)) + geom_point()

Also, the intersect() function will return a vector of characters, so you must use aes_string() instead of aes()

If intersect returns all three of "a", "b", and "c", aes_string() will not know what to do with the third value. Do you know that there will always be a fixed number of returned values from intersect()?

It is not clear what data() is, so I have to assume you have that correctly defined.

I suggest you first get your plot working outside of shiny using just a data frame, the vector possible_name, intersect() and a correct call to ggplot(). When that is working, move to implementing it in shiny.

3 Likes

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