【问题标题】:Create a ShinyApp to draw scatterplots using ggplot创建一个 ShinyApp 以使用 ggplot 绘制散点图
【发布时间】:2017-07-13 04:25:21
【问题描述】:

我想创建一个闪亮的应用程序来绘制散点图,方法是选择上传数据集中的变量到闪亮的应用程序。我想使用 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) 

        })

【问题讨论】:

  • 如果首先尝试缩小代码的哪个部分不起作用以及具体问题是什么,您将获得更好的帮助

标签: r shiny


【解决方案1】:

其实问题不在output$trendPlot,而是在mainPanel(...)

1 : 仔细观察,在mainPanel 中,您调用的是plotOutput('MyPlot')output$MyPlot 不存在,唯一存在的情节是output$trendPlot

2 :注意output$trendPlot &lt;- renderPlotly({...})trendPlot 的返回类型是renderPlotly,但plotOutput 被调用main

所以,修复是

mainPanel(
    plotlyOutput('trendPlot')
)

修复后,输出如下:

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2014-04-18
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多