R闪亮随机数生成

时间:2014-08-26 08:11:14

标签: r shiny

我想使用Shiny创建密码生成应用程序。应用程序outlook,ui.R和server.R脚本已附加。

我的问题是,当我修正了pw的长度并按下更新视图时,它不会生成新的pw。当我将长度更改为5然后再更改为4时,将生成新的pw。

其次,如何在mainPanel中放大输出pw

由于

enter image description here

ui.R

library(shiny)

shinyUI(fluidPage(

    #Application title
    titlePanel("Password Generation apps"),

    #Sidebar with controls length of password
    sidebarLayout(
        sidebarPanel(
            numericInput(inputId = "num", 
                     label = h3("Length of password"),
                     value = 4, 
                     min = 4, max = NA, 
                     step = 1),

            submitButton("New Password")
        ),

        mainPanel(
            textOutput('pw')
        )
    )

))

server.R

library(shiny)

shinyServer(function(input, output){

    password <- function(n){

        A <- LETTERS[1:26]
        B <- letters[1:26]
        C <- seq(0,9)
        ascii <- rawToChar(as.raw(0:127), multiple=TRUE)
        D <- ascii[grepl('[[:punct:]]', ascii)][c(1:23,25:32)]

        All <- c(A, B, C, D)
        list <- list()
        list[[1]] <- A
        list[[2]] <- B
        list[[3]] <- C
        list[[4]] <- D
        names(list) <- c("A","B","C","D")

        first <- sample(names(list), replace=FALSE, 4)
        pw <- vector(length=n)

        pw[1] <- sample(list[[first[1]]],1)
        pw[2] <- sample(list[[first[2]]],1)
        pw[3] <- sample(list[[first[3]]],1)
        pw[4] <- sample(list[[first[4]]],1)

        if (n==4){
            pw <- paste(pw, collapse="")
        } else{
            for (i in 5:n){
                pw[i] <- sample(All, 1)
            }
            pw <- paste(pw, collapse="")                            
        }

        return(noquote(pw))
    }   

    output$pw <- renderPrint({
        n <- input$num
        password(n)
    })

})

1 个答案:

答案 0 :(得分:2)

这是你想要的吗?您应该使用actionButton,而不是submitButton

<强> server.R

rm(list = ls())
library(shiny)

password <- function(n){

  A <- LETTERS[1:26]
  B <- letters[1:26]
  C <- seq(0,9)
  ascii <- rawToChar(as.raw(0:127), multiple=TRUE)
  D <- ascii[grepl('[[:punct:]]', ascii)][c(1:23,25:32)]

  All <- c(A, B, C, D)
  list <- list()
  list[[1]] <- A
  list[[2]] <- B
  list[[3]] <- C
  list[[4]] <- D
  names(list) <- c("A","B","C","D")

  first <- sample(names(list), replace=FALSE, 4)
  pw <- vector(length=n)

  pw[1] <- sample(list[[first[1]]],1)
  pw[2] <- sample(list[[first[2]]],1)
  pw[3] <- sample(list[[first[3]]],1)
  pw[4] <- sample(list[[first[4]]],1)

  if (n==4){
    pw <- paste(pw, collapse="")
  } else{
    for (i in 5:n){
      pw[i] <- sample(All, 1)
    }
    pw <- paste(pw, collapse="")                            
  }

  return(noquote(pw))
}    
shinyServer(function(input, output,session) {

  my_password <- reactive ({
    if(input$button == 0)
    {  
      return()
    }  
    isolate({   
      n <- input$num
      password(n)

    })

  })
  output$pw <- renderPrint({my_password()})

})

<强> ui.R

rm(list = ls())
library(shiny)

shinyUI(
  fluidPage(   
    #Application title
    titlePanel("Password Generation apps"),

    #Sidebar with controls length of password
    sidebarLayout(
      sidebarPanel(
        numericInput("num", 
                     label = h3("Length of password"),
                     value = 4, 
                     min = 4, max = NA, 
                     step = 1),
        actionButton("button", "Get Data")
      ),

      mainPanel(
        textOutput('pw')
      )
    )  
  )
)
相关问题