2016-05-11 4 views
0

Добро пожаловать.selectInput and plot render

Я попытался показать графики, используя selectInput. Я установил selectInput с именем, названием, параметрами. В основном я добавляю шесть сюжетов ...

Как заставить его работать? Ошибка отсутствует, и все графики отображаются на одной странице.

.ui

library(shiny) 

shinyUI(fluidPage(
    headerPanel("Japan TeleCOM"), 

    includeCSS("styles.css"), 
    # 
    # Application title 
    titlePanel("Subscribers Market Share in Japan for Mobile Prepaid and Postpaid market and its' competition in 2000-2013"), 

    sidebarPanel 
    (

     selectInput("statename", "Select plot", c("plotOne", "plotTwo", "plotThree", "plot4th", "plot5th", "plot6th"), selected = "PlotOne") 

    ), 

    # Show a plot of the generated distrisbution 
    mainPanel(
     plotOutput("plotOne"), 
     plotOutput("plotTwo"), 
     plotOutput("plotThree"), 
     plotOutput("plot4th"), 
     plotOutput("plot5th"), 
     plotOutput("plot6th") 
    ) 


)) 

.server (два участка, другой полугодовым код)

ibrary(shiny) 
library(xlsx) # to read excel files 
library(ggplot2) # to plot 
library(scales) # to describe values on the plot 2,000,000 instead of 2000000 

dataFromExcel <- read.xlsx(file="japan_telecom_dane.xlsx", sheetIndex=1,header=TRUE) 
dataFromExcel2 <- read.xlsx(file="japan_telecom_dane_perc.xlsx", sheetIndex=1,header=TRUE) 
###FIRST PLOT##### 

shinyServer(function(input, output) { 

    df <- dataFromExcel 
    df2 <- dataFromExcel2 

    output$plotOne <- renderPlot({ 

    df$Date <- as.Date(as.character(df$Date), format="%Y-%m-%d") 
    x <- df$Date # first column with Date 
    y <- df[ , 2:length(df)] # (all columns from df without the first one, the first column was x = Date) 
    plotGgplot <- ggplot() + 
     geom_line(data = df, aes(x = x, y = y$nttdocomo_prepaid, color=" nttdocomo_prepaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$nttdocomo_postpaid, color=" nttdocomo_postpaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$softbank_prepaid, color=" softbank_prepaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$softbank_postpaid, color=" softbank_postpaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$kddi_prepaid, color=" kddi_prepaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$kddi_postpaid, color=" kddi_postpaid "), linetype = 1, size = 1.6) + 
     ylab('Number of Subscribers') + 
     xlab('Year') + 
     scale_y_continuous (labels = comma, breaks = seq(from=0,to=190000000,by=5000000)) + 
     ggtitle("Subscribers in Japan for main privider and its' competition in 2000-2013") + 
     theme(plot.title=element_text(size=8, face="bold", 
            hjust = 0.5), 
      axis.title=element_text(size=8)) 
    plotGgplot 
    }) 

    #####Second PLOT###### 

    output$plotTwo <- renderPlot({ 

    df$Date <- as.Date(as.character(df$Date), format="%Y-%m-%d") 
    x <- df$Date # first column with Date 
    y <- df[ , 2:length(df)] # (all columns from df without the first one, the first column was x = Date) 
    plotGgplot <- ggplot() + 
     geom_line(data = df, aes(x = x, y = y$nttdocomo_postpaid, color=" nttdocomo_postpaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$softbank_postpaid, color=" softbank_postpaid "), linetype = 1, size = 1.6) + 
     geom_line(data = df, aes(x = x, y = y$kddi_postpaid, color=" kddi_postpaid "), linetype = 1, size = 1.6) + 
     ylab('Number of Subscribers') + 
     xlab('Year') + 
     scale_y_continuous (labels = comma, breaks = seq(from=0,to=190000000,by=20000)) + 
     ggtitle("Subscribers in Japan for main privider and its' competition in 2000-2013") + 
     theme(plot.title=element_text(size=8, face="bold", 
            hjust = 0.5), 
      axis.title=element_text(size=8)) 
    plotGgplot 
    }) 

ответ

1

Так что, если я правильно понимаю, что вы хотите построить только один из ggplot в то время на основе значение selectInput.

Вы можете сделать это с помощью:

UI.R

library(shiny) 

shinyUI(fluidPage(
    headerPanel("SO Test"), 
    titlePanel("Test"), 
    # Your input selection 
    sidebarPanel(
    selectInput("plotnumber", "Select plot", c("Bubble", "Line"), selected = "Bubble") 
), 
    # Show the selected plot 
    mainPanel(
    plotOutput("whichplot") 
) 
)) 

и SERVER.R

library(shiny) 
library(ggplot2) 
library(scales)  

shinyServer(function(input, output) { 

    #Random dataframe 
    df <- data.frame(x = 1:100, y = rnorm(100)) 

    # If conditions determining which plot should be used 
    output$whichplot <- renderPlot({ 
    if(input$plotnumber == 'Bubble'){ 
     G = ggplot(df, aes(x = x, y = y)) + 
     geom_point() 
    } 
    if(input$plotnumber == 'Line'){ 
     G = ggplot(df, aes(x = x, y = y)) + 
     geom_line() 
    } 
    G 
    })  

}) 
+1

Спасибо человек !! Ты помог мне ! –