闪亮的动态选择输入错误

时间:2014-09-01 07:12:01

标签: r shiny

我从Rstudio闪亮的例子(http://shiny.rstudio.com/gallery/update-input-demo.html)学会了在我的闪亮应用程序中使用动态选择输入。一切似乎都没问题,但发生了错误。我测试了很多,发现错误是由于使用了动态选择输入(server.R中的观察功能)。但我无法弄清楚如何解决它。任何帮助将受到高度赞赏。谢谢! 为了节省空间,一些代码没有显示。

server.R

load("./data/genomicVar.RData")
load("./data/geneInfo.RData")

fetchInfoByMsu <- function(locus="") {...}
fetchSnpByMsu <- function(locus="") {...}
fetchIndelByMsu <- function(locus="") {...}
fetchSvByMsu <- function(locus="") {...}
fetchExpByMsu <- function(locus="") {...}
fetchInfoByBin <- function(binNumber="") {...}
fetchGeneByBin <- function(binNumber="") {...}

shinyServer(function(input, output, session) {
  output$mytable1 = renderDataTable({...})
  output$mytable2 = renderDataTable({...})
  output$mytable3 = renderDataTable({...})
  output$mytable4 = renderDataTable({...})
  output$mytable5 = renderDataTable({...})
  output$mytable6 = renderDataTable({...})
  output$mytable7 = renderDataTable({...})

  observe({
    c_bin <- input$bin
    c_gene <- fetchGeneByBin(input$bin)
    c_gene <- c_gene$locus

    # Select input 
    s_options <- list()
    for (i in c_gene) {
      s_options[[i]] <- i
    }

    # Change values for input$inSelect
    updateSelectInput(session, "inSelect",
                      choices = s_options,
                      selected = c_gene[1]
    )
  })

  output$mytable8 = renderDataTable({...})
  output$mytable9 = renderDataTable({...})
  output$mytable10 = renderDataTable({...})
  output$mytable11 = renderDataTable({...})
  output$mytable12 = renderDataTable({...})
})

UI.R

shinyUI(fluidPage(

  fluidRow(
    absolutePanel(
      textInput("msu", label = h4("MSU genomic locus:"), 
                value = "LOC_Os07g15770"),
      tabsetPanel(
        tabPanel(strong('Information'), dataTableOutput("mytable1")),
        tabPanel(strong('SNP'), dataTableOutput("mytable2")),
        tabPanel(strong('Indels'), dataTableOutput("mytable3")),
        tabPanel(strong('SVs'), dataTableOutput("mytable4")),
        tabPanel(strong('Expression'), dataTableOutput("mytable5"))
      ),

      br(),
      p(HTML("<b><div style='background-color:#FADDF2;border:1px solid
             blue;'></div></b>")),

      textInput("bin", label = h4("Bin ID:"), value = "Bin1078"),
      tabsetPanel(
        tabPanel(strong('Information'), dataTableOutput("mytable6")),
        tabPanel(strong('Gene'), dataTableOutput("mytable7"))
      ),

      wellPanel(
        selectInput("inSelect", strong("Select gene:"),
                    c("gene 1" = "option1",
                      "gene 2" = "option2"))
      ),

      tabsetPanel(
        tabPanel(strong('Information'), dataTableOutput("mytable8")),
        tabPanel(strong('SNP'), dataTableOutput("mytable9")),
        tabPanel(strong('Indels'), dataTableOutput("mytable10")),
        tabPanel(strong('SVs'), dataTableOutput("mytable11")),
        tabPanel(strong('Expression'), dataTableOutput("mytable12"))
      ),

      br(),
      p(HTML("<b><div style='background-color:#FADDF2;border:1px solid
             blue;'></div></b>")),


      right=5, left=10
    )
  )

))

1 个答案:

答案 0 :(得分:3)

我更喜欢将uiOutput用于动态输入,请参阅此最小示例:

<强> ui.R

shinyUI(fluidPage(
  fluidRow(
    absolutePanel(
      #select bin
      textInput("bin", label = h4("Bin ID:"), value = 1),
      #dynamic options based on selected bin
      uiOutput("inSelect")
    )
  )
)
)

<强> server.R

shinyServer(function(input, output, session){

  #genes dataframe
  df <- data.frame(bin=c(1,1,1,2,2,2),
                   gene=c(12,13,14,21,23,24))

  #dynamic select
  output$inSelect <- renderUI({
    selectInput("inSelect", strong("Select gene:"),
                choices = df[ df$bin==input$bin,"gene"])
  })
})