简体   繁体   中英

Shiny reactive data plotting using ggplot

I am creating a shiny application consisting in plotting data from different datasets.

Basically user has to choose two times using one radiobutton and one selectinput for getting desired plot. Here is a minimal example:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

# create data
name <- c("Jon", "Bill", "Maria")
age <- c(23, 41, 32)
d1 <- data.frame(name, age)
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
d2 <- data.frame(employee, salary, startdate)
library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            radioButtons("ind1", "Choose indicator:",
                         c("Q1" = "q1",
                           "Q2" = "q2")
                         
            )
        ),

        # Show a plot of the generated distribution
        mainPanel(
            selectInput("ind2", "Choose metrics:",
                        c("M1" = "m1",
                          "M2" = "m2"),
            plotOutput("gmplot"))
    
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    # Create a "data_source" reactive variable
    data_intacc <- reactive({
        # Return the appropriate data source depending on
        # the chosen radio button
        if (input$ind1 == "q1" & input$ind2 == "m1") {
            data <- d1
        } else if (input$ind1 == "q2" & input$ind2 == "m2") {
            data <- d2
        } 
        return(data)
    })
    
    
    output$gmplot <-renderPlot({
        data = data_intacc()
        p1 <- ggplot(data, aes(x, y, fill)) +geom_bar(stat= "identity")
        print(p1)
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

My problem is that based on user input data is different, so I need to figure out one way to subset reactive data inside ggplot2 to define aes(x, y) because x and y will be different depending on user input.

Any idea how to deal with this case?

Thank you

You can try something like this.

output$gmplot <- renderPlot({
    ggplot(data = data_intacc(), aes_string(x = input$input1, y = input$input2, fill = input$input3)) + 
      geom_bar(stat = "identity")
  })

You have to specify the inputs in the right way for this to work. If you can share some more of your code, perhaps we can help you more.

Thank you I found the solution. I made a wrap yof aes() function in an if statement:

p1 <- ggplot(data = data_intacc(), 
{if (input$ind_access == "reason_access" & input$dissag_access == "age_access") {
aes(x=value_1, y=value_2, fill= Total)
}}
{if (input$ind_access == "reason_access" & input$dissag_access == "gender_access") {
aes(x=value_3, y=value_4, fill= Total)
}}
)
+geom_bar(stat= "identity")
print(p1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM