Creating data frame with multiple rows from shiny input

Basically, I want the user to provide a vector of values: X=1,2,3 and Y=2.3,4.5,6.7 in the shiny interface

and get a table like that:

X Y    
1 2.3  
2 4.5  
3 6.7  

However, I only get the values in a row:

X        Y    
1,2,3    2.3,4.5,6.7 

server

    library("shiny")
shinyServer(
  function(input,output,session){

    Data = reactive({
      if (input$submit > 0) {
        df <- data.frame(x=input$x,y=input$y)
        return(list(df=df))
      }
    })

    output$table <- renderTable({
      if (is.null(Data())) {return()}
      print(Data()$df)
    }, 'include.rownames' = FALSE
    , 'include.colnames' = TRUE
    , 'sanitize.text.function' = function(x){x}
    )

  })

ui

library("shiny")    
shinyUI(
  pageWithSidebar(
    headerPanel("textInput Demo")
    ,
    sidebarPanel(
      wellPanel(
        textInput('x', "enter X value here","")
        ,
        textInput('y', "enter Y value here","")
        ,
        actionButton("submit","Submit")
      )
    )
    ,
    mainPanel(uiOutput('table'))
  ))

Any ideas about how to solve this issue?

Thanks a LOT in advance,

Upvotes: 0

Views: 569

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12087

textInput returns a string. So what you get is a single string "1,2,3" instead of three numbers. To convert the string to a vector of numbers

x = as.numeric(unlist(strsplit(input$x, ',')))

Upvotes: 1

Related Questions