首页 > 解决方案 > 如何从闪亮的导入数据中提取信息并将其用作文本输入?

问题描述

我想创建一个带有 R 闪亮的应用程序,我在其中加载数据,显示它,然后我可以对另一个 tibble/数据帧中的数据进行一些操作,并将原始数据显示在选项卡中。此外,我想使用 tibble 单元格中的数据作为 textInput 的值。到目前为止,我已经为 UI 提出了这个:

ui <- 
  dashboardPage(
    dashboardHeader(title="NGS library quantification app", titleWidth=500),
    dashboardSidebar(
    menuItem(text = "Raw Data", tabName = "r_data", icon=icon("clipboard")),
    menuItem(text = "User Data", tabName = "u_data", icon=icon("clipboard")),
    menuItem(text = "Analysis", tabName = "analysis", icon=icon("cog", lib = "glyphicon")),
    menuItem(text = "Summary", tabName = "summary", icon=icon("book")),
    collapsed=TRUE),
    dashboardBody(
      tags$head( 
        tags$style(HTML(".main-sidebar { font-size: 30px; }")) #change the font size to 30
      ),
      tags$script(
        HTML(
          "
                        $(document).ready(function(){
          // Bind classes to menu items, easiet to fill in manually
          var ids = ['r_data','u_data','analysis', 'summary'];
          for(i=0; i<ids.length; i++){
          $('a[data-value='+ids[i]+']').addClass('my_subitem_class');
          }

          // Register click handeler
          $('.my_subitem_class').on('click',function(){
          // Unactive menuSubItems
          $('.my_subitem_class').parent().removeClass('active');
          })
          })
          "
        )
        ),


      tabItems(
#Raw data tab              
tabItem(tabName = "r_data", fileInput(inputId = "file", label = "Choose file"), tableOutput("info"), tableOutput("raw_data")),

#user data tab
tabItem(tabName = "u_data",
            column(textInput("ID1", "Sample ID", value = ""),
                   textInput("ID2", "", value = ""),
                   textInput("ID3", "", value = ""),
                   textInput("ID4", "", value = ""),
                   textInput("ID5", "", value = ""),
                   textInput("ID6", "", value = ""),
                   textInput("ID7", "", value = ""),
                   textInput("ID8", "", value = ""),
                   width=1),  
            column(numericInput("size1", "library size (bp)", value = NULL, step=1), 
                        numericInput("size2", "", value = NULL, step=1),
                        numericInput("size3", "", value = NULL, step=1),
                        numericInput("size4", "", value = NULL, step=1),
                        numericInput("size5", "", value = NULL, step=1),
                        numericInput("size6", "", value = NULL, step=1),
                        numericInput("size7", "", value = NULL, step=1),
                        numericInput("size8", "", value = NULL, step=1),
                        width = 2),
              column(numericInput("dil1", "First dilution", value = 100000, step=10000), 
                     numericInput("dil2", "", value = 100000, step=10000),
                     numericInput("dil3", "", value = 100000, step=10000),
                     numericInput("dil4", "", value = 100000, step=10000),
                     numericInput("dil5", "", value = 100000, step=10000),
                     numericInput("dil6", "", value = 100000, step=10000),
                     numericInput("dil7", "", value = 100000, step=10000),
                     numericInput("dil8", "", value = 100000, step=10000),
                     width = 2),
              column(numericInput("dil9", "Second dilution", value = 250000, step=10000), 
                     numericInput("dil10", "", value = 250000, step=10000),
                     numericInput("dil11", "", value = 250000, step=10000),
                     numericInput("dil12", "", value = 250000, step=10000),
                     numericInput("dil13", "", value = 250000, step=10000),
                     numericInput("dil14", "", value = 250000, step=10000),
                     numericInput("dil15", "", value = 250000, step=10000),
                     numericInput("dil6", "", value = 250000, step=10000),
                     width = 2),
              column(numericInput("dil17", "Third dilution", value = 500000, step=10000), 
                     numericInput("dil18", "", value = 500000, step=10000),
                     numericInput("dil19", "", value = 500000, step=10000),
                     numericInput("dil20", "", value = 500000, step=10000),
                     numericInput("dil21", "", value = 500000, step=10000),
                     numericInput("dil22", "", value = 500000, step=10000),
                     numericInput("dil23", "", value = 500000, step=10000),
                     numericInput("dil24", "", value = 500000, step=10000),
                     width = 2),
              column(textInput("check1", "Sample Detected?", "YES"),
                     textInput("check2", "", "YES"),
                     textInput("check3", "", "YES"),
                     textInput("check4", "", "YES"),
                     textInput("check5", "", "YES"),
                     textInput("check6", "", "YES"),
                     textInput("check7", "", "YES"),
                     textInput("check8", "", "YES"),
                     width = 2) 
                    )

然后对于服务器:

server <- function(input, output, session){

# Reading data from CFX exported file      
    data <- reactive({
          inFile <- input$file
            if(is.null(inFile))
              return(NULL)

          data <- read_excel(paste(inFile$datapath), sheet=1, skip=18, col_types = c("text", "text", "text", 
                                                                                                      "text", "numeric", "numeric", "numeric", 
                                                                                              "numeric", "numeric"))
          })
ID1 <- reactive({data()[12, 2]})
updateTextInput(session, "ID1", value = reactive({ID1()}))  
}

这些只是我希望对评估这个问题有所帮助的片段。问题是当我返回用户数据并查看样本 ID 而不是查看样本 ID 时,我看到的似乎是 ID1 变量的代码?我不知道我做错了什么。我是 Shiny 的新手,无法真正解决这个问题。你可以在这里找到应用程序

这是中的输出textInput("ID1", "Sample ID", value = "")

structure(function () ,{,    .dependents$register(),    if (.invalidated || .running) {,        ..stacktraceoff..(self$.updateValue()),    },    .graphDependsOnId(getCurrentContext()$id, .mostRecentCtxId),    if (.error) {,        stop(.value),    },    if (.visible) ,        .value,    else invisible(.value),}, observable = <environment>)

但我期待单元格中的值。任何帮助将不胜感激。谢谢大家

标签: rshiny

解决方案


您需要进行一些更改:

  1. 把你的updateTextInput里面一个观察者。这是因为您需要从反应式表达式中读取,并且observe这样做。
  2. 您需要先解析ID1()usingas.character才能在您的应用中显示它。

将您更改server为以下内容:

server <- function(input, output, session){

  # Reading data from CFX exported file      
  data <- reactive({
    inFile <- input$file
    if(is.null(inFile))
      return(NULL)

    data <- readxl::read_excel(paste(inFile$datapath), sheet=1, skip=18, 
                    col_types = c("text", "text", "text","text", "numeric", 
                    "numeric", "numeric","numeric", "numeric"))
  })

  ID1 <- reactive({data()[12, 2]})

  observe({
    updateTextInput(session, "ID1", value = as.character(ID1()))  
  })

}

推荐阅读