R Shiny create several random numbers with button and save it

丶灬走出姿态 提交于 2021-02-04 21:07:48

问题


I want to create a button that generates a random number and save all random numbers on my server that I can evaluate that data later.

Unfortunately I am not able to generate a vector with all random numbers. Somehow a for loop is not working. Thanks!

library(shiny)

ui <- fluidPage(
  actionButton("button", "Show")
)

server <- function(input,output) {
  eventReactive(input$button, {
    counter <- sample(1:10,1)
  })
}
shinyApp(server = server, ui = ui)

回答1:


You don't need a for loop in R to generate a vector of random numbers, there are many functions for random number generation, please check here for some examples.

Here is a sample code:

library(shiny)

ui <- shinyUI(fluidPage(
  titlePanel("Random number generator"),
  sidebarLayout(
    sidebarPanel( 
      sliderInput("rangeSl", "Range", min = 0, 
        max = 100, value = c(40, 60)
      ),
      numericInput("num", "Quantity:", 20, min = 1, max = 100, width = "40%"),
      actionButton("generateBt", "Generate Numbers")
    ),
    mainPanel( 
      verbatimTextOutput("result")
    ) 
  )
))

server <- shinyServer(function(input, output) {
  output$result <- renderPrint({ 
    if (input$generateBt > 0 ) 
      isolate(
        floor(runif(input$num, min = input$rangeSl[1], max = input$rangeSl[2]))
      )
  })
})

shinyApp(ui = ui, server = server)


来源:https://stackoverflow.com/questions/45644700/r-shiny-create-several-random-numbers-with-button-and-save-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!