R在tabsetPanel中闪亮的多个tabPanel不加载 [英] R Shiny multiple tabPanel in tabsetPanel not loading
问题描述
我在我的ui.R中使用了tabsetPanel,每个tabPanel都有一个特定的图。这些图不需要渲染任何用户输入。但只有第一个标签被加载。当我切换标签页时,它们是空白的,我必须点击提交按钮才能加载图表。我想在切换标签时加载图表。我怎么做?
以下是我的server.R代码
server.R
$ p $
library(shiny)
library(ggplot2)
anc_data< - read.csv(anc_indicator .csv,header = TRUE)
shinyServer(函数(输入,输出){
输出$ piePlot< - renderPlot({
#为每个指标的分组生成总计饼图
总计< -tapply(anc_data $ Total,anc_data $ Indicator,sum)
graphdatapie< - as.data.frame(总计)
指标< -rownames(graphdatapie)
c< - ggplot(graphdatapie,aes(x = Indicator,y = Total,fill = Totals),environment = environment())+ geom_bar(width = 1,stat =identity)
print(c + coord_polar(theta =y))
})
输出$ histPlot< - renderPlot({
#为每个指标的分组生成总计的直方图
indicatorTotals< - tapply(anc_data $ Total,anc_data $ Indicator,sum)
graphdatahist< - as.data.frame(indicatorTotals)
c < - ggplot(graphdatahist,aes(x = rownames(graphdatahist),y = indicatorTotals,fill = indicatorTotals),environment = environment())
print( c + geom_bar(width = 1,stat =identity)+ xlab(Indicators)+ ylab(ANC总计))
})
输出$ boxPlot< - renderPlot({
#)为每个指标生成箱形图
indicatorTotals< - tapply(anc_data $ Total,anc_data $ Indicator,sum)
graphdatabox< - as.data.frame(indicatorTotals)
指标< -anc_data $指标
访问< - anc_data $总计
c< - ggplot(anc_data,aes(指标,访问),environment = environment())
print(c + geom_boxplot(outlier.colour =green,outlier.size = 3,aes(fill = Indicators))+ geom_jitter())
})
输出$ violinPlot< ; - renderPlot({
#generate violin plot
indicatorTotals< - tapply(anc_data $ Total,anc_data $ Indicator,sum)
graphdataviolin< - as.data.frame(indicatorTotals)
I ndicators< -anc_data $指标
访问<-anc_data $总计
c < - ggplot(anc_data,aes(指标,访问),environment = environment())
print(c + geom_violin(aes(fill = Indicators))+ geom_jitter(height = 0))
})
输出$ timeSeriesPlot< - renderPlot({
#generate所有时期的每个指标的时间序列图
指标< -anc_data $指标
c < - ggplot(anc_data,aes(group = factor(anc_data $指标),x = anc_data $期间,y = anc_data $ Total),environment = environment())
print(c + geom_line(aes(color = Indicators),size = 2,alpha = 0.5)+ xlab(Months)+ ylab(ANC Total Visits ))
})
})
以下是我的ui.R代码
ui.R
库(闪亮)
库(ggplot2)
shinyUI(pageWithSidebar(
)应用程序标题
headerPanel(KEMRI Wellcome Trust Program),
#边栏,带有用于nu的滑块输入(请注意:尽管数据视图将只显示指定的,
个观测值,但汇总仍将基于,
),
selectInput(locations,Choose a location:,
choices = c(Kilifi District Hospital,
Bungoma区医院,
锡卡区医院)),
submitButton(更新视图)
#sliderInput(obs,观察次数:,最小= 0,最大= 1000,value = 500)
),
#显示生成的分布图
mainPanel(
tabsetPanel(
tabPanel(Histogram ,plotOutput(histPlot),id =hist),
tabPanel(Pie,plotOutput(piePlot),id =pie),
tabPanel(Time Series, plotOutput(timeSeriesPlot),id =time),
tabPanel(Box,plotOutput(boxPlot),id =box),
tabPanel(Violin,plotOutput violinPlot),id =violin)
)
)
))
标签和提交按钮之间的奇怪交互是在GitHub版本的Shiny中修复的错误。您可以通过以下步骤安装它:
install.packages('httpuv',repos = c(RStudio ='http:/ /rstudio.org/_packages',CRAN ='http://cran.rstudio.com'))
install.packages('devtools')#如果你还没有安装devtools
devtools :: install_github('shiny','rstudio')
执行以下步骤后,请确保重新启动您的R过程,然后再试。
I have used tabsetPanel in my ui.R and for each tabPanel there is a specific plot. The plots do not require any user input to be rendered. But only the first tab is loaded. When I switch tabs, they are blank, and I have to click on a submit button for the graph to load. I want to load the graph when I switch tabs. How do i do that?
Below is my server.R code
server.R
library(shiny)
library(ggplot2)
anc_data <- read.csv("anc_indicator.csv", header = TRUE)
shinyServer(function(input, output) {
output$piePlot <- renderPlot({
# generate pie chart for Totals for the grouping of each indicator
Totals<-tapply(anc_data$Total,anc_data$Indicator,sum)
graphdatapie <- as.data.frame(Totals)
Indicators <-rownames(graphdatapie)
c <- ggplot(graphdatapie, aes(x=Indicators,y=Totals,fill = Totals),environment=environment()) + geom_bar(width = 1,stat="identity")
print(c + coord_polar(theta = "y"))
})
output$histPlot <- renderPlot({
# generate histogram for Totals for the grouping of each indicator
indicatorTotals <- tapply(anc_data$Total,anc_data$Indicator,sum)
graphdatahist <- as.data.frame(indicatorTotals)
c <- ggplot(graphdatahist, aes(x=rownames(graphdatahist),y=indicatorTotals,fill =indicatorTotals),environment=environment())
print(c + geom_bar(width = 1,stat="identity")+ xlab("Indicators")+ylab("ANC Totals"))
})
output$boxPlot <- renderPlot({
# generate box plot for each indicator
indicatorTotals <- tapply(anc_data$Total,anc_data$Indicator,sum)
graphdatabox <- as.data.frame(indicatorTotals)
Indicators <-anc_data$Indicator
Visits <- anc_data$Total
c <- ggplot(anc_data, aes(Indicators,Visits),environment=environment())
print(c + geom_boxplot(outlier.colour = "green", outlier.size = 3,aes(fill = Indicators))+ geom_jitter())
})
output$violinPlot <- renderPlot({
# generate violin plot
indicatorTotals <- tapply(anc_data$Total,anc_data$Indicator,sum)
graphdataviolin <- as.data.frame(indicatorTotals)
Indicators <-anc_data$Indicator
Visits <- anc_data$Total
c <- ggplot(anc_data, aes(Indicators,Visits),environment=environment())
print(c + geom_violin(aes(fill = Indicators))+ geom_jitter(height = 0))
})
output$timeSeriesPlot <- renderPlot({
# generate time series graph for each indicator for all periods
Indicators <-anc_data$Indicator
c <- ggplot(anc_data, aes(group=factor(anc_data$Indicator),x=anc_data$Period,y=anc_data$Total),environment=environment())
print(c + geom_line(aes(colour = Indicators),size=2, alpha=0.5)+ xlab("Months") + ylab("ANC Total Visits"))
})
})
Below is my ui.R code
ui.R
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
# Application title
headerPanel("KEMRI Wellcome Trust Programme"),
# Sidebar with a slider input for number of observations
sidebarPanel(
helpText("Note: while the data view will show only the specified",
"number of observations, the summary will still be based",
"on the full dataset."),
selectInput("locations", "Choose a location:",
choices = c("Kilifi District Hospital",
"Bungoma District Hospital",
"Thika District Hospital")),
submitButton("Update View")
#sliderInput("obs","Number of observations:", min = 0, max = 1000, value = 500)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Histogram",plotOutput("histPlot"),id="hist"),
tabPanel("Pie",plotOutput("piePlot"),id="pie"),
tabPanel("Time Series",plotOutput("timeSeriesPlot"),id="time"),
tabPanel("Box",plotOutput("boxPlot"),id="box"),
tabPanel("Violin",plotOutput("violinPlot"),id="violin")
)
)
))
The weird interaction between tabs and the submit button is a bug that is fixed in the GitHub version of Shiny. You can install it with these steps:
install.packages('httpuv', repos=c(RStudio='http://rstudio.org/_packages', CRAN='http://cran.rstudio.com'))
install.packages('devtools') # if you don't already have devtools installed
devtools::install_github('shiny', 'rstudio')
After following these steps, be sure to restart your R process before you try again.
这篇关于R在tabsetPanel中闪亮的多个tabPanel不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!