Vector input in shiny R and then use it

前端 未结 2 563
野性不改
野性不改 2021-02-04 13:11

In Shiny R, I want a simple way to take a vector as user input in ui.R and then want to use that in a function in server.R.I am new in shiny, please help.

2条回答
  •  悲&欢浪女
    2021-02-04 14:07

    Here is something simple to get you started - good luck. And remember - next time post some code or you will surely get downvoted:

    library(shiny)
    
    u <- shinyUI(pageWithSidebar(
    
      headerPanel("Entering Vectors in Shiny"),
      sidebarPanel(
        textInput('vec1', 'Enter a vector (comma delimited)', "0,1,2")
      ),
    
      mainPanel(
        h4('You entered'),
        verbatimTextOutput("oid1"),
        verbatimTextOutput("oid2")
      )
    ))
    
    s <- shinyServer(function(input, output) {
    
      output$oid1 <- renderPrint({
        cat("As string:\n")
        cat(input$vec1)
        }
        )
    
      output$oid2<-renderPrint({
        x <- as.numeric(unlist(strsplit(input$vec1,",")))
        cat("As atomic vector:\n")
        print(x)
      }
      )
    }
    )
    shinyApp(ui = u, server = s)
    

    Yielding:

提交回复
热议问题