Anakin Skywalker
Anakin Skywalker

Reputation: 2520

Rendering Shiny ggplot chart output after gather()

I am trying to build a ggplot output for Shiny. I gathered my initial data frame with gather() into tidy format.

               variable             value
1                Region            Africa
2                Region            Europe
3                Region           Europe 
4                Region     Latin America
13              Country             Sudan
14              Country            Russia
15              Country    United Kingdom
16              Country              Peru
17              Country          Malaysia
35              Client              Bank
36              Client              Bank2
37              PG                  PG 1
38              PG                  PG 2
54                Status              High
55                Status            Signed
56                Status               Low

Now I am trying to create a simple chart, where change of variable should lead to changes of plot.

 # UI code

 ui <- fluidPage(theme = shinytheme("united"),
            sidebarLayout(
              sidebarPanel(
                           selectInput("dataInput", "Choose to filter by:",
                                       choices = c("Region",
                                                   "Country",
                                                   "Client",
                                                   "PG",
                                                   "Status"),
                                       selected = "Choose to display by")
              ),


              mainPanel(

                # Output
                tabsetPanel(type = "tabs",
                            # tabPanel("Map", leafletOutput(outputId = 'map', height = 850)),
                            tabPanel("Plot",  plotOutput("plot", height = 850)))
                )
              )
            )

My server looks like this

 server <- function(input, output) {

 # 1. Select among columns
 selectedData <- reactive({

 data_pg_df1[!is.na(data_pg_df1$variable) & data_pg_df1$variable == input$dataInput, ]
 })

 # Output
 output$plot <- renderPlot({
ggplot(data_pg_df1, aes(selectedData)) + geom_bar()
 })

 }
 shinyApp(ui = ui, server = server)

I receive an error Don't know how to automatically pick scale for object of type reactiveExpr/reactive. Defaulting to continuous. Warning: Error in : Column x must be a 1d atomic vector or a list

Tried to resolve, but failed.

Is my approach for ggplot correct? Or I need also add "value" column to data input. I do not think so, because I want to select based on "variable" only. What am I missing?

My desired output looks like this. I have it with the eval(), but would like to do in a tidy way, described above. Plot

output$plot <- renderPlot({
  eval(parse(text = paste0("ggplot(data_pg_df, aes(x=",input$dataInput,")) + geom_bar()"  )))
})

Upvotes: 0

Views: 209

Answers (1)

AleBdC
AleBdC

Reputation: 81

Your problem lies in how you retrieve object from reactive. You can try something like:

output$plot <- renderPlot({
data <- selectedData()
ggplot(data_pg_df1, aes(data)) + geom_bar()
})

See here for reactive helps and here for SO related question. Careful also with ggplot2 syntax...

Upvotes: 1

Related Questions