ggplot2 - R Shiny multiple tabPanel in tabsetPanel not loading -
i have used tabsetpanel in ui.r , each tabpanel there specific plot. plots not require user input rendered. first tab loaded. when switch tabs, blank, , have click on submit button graph load. want load graph when switch tabs. how do that?
below 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 totals 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 totals 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 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 each indicator 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 ui.r code
ui.r
library(shiny) library(ggplot2) shinyui(pagewithsidebar( # application title headerpanel("kemri wellcome trust programme"), # sidebar slider input number of observations sidebarpanel( helptext("note: while data view show specified", "number of observations, summary still based", "on full dataset."), selectinput("locations", "choose 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 plot of 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 , submit button bug fixed in github version of shiny. can install these steps:
install.packages('httpuv', repos=c(rstudio='http://rstudio.org/_packages', cran='http://cran.rstudio.com')) install.packages('devtools') # if don't have devtools installed devtools::install_github('shiny', 'rstudio')
after following these steps, sure restart r process before try again.
Comments
Post a Comment