问题
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