本文介绍了selectInput并绘制渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
欢迎.
我试图使用selectInput显示图.我将selectInput设置为名称,标题,选项.在主要我添加六个情节...
I tried to show plots using selectInput.I set the selectInput with name, title, options.In main i add six plot...
如何使其工作?没有错误,所有图都显示在一页上.
How to make it work ? There is no error and all plots is showing on one page.
.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(两个地块,其他都有半代码)
.server (two plots, other have semi code)
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
})
推荐答案
因此,如果我理解正确,那么您一次只想基于selectInput的值绘制ggplot之一即可.
So if I understand correctly you want to plot only one of the ggplot at the time based on the value of the 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
})
})
这篇关于selectInput并绘制渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!