欢迎。
我尝试使用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
})
答案 0 :(得分:1)
所以,如果我理解正确,你想根据selectInput的值只绘制一个ggplot。
你可以这样做:
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
})
})