使用ggplot创建一个ShinyApp来绘制散点图

时间:2017-07-13 04:25:21

标签: r shiny

我想通过选择上传数据集中的变量来创建一个闪亮的应用程序来绘制散点图。我想用ggplot和plotly创建绘图。我试过的代码如下。但我的应用程序没有给出情节。

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)

在上面的代码中,以下部分似乎不起作用。

output$trendPlot <- renderPlotly({

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

            ggplotly(p) 

        })

1 个答案:

答案 0 :(得分:1)

实际上问题不在output$trendPlot中,而在mainPanel(...)

1:仔细观察,在mainPanel中,您正在调用plotOutput('MyPlot'),但output$MyPlot不存在,只有存在的地块为{{1} }}

2:注意output$trendPlotoutput$trendPlot <- renderPlotly({...})的返回类型为trendPlot,但renderPlotly正在调用plotOutput

所以,修复是

main

修好后,输出如下:

snap1