簡體   English   中英

如何將取自輸入數據幀的字符串標簽向量作為checkboxGroupInput中的選項傳遞

[英]How can I pass a vector of character string labels, taken from an input data frame, as options in a checkboxGroupInput

因此,我嘗試獲取輸入文件,讀取列名,然后將其作為checkboxGroupInput()的選項返回。

當前,它提取列名並將其作為字符串向量返回,但我無法使用該向量作為checkboxGroupInput中的checkboxGroupInput

注意:服務器的實際代碼使用read.csv(file = file1$datapath)和上載的文件(並且正常工作),但是我不知道如何在此復制中包括.csv文件。 因此,為了創建可重現的示例,我嘗試在此處使用read.table()和數據框的文本表示形式(可能無法完美運行)。

library(shiny)

df <- "substrate rep treatment time RFU
1             1   1      live  9:56 137,813.04
2             1   2      live  9:57 135,673.5
3             1   3      live  9:57 138,597.51
4             1 kill    killed 9:57 138,111.29
5             2   1      live  9:58 131,257.6
6             2   2      live  9:58 129,746.85"

ui <- fluidPage( 

  titlePanel("File Upload"),
  sidebarLayout(
    sidebarPanel(
      fileInput(inputId = "data", label = "Data")
    ),
    mainPanel(
      uiOutput("names")
    ),
    checkboxGroupInput("design.variables", "Design Variables",
                       uiOutput("names")
    )
  )
)

server <- function(input, output){

  nms <- reactive({
    file1 <- input$data
    if(is.null(file1)){return()} 
    d_uncal <- read.table(file = df
                        header = TRUE, stringsAsFactors = FALSE)
    nm <- names(d_uncal)
    nm
  })

  output$names <- renderPrint({
    if(is.null(nms()))
      return()
    else
      nms()

  })
}

shinyApp(ui = ui, server = server)

我無法運行您的示例,似乎有一些錯誤。 但是,這是您可以嘗試解決問題的簡要說明。

讀取文件后,可以使用updateCheckboxGroupInput更改輸入選項的值。 假設您的nms()反應性對象正常工作,則可以在其后添加以下代碼塊:

observeEvent(nms(), {
  updateCheckboxGroupInput(session, "design.variables", choices = nms())
})

那么這將更新的價值choices上的checkboxGroupInput每當nms()的變化。 你需要做的另外一個變化,就是要通過session通過指定服務器的功能對象到您的服務器:

server <- function(input, output, session) { ... # Everyhing else }

看看我的答案,關於更新的輸入值,類似的問題在這里使用的另一個例子update*功能。

PS。 uiOutput用於從renderUI給定的輸出中創建ui元素。 renderPrint創建一個字符串,其中包含您通常會在控制台上看到的打印輸出-要在ui中顯示文本,必須使用textOutput函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM