添加不同的输入后如何使Shiny App显示图表

时间:2019-12-23 16:23:18

标签: r shiny

我想为我的Shiny App寻求帮助,我整天都在工作,但无法正常工作。我的应用程序应该显示有关游戏销售的不同图表。我能够创建工作版本,但是由于我添加了selectInput,因此它拒绝显示任何图表,并且我耗尽了为什么这样做的想法。我将非常感谢您的帮助。 提前非常感谢您!

代码如下:

library(shiny)
library(ggplot2)

#ui.R
ui <- fluidPage(
    titlePanel(""), sidebarLayout(
        sidebarPanel(
            helpText(""),
            tags$hr(),
            fileInput("file","Where csv is located: "), 
            selectInput("att", "Select an attribute", choices=c("NA Sales","Global Sales","EU Sales","Japan Sales","Other Sales"),selected="Global Sales",multiple=FALSE,selectize=TRUE)),
        mainPanel(
            uiOutput("tb"),
            plotOutput("line")             
        )
    )
)

#server.R
server <- function(input,output){

    data <- reactive({

        file1 <- input$file
        if(is.null(file1)){return()} 

        read.csv(file=file1$datapath, sep=",", header=TRUE)})

    output$filedf <- renderTable({
        if(is.null(data())){return ()}
        input$file
    }) 

    output$sum <- renderTable({
        if(is.null(data())){return ()}
        summary(data())
    })

    output$table <- renderTable({
        if(is.null(data())){return ()}
        data()
    })

    output$line <- renderPlot({
        if (is.null(data())) { return() }

    })

        output$map1 <- renderPlot({ 

            if (is.null(input$att)){ warning("select at least one attribute"); return(NULL)}

            else if(identical(input$att,"Global Sales")){
                print(ggplot(data(), aes(table$Genre, table$Global_Sales)) 
                      + geom_col() 
                      + theme(axis.text.x = element_text(angle=90, hjust=1))  
                      + facet_wrap(~Platform)
                )
            }

            else if(identical(input$att,"EU Sales")){
                print(ggplot(data(), aes(table$Genre, table$Global_Sales)) 
                      + geom_col() 
                      + theme(axis.text.x = element_text(angle=90, hjust=1))  
                      + facet_wrap(~Platform)
                )
            }

            else if(identical(input$att,"Japan Sales")){
                print(ggplot(data(), aes(table$Genre, table$Global_Sales)) 
                      + geom_col() 
                      + theme(axis.text.x = element_text(angle=90, hjust=1))  
                      + facet_wrap(~Platform)
                )
            }

            else if(identical(input$att,"NA Sales")){
                print(ggplot(data(), aes(table$Genre, table$Global_Sales)) 
                      + geom_col() 
                      + theme(axis.text.x = element_text(angle=90, hjust=1))  
                      + facet_wrap(~Platform)
                )
            }

            else if(identical(input$att,"Other Sales")){
                print(ggplot(data(), aes(table$Genre, table$Global_Sales)) 
                      + geom_col() 
                      + theme(axis.text.x = element_text(angle=90, hjust=1))  
                      + facet_wrap(~Platform)
                )
            }

            })

        output$tb <- renderUI({if(is.null(data())) h5()
            else
                tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))

    })
}


shinyApp(ui = ui, server = server)

这是我正在使用的csv:https://www.sendspace.com/file/s1dcom

这也是应用程序的外观:

0 个答案:

没有答案