Create a data frame using text input in Shiny

前端 未结 1 1979
耶瑟儿~
耶瑟儿~ 2021-02-06 10:14

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

相关标签:
1条回答
  • 2021-02-06 10:21

    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}
        )
    
    })
    
    0 讨论(0)
提交回复
热议问题