【问题标题】:R Shiny create several random numbers with button and save itR Shiny 用按钮创建几个随机数并保存
【发布时间】:2017-08-11 22:38:09
【问题描述】:

我想创建一个生成随机数的按钮,并将所有随机数保存在我的服务器上,以便以后评估该数据。

不幸的是,我无法生成包含所有随机数的向量。不知何故,for循环不起作用。谢谢!

library(shiny)

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

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

【问题讨论】:

  • 不工作的for 循环在哪里?你真的需要一个循环吗?
  • 我想使用循环但有错误。目前我不知道如何解决这个问题......

标签: r shiny


【解决方案1】:

R 中不需要for 循环来生成随机数向量,随机数生成的函数有很多,请查看here 中的一些示例。

这是一个示例代码:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2020-09-17
    • 2016-02-02
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多