9

Trying to create a data frame like below;

X   Y
20  30

Using textInput to create data frame.
But values entered in text area are not assigning properly to data frame.

Could you please help me?

ui.R

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel( "", ""),
  sidebarPanel(

    wellPanel(
      textInput('datavalues', "variable values",""),
      actionButton("submit","Apply")

    )
  ),

  mainPanel(   
    verbatimTextOutput('datatable')
  )
))

server.R

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

  data1= reactive({
    if(input$submit!=0){
      isolate({
        data.frame(paste(input$datavalues))
      })
    }
  })

  output$datatable<-renderPrint(function(){
    if(!is.null(data1())){
      d<-data1()
      print(d)
    }
  })


})
4
  • You'll need to parse the data from the text field as you would any other body of text. For starters, try reading that data in from a static file into a data.frame. Once you get that working, then try incorporating it into Shiny. In particular, check out the read.csv or read.table functions to create data.frames out of tabular text. Commented Oct 31, 2013 at 14:24
  • I tried data.frame(eval(parse(input$datavalues))). But it's not working. Commented Nov 5, 2013 at 5:49
  • Yeah. Look at the documentation for read.table(). You can pull it up by typing the command ?read.table. That's the function you'll want to use. You may even find that read.table("yourFileName.txt") will work right away. Then you can start looking at how to use a text string instead of a file. Commented Nov 5, 2013 at 15:25
  • 1
    I don't want to use the existing file. Just I want to create a only one row data set using text input in shiny. Commented Nov 6, 2013 at 7:33

1 Answer 1

9

I would recommend using the matrixInput function of the shinyIncubator package. I have made a demo here: https://gist.github.com/anonymous/8207166. You can run it from RStudio with:

library("shiny")
runGist("https://gist.github.com/anonymous/8207166")

But to answer your question based on your code, below is a modification that works. Note that the function renderTable() takes arguments that allow you to control the display of the data.frame. The advantage of using the matrixInput function is that you can make the size of your dataframe reactive, whereas below it is hard-coded as a 2 variable dataframe.

ui.R

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'))
))

server.R

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}
    )

})
Sign up to request clarification or add additional context in comments.

3 Comments

This is also very helpful for non-numeric input. Thanks.
The gist does not work for me. > library("shiny") > runGist("gist.github.com/anonymous/8207166") Downloading gist.github.com/anonymous/8207166/download Error in rawToChar(block[seq_len(ns)]) : embedded nul in string: 'PK\003\004\n\0\0\0\0\0T\034!D\0\0\0\0\0\0\0\0\0\0\0\0\017\0\t\08207166-master/UT\005\0\001PýÃRPK\003\004\n\0\0\0\b\0T\034!Dt¤k‚\002\001\0\0Õ\001\0\0\027\0\t\08207166-master/s'
@Deleet, I got the same error. Copy & Paste the code in a new shiny project. It runs just fine.

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.