闪亮和条件陈述

时间:2014-09-01 21:48:33

标签: r shiny

我有一个闪亮的服务器.R正在尝试执行以下操作:

我在侧边栏上有一个selectInput,它允许用户选择2个条件中的一个,随后更改UI,它由条件面板组成,每个条件对应一个。

我的server.R按以下方式设置:

if (input$option1) {return()}
else{run code corresponding to option 2 which outputs reactive variable 2}

if (input$option2) {return()}
else{run code corresponding to option 1 which outputs reactive variable 1}
.
.
.
run another function that depends on variable 1 or variable 2, depending on which input is chosen above

上述代码不起作用的原因是因为为了引用input $ option *,我需要处于被动环境中。但很明显,我不能把它放在isolate()调用中,因为我需要UI来响应用户输入。显然我不能把它放在observe()调用中,因为我需要能够根据条件逻辑输出变量1或变量2。那么我该如何完成我想要完成的任务呢?如有必要,我可以提供我的代码,但我认为这更像是设计/程序流程问题。

感谢。代码如下:

runApp(list(
  ui = fluidPage(
    headerPanel("App"),

    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' ",
        aceEditor(
          "text",
          "",
          mode = "r",
          theme = "twilight",
          height = "40px",
          fontSize = 10
        ),
        uiOutput('varselect2')
      )
    ),
    mainPanel(#uiOutput('tabs')
      tabsetPanel(
        tabPanel(title = 'FILE', value = "filetab", tableOutput("filetable")),
        tabPanel(
          title = "SUMMARY",
          value = "summarytab",
          dataTableOutput("summarytable")
        ),
        id = "tabs"
      ))
  ),

  server = function(input, output) {
    if (input$simtype == "SS") {
      return()
    }  else{
      file <- reactive({
        file <- input$file1
      })
      output$filetable <- renderTable({
        file()
      })

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

      list(actionButton("goButton", 'GO'),
           numericInput("s1", "Input value 1", NULL))

    }) # end renderUI()

    if (input$simtype == "FF") {
      return()
    } else{
      get.text <- reactive({
        input$text
      })
      output$filetable <- renderTable({
        X <- read.table(text = get.text())
        X
      }) # end renderTable()

    } #end if
    output$varselect2 <- renderUI({
      if (input$text == "") {
        return()
      }

      list(numericInput("s0", "Input value", NULL),
           actionButton("goButton", 'GO'))

    })
    #end renderUI()
    mydata <- reactive({
      input$goButton
      if (input$simtype == "FF") {
        variable = file()
      } else {
        variable = X()
      }
    }) #end mydata()
    output$summarytable <- renderDataTable({
      variable
    })
  }
))

1 个答案:

答案 0 :(得分:2)

我很难理解你想要做什么。我试图移动代码并重新格式化,看看是否有帮助。我真的不知道你为这些程序提供了什么数据,所以我没有一个好的方法来测试。

library(shiny)
library(shinyAce)
runApp(list(
  ui = fluidPage(
    headerPanel("App"),
    sidebarPanel(
      selectInput("simtype", "Select Simulation Type", list(SingleSubject="SS", FromFile="FF" ),  selected="SS", multiple=FALSE),

      conditionalPanel(condition="input.simtype=='FF' ",
        fileInput('file1', 'Upload File Here', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
        checkboxInput('header', 'Header', TRUE),
        radioButtons('sep', 'Separator', c(Comma=',', Semicolon=';', Tab='\t'),',') #change 'Comma' to ','

      ),
      conditionalPanel(condition="input.simtype== 'SS' ",
        aceEditor("text", "", mode="r", theme="twilight", height="40px",fontSize = 10 )
      ),
      uiOutput('varselect')
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(title='FILE', value="filetab", tableOutput("filetable")),
        tabPanel(title="SUMMARY", value="summarytab", dataTableOutput("summarytable")),
      id="tabs") 
    )
   ),
  server = function(input, output) {

    userdata <- reactive({  
      x<-if(input$simtype=="SS" && input$text != "") {
        read.table(text=input$text)
      } else if (input$simtype=="FF" && !is.null(input$file1$datapath)) {
        read.table(input$file1$datapath)
      } else {
        data.frame()
      }
      x
    })

    output$varselect <- renderUI({
      if(nrow(userdata())>0){list()} 
      list(
        numericInput("s1", "Input value 1", NULL),
        actionButton("goButton",'GO')
      )
    }) # end renderUI()

    currentdata <- reactive({
      input$goButton        
      isolate(userdata())
    })

    output$filetable <- renderTable({
      currentdata()
    })

    output$summarytable<- renderDataTable({
      currentdata()
    })

  } #end server
))