简体   繁体   中英

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

So im trying to take an input file, read the column names, and return them as the options in a checkboxGroupInput() .

It currently extracts the column names and returns them as a vector of character strings, but I can't get it to use that vector as the options in a checkboxGroupInput .

Note: The actual code for the server uses read.csv(file = file1$datapath) with an uploaded file (and works properly), but I don't know how to include the .csv file in this replication. So I tried to use read.table() and a text representation of the data frame here for the sake of creating a reproducible example (may not work perfectly).

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)

I can't get your example to run, there seems to be a couple of errors. However, here's a brief description of what you could try to fix your problem.

You can use updateCheckboxGroupInput to change the values for input choices after you read in the file. Assuming your nms() reactive object is working, you could add this block after it:

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

This would then update the value of choices on your checkboxGroupInput whenever nms() changes. An additional change you need to do, is to pass the session object to your server by specifying the server function as:

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

Check out my answer to a similar question regarding updating input values here for another example of using the update* functions.

PS. uiOutput is used to create ui elements from the output given by renderUI . renderPrint creates a character string containing the printed output that you would normally see on the console - to display the text in the ui, you have to use the textOutput function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM