闪亮:如何从observe()返回变量

时间:2014-08-29 23:33:16

标签: r shiny

以下代码我的工作正如我打算申请的第一部分:

ui.R

library(shiny)
library(shinyAce)

shinyUI(fluidPage(

tags$head(
tags$link(rel="stylesheet", type="text/css", href="styles.css")
    ),


headerPanel(
list(HTML('<img src="photo.jpg" HEIGHT="70" WIDTH="60" BORDER="0"/><font face="Myriad Pro""  
color="#005691"><br>APP</font>')),

),

sidebarPanel(

selectInput("simtype", "Select Simulation Type", c(SingleSubject="SS", FromFile="FF" ),   
selected="FF", multiple=FALSE),

conditionalPanel(condition="input.simtype=='FF' ",

  fileInput('file1', 'Upload File Here',
            accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),


  tags$hr(),

  checkboxInput('header', 'Header', TRUE),

  radioButtons('sep', 'Separator',
               c(Comma=',',
                 Semicolon=';',
                 Tab='\t'),
               'Comma'),

  uiOutput('varselect1')

),

conditionalPanel(condition="input.simtype== 'SS' ",

                helpText(HTML("
                              <ul>
                              <li>Insert SUVmax values in black window below</li>
                              <li>Each row corresponds to a timepoint</li>
                              <li>Enter each lesion's SUVmax value separated by a space</li>
                              </ul>")),


       aceEditor("text", "",
       mode="r", theme="twilight", height="40px",fontSize = 10 ),

                uiOutput('varselect2')

                )
),


mainPanel(




tabsetPanel(

tabPanel(title='FILE', value="filetab", loadingPanel, tableOutput("filetable")),
tabPanel(title='PLOT', value="plottab", loadingPanel, plotOutput("histogram")),
tabPanel(title="SUMMARY", value="summarytab",loadingPanel,dataTableOutput("summarytable")),
id="tabs"


        ) 
      )
))

server.R

library(shiny)
library(shinyAce)

shinyServer(


function(input, output, session) {


 observe({   

   if (input$simtype=="FF"){  

    csvfile <- reactive({
    csvfile <- input$file1
    if (is.null(csvfile)){return(NULL)}
    read.csv(csvfile$datapath, header=input$header, sep=input$sep,   quote=input$quote)
  })

  output$varselect1 <- renderUI({
      if(is.null(input$file1$datapath)){return()} 

      output$filetable <- renderTable({
    #isolate(input$tabs)
      csvfile()
    })


list(
  checkboxGroupInput("var", "Variables", choices = names(csvfile()), select = names(csvfile())),
  numericInput("s0", "Noise Distribution variance:", NULL),
  numericInput("n.it",'Number of Simulations: ', 1000),
  actionButton("goButton",'Create Plot and Summary Statistics'))

 }) # end renderUI()

 }   # end if

}) # end observe  

observe({  
  if (input$simtype=="SS"){     # Single-Subject Input

   output$varselect2<- renderUI({

     get.text <- reactive({
       input$text
     })  

     if(input$text == ""){return()}

     output$filetable <- renderTable({
       X<- read.table(text=get.text())
       j=1:ncol(X)
       i=1:nrow(X)
       colnames(X) <- paste("Lesion", j, sep=" ")  
       rownames(X) <- paste("Timepoint", i, sep=" ")
       X  
     })


    list(
     #
       # aceEditor("text", "Enter SUV vals. here",     
            # mode="r", theme="twilight", height="40px",fontSize = 10 ),


   numericInput("s0", "Noise Distribution variance:", NULL),
   numericInput("n.it",'Number of Simulations: ', 1000),
   actionButton("goButton",'Create Plot and Summary Statistics'))


   }) #end renderUI()
 } #end if       


 }) #end observe 

})

即,我选择一个以selectInput为条件的面板,然后根据该选择更改UI。问题是,当我的条件逻辑包含在上面的observe()调用中时,这似乎才有效,这样我就可以在文件选项卡上呈现正确的输出表,具体取决于通过selectInput选择的输出类型。

现在,我想使用两种情况的输出,即我想在server.R中的后续函数调用中访问csvfile()和X(),这意味着我需要在外部访问它们observe()调用。不知道该怎么做。任何人都可以指出我正确的方向或建议替代?我意识到这可能只是一个流量控制问题,但无论出于何种原因,我都没有看到它。

感谢。

1 个答案:

答案 0 :(得分:1)

observe()没有返回值

reactive()会

通常你可以做类似

的事情
return_a_value <- reactive({... })

observe({
   ...
  something <- return_a_value()
  ....
})