【问题标题】:R Shiny multiple tabPanel in tabsetPanel not loadingtabsetPanel中的R Shiny多个tabPanel未加载
【发布时间】:2013-05-16 06:33:59
【问题描述】:

我在我的 ui.R 中使用了 tabsetPanel,每个 tabPanel 都有一个特定的情节。这些图不需要渲染任何用户输入。但只加载了第一个选项卡。当我切换选项卡时,它们是空白的,我必须单击提交按钮才能加载图表。我想在切换标签时加载图表。我该怎么做?

下面是我的 server.R 代码

服务器.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"))
    })
  })

下面是我的 ui.R 代码

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")
    )

  )
 ))

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    选项卡和提交按钮之间奇怪的交互是一个错误,已在 Shiny 的 GitHub 版本中修复。您可以通过以下步骤安装它:

    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')
    

    按照这些步骤操作后,请务必重新启动您的 R 进程,然后再重试。

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 2013-10-28
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2014-05-20
      • 2021-10-27
      • 2013-07-13
      相关资源
      最近更新 更多