user7892705
user7892705

Reputation: 147

Create a ShinyApp to draw scatterplots using ggplot

I want to create a shiny app to draw scatterplots by selecting the variables in the uploaded data set to shiny app. I want to create plots using ggplot and plotly. The code I tried is as follow. But my app does not give the plot.

library(shiny)
library(datasets)
library(plotly)

ui <- shinyUI(fluidPage(
    titlePanel("Column Plot"),
    tabsetPanel(
        tabPanel("Upload File",
                         titlePanel("Uploading Files"),
                         sidebarLayout(
                            sidebarPanel(
                                fileInput('file1', 'Choose CSV File',
                                                    accept=c('text/csv', 
                                                                     'text/comma-separated-values,text/plain', 
                                                                     '.csv')),


                                tags$br(),
                                checkboxInput('header', 'Header', TRUE),
                                radioButtons('sep', 'Separator',
                                                         c(Comma=',',
                                                            Semicolon=';',
                                                            Tab='\t'),
                                                         ','),
                                radioButtons('quote', 'Quote',
                                                         c(None='',
                                                            'Double Quote'='"',
                                                            'Single Quote'="'"),
                                                         '"')

                            ),
                            mainPanel(
                                tableOutput('contents')
                            )
                         )
        ),
        tabPanel("First Type",
                         pageWithSidebar(
                            headerPanel('My First Plot'),
                            sidebarPanel(

                                # "Empty inputs" - they will be updated after the data is uploaded
                                selectInput('xcol', 'X Variable', ""),
                                selectInput('ycol', 'Y Variable', "", selected = "")

                            ),
                            mainPanel(
                                plotOutput('MyPlot')
                            )
                         )
        )

    )
)
)

server <- function(input, output, session) {

    data <- reactive({ 
        req(input$file1) 

        inFile <- input$file1 

        df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
                                     quote = input$quote)


        updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                                            choices = names(df), selected = names(df))
        updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                                            choices = names(df), selected = names(df)[2])

        return(df)
    })

    output$contents <- renderTable({
        data()
    })

    output$trendPlot <- renderPlotly({

        # build graph with ggplot syntax
        p <- ggplot(data(), aes_string(x = input$xcol, y = input$ycol)) + geom_point()

        ggplotly(p) 

    })
}

shinyApp(ui, server)

In the above code, the following section doesn't seem to work.

output$trendPlot <- renderPlotly({

            # build graph with ggplot syntax
            p <- ggplot(data(), aes_string(x = input$xcol, y = input$ycol)) + geom_point()

            ggplotly(p) 

        })

Upvotes: 0

Views: 82

Answers (1)

parth
parth

Reputation: 1631

Actually the problem is not in output$trendPlot but in mainPanel(...)

1 : Look closely, in mainPanel, you're calling plotOutput('MyPlot') but output$MyPlot doesn't exist, only plot that exists is output$trendPlot.

2 : Note output$trendPlot <- renderPlotly({...}), return type of trendPlot is renderPlotly but plotOutput is being called at main.

So, fix is

mainPanel(
    plotlyOutput('trendPlot')
)

After fixing this, output is as follows :

snap1

Upvotes: 1

Related Questions