2014-02-13 68 views
3

我有一个R Shiny应用程序,它可以计算不同标签集中的多个统计信息。由于计算量非常大,我使用submitButton来防止反应。我的问题是现在每个计算(全部在不同的制表符集)正在将输出写入一个文件夹,并且我希望Shiny在初始化时为所有制表符写入输出。不幸的是,Shiny只会为标签集创建一个输出,它在初始化时处于活动状态。有没有办法告诉Shiny,它应该在初始化时为每个选项卡计算/渲染输出?R Shiny Tabsets同时处理

下面是从Shiny [教程]修改例:(http://www.http://rstudio.github.io/shiny/tutorial/#more-widgets/

ui.R:

library(shiny) 

# Define UI for dataset viewer application 
shinyUI(pageWithSidebar(

    # Application title. 
    headerPanel("More Widgets"), 

    # Sidebar with controls to select a dataset and specify the number 
    # of observations to view. The helpText function is also used to 
    # include clarifying text. Most notably, the inclusion of a 
    # submitButton defers the rendering of output until the user 
    # explicitly clicks the button (rather than doing it immediately 
    # when inputs change). This is useful if the computations required 
    # to render output are inordinately time-consuming. 
    sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
       choices = c("rock", "pressure", "cars")), 

    numericInput("obs", "Number of observations to view:", 10), 

    helpText("Note: while the data view will show only the specified", 
      "number of observations, the summary will still be based", 
      "on the full dataset."), 

    submitButton("Update View") 
), 

    # Show a summary of the dataset and an HTML table with the requested 
    # number of observations. Note the use of the h4 function to provide 
    # an additional header above each output section. 
    mainPanel(
    tabsetPanel(
     tabPanel("Summary", verbatimTextOutput("summary")), 
     tabPanel("Table", tableOutput("view")) 

    ) 
) 
)) 

server.R:

library(shiny) 
library(datasets) 

# Define server logic required to summarize and view the selected dataset 
shinyServer(function(input, output) { 

    # Return the requested dataset 
    datasetInput <- reactive({ 
    switch(input$dataset, 
      "rock" = rock, 
      "pressure" = pressure, 
      "cars" = cars) 
    }) 

    # Generate a summary of the dataset 
    output$summary <- renderPrint({ 
    dataset <- datasetInput() 

    capture.output(summary(dataset),file="summary.txt") 

    }) 

    # Show the first "n" observations 
    output$view <- renderTable({ 
    a<-head(datasetInput(), n = input$obs) 

    capture.output(a,file="table.txt") 

    }) 
}) 

回答

3

我想你想:

outputOptions(output, "summary", suspendWhenHidden = FALSE) 
outputOptions(output, "view", suspendWhenHidden = FALSE) 

把这个放入你的server.R。让我(我们)知道这是否符合您的预期。

文档:

http://www.inside-r.org/packages/cran/shiny/docs/outputOptions 
+0

感谢@Vincent,它确实适用于我已经发布,但不幸的是在我的应用只有我的四个tabsets的前两个按预期工作,虽然我已经定义'的例子outputOptions'所有四个输出。这真的很奇怪。但它可能适用于其他人,所以+1。 –

+1

如果你可以做一个简单的可重复使用的例子,那么你可能需要将它发布到[Shiny discussion group](https://groups.google.com/forum/?fromgroups=#!forum/shiny-discuss)或者作为Github上的[Issue](https://github.com/rstudio/shiny/issues?state=open)。 – Vincent