1

This seems like it should be trivial but I'm obviously missing something. I'd like to have the selection from a selectInput() be used to create a data frame. The selectInput() produces a character output that doesn't seem to work as an input for creating a data frame. My ui.R and server.R code is below. The df<-data.frame(input$select, header=TRUE) line doesn't produce the expected output. Thanks in advance for your assistance.

ui.R

library(shiny)

shinyUI(

  # Use a fluid Bootstrap layout
  fluidPage(    

    # Give the page a title
    titlePanel("Select Test Dataset"),

    # Generate a row with a sidebar
    sidebarLayout(      

      # Define the sidebar with one input
      sidebarPanel(
        selectInput("select", "Test Dataset", 
                    choices= list("Model Year 2011" = 'cars2011', "Model Year 2012" = 'cars2012'),selected='cars2011')

      ),

      # mainPanel Output
      mainPanel(
        verbatimTextOutput("value"),
        verbatimTextOutput("type"),
        dataTableOutput(outputId="data")
      )

    )
  )
)

server.R

library(AppliedPredictiveModeling)
data(FuelEconomy)



shinyServer(function(input, output) {

  output$value <- renderPrint({ input$select })
  output$type <- renderPrint({class(input$select)})

  output$data <- reactive({
    df <- data.frame(input$select, header=TRUE)
    head(df)
  })




})

2 Answers 2

1

You should use shiny::renderDataTable instead of shiny::reactive when defining output$data.

output$data <- renderDataTable({
    df <- data.frame(input$select, header=TRUE)
    head(df)
})
Sign up to request clarification or add additional context in comments.

4 Comments

is this code working for you? I tested this solution but I still wasnt able to see any output.
The answer renders a data table, but doesn't display the correct data. The problem is in passing a character string to data.frame. The following R code illustrates the problem: >chr <- "cars2011" >class(chr) [1] "character" >df<-data.frame(chr,head=TRUE) >head(df) chr head 1 data2011 TRUE
Thanks for clarifying. What would you like the output to look like?
Quinn, The answer I posted provides the desired output. Thank you for your suggestion regarding the use of renderDataTable: it is obviously the preferred method for displaying data and I will use it in my application.
1

Converting the character string to a data frame name using a switch statement seems to work.

In server.R inserting:

dataset <- reactive({ switch(input$select, "cars2011" = cars2011, "cars2012" = cars 2012) })

and then using head(dataset()) to output data provides the desired result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.