0
votes

I am trying to build my first Shiny app.

I have a user defined function which takes generates encryption keys. I'd like to make it in to a Shiny app.

I had a go as below. The first bit of the UI works in that it asks for text input for the users name (this should be used to label the output files) - and I managed to get an action button which the user could then click to in theory run the code to make the encryption keys.

library(shiny)

# Define UI for ODK Encryption app ----

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Make ODK Encryption Keys"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      textInput("name",
                  "Enter your name:"),
      actionButton(inputId = "input_action", label = "Generate Keys")

    ),

    mainPanel(

    )
  )
)

I feel that there ?should be something in mainPanel to give a link for the outputted key files or alternatively they could just automatically get downloaded?

Then within the server section I 1) Take the sys-date-time as a seed 2) Take the name (as inputted above) and link that to the sys-date to make a file name for the eventual keys 3) Then I define my function (create.key.pair) - This takes the sys-date-time (set in 1 above) and uses that and openSSL to generate a public and private key. It labels these files using the name inputted in point 2 (which should take the input from the sidebar section of the UI in the shiny app)

#Make the Key

server <- function(input, output) {
  observeEvent(input$button, {
    key_seed <- round(as.numeric(Sys.time()),0)
    key_name <- paste(name,Sys.Date(),sep="_")
    create.key.pair<-function(keynumber)
    {
      folder<-paste("ODK.KEY",key_name,sep=".")                      #make a folder name
      system(paste("mkdir",folder,sep=" "))                           #create the folder

      name.private.key<-paste("ODK.PRIVATE.KEY.",key_name,".pem",sep = "") #make a name for the keys
      name.public.key<-paste("ODK.PUBLIC.KEY.",key_name,".pem",sep = "")

      keygen.private<-paste("openssl genrsa -out ",folder,"/",name.private.key," 2048",sep="") #create the system call
      system(command = keygen.private) #create the private key

      keygen.public<-paste("openssl rsa -in ",folder,"/",name.private.key," -inform PEM -out ",folder,"/",name.public.key," -outform PEM -pubout",sep="")

      system(command = keygen.public) #create the private key
    }
    create.key.pair(key_seed)
  })
}


shinyApp(ui, server)

I need some guidance on whether A) I need to add something within mainPanel to show/download the outputted files B) The code within the server section should work

I can't currently tell if my error is in A or B just that it doesnt generate keys (whereas my R script version of this works just fine)

First time using Shiny so any help gratefully accepted.

1
There are some issues with calling input on the server site in your code: input$input_action instead of input$button and input$name instead of name. First change this and then you can start debugging the restPeter Dieter
The function also does not work correct. key_name is not specified as parameterThomas

1 Answers

1
votes

If I fix some slight bugs it's working the same way in shiny and as a usual R script:

  1. input$input_action and input$name
  2. adding key_name to your function

Also, do not load your function in observeEvent as this will cause your app to reload the function every time someone clicks the actionButton. You can instead load it once prior to your app start.

Your code is then:

library(shiny)

create.key.pair<-function(keynumber, key_name)
{
    folder<-paste("ODK.KEY",key_name,sep=".")                      #make a folder name
    system(paste("mkdir",folder,sep=" "))                           #create the folder

    name.private.key<-paste("ODK.PRIVATE.KEY.",key_name,".pem",sep = "") #make a name for the keys
    name.public.key<-paste("ODK.PUBLIC.KEY.",key_name,".pem",sep = "")

    keygen.private<-paste("openssl genrsa -out ",folder,"/",name.private.key," 2048",sep="") #create the system call
    system(command = keygen.private) #create the private key

    keygen.public<-paste("openssl rsa -in ",folder,"/",name.private.key," -inform PEM -out ",folder,"/",name.public.key," -outform PEM -pubout",sep="")

    system(command = keygen.public) #create the private key
}

# Define UI for ODK Encryption app ----

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Make ODK Encryption Keys"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            textInput("name",
                      "Enter your name:"),
            actionButton(inputId = "input_action", label = "Generate Keys")

        ),

        mainPanel(

        )
    )
)

#Make the Key

server <- function(input, output) {
    observeEvent(input$input_action, {

        key_seed <- round(as.numeric(Sys.time()),0)
        key_name <- paste(input$name,Sys.Date(),sep="_")
        create.key.pair(key_seed, key_name)
    })
}


shinyApp(ui, server)