使用Shiny中的文本输入创建数据框

Pun*_*ith 6 r shiny shiny-server

尝试创建如下的数据框;

X   Y
20  30
Run Code Online (Sandbox Code Playgroud)

使用textInput创建数据框.
但是在文本区域中输入的值未正确分配给数据框.

请你帮助我好吗?

ui.R

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

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

    )
  ),

  mainPanel(   
    verbatimTextOutput('datatable')
  )
))
Run Code Online (Sandbox Code Playgroud)

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


})
Run Code Online (Sandbox Code Playgroud)

Pat*_*ckT 6

我建议使用shinyIncubator包的matrixInput函数.我在这里做了一个演示:https://gist.github.com/anonymous/8207166.您可以使用以下命令从RStudio运行它:

library("shiny")
runGist("https://gist.github.com/anonymous/8207166")
Run Code Online (Sandbox Code Playgroud)

但要根据您的代码回答您的问题,下面是一个有效的修改.请注意,函数renderTable()接受允许您控制data.frame显示的参数.使用matrixInput函数的优点是,您可以使数据帧的大小具有反应性,而在其下面则硬编码为2个可变数据帧.

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'))
))
Run Code Online (Sandbox Code Playgroud)

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

})
Run Code Online (Sandbox Code Playgroud)