无法弄清楚如何使用输入文本框在Shiny中创建和运行我的用户定义的R函数

时间:2016-03-01 14:09:33

标签: r shiny

我有一个R脚本,我需要在R中的shiny包中开发和调用用户界面。

我的以下内容是我正在尝试构建界面的R代码的一部分:

# A random normal function to generate numbers with exact mean and SD
rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }
age <- rnorm2(n = 10000, mean = 55 , sd = 15)
cholestrol <- rnorm2(n = 10000, mean = 200 , sd = 30)
bp <- rnorm2(n = 10000, mean = 90 , sd = 25)
df <- cbind(age, cholestrol,bp)
Org_Data <- as.data.frame(df)

由于有9个输入(agecholestrolbp各3个),我创建了9个输入框,用户输入输入,然后点击按钮I想要运行我创建的rnorm2函数。

我闪亮的应用代码如下:

ui.R:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("function1"),

    sidebarPanel( 
      #Age input
      textInput(inputId = "n1", 
                label = h4("Enter n for Age:"), 
                value = ""),

      textInput(inputId = "mean1", 
                label = h4("Enter mean for Age:"), 
                value = ""),

      textInput(inputId = "sd1", 
                label = h4("Enter sd for Age:"), 
                value = ""),

      #Cholestrol input
      textInput(inputId = "n1", 
                label = h4("Enter n for Cholestrol:"), 
                value = ""),

      textInput(inputId = "mean1", 
                label = h4("Enter mean for Cholestrol:"), 
                value = ""),

      textInput(inputId = "sd1", 
                label = h4("Enter sd for Cholestrol:"), 
                value = ""),


      #Blood Pressure input
      textInput(inputId = "n1", 
                label = h4("Enter n for Blood Pressure:"), 
                value = ""),

      textInput(inputId = "mean1", 
                label = h4("Enter mean for Blood Pressure:"), 
                value = ""),

      textInput(inputId = "sd1", 
                label = h4("Enter sd for Blood Pressure:"), 
                value = ""),


      actionButton(inputId = "input_action", label = "Show Inputs")),



    mainPanel( 
      h2("Input Elements"), # title with HTML helper
      textOutput("td"))


  ))

但是我完全无法弄清楚如何将我在开头提到的R代码放入server.R文件中。我可以从简单定义函数开始吗?然后如何在用户输入的那些输入上运行该函数,然后将输出保存到每个变量中:agecholestrolbp ???

server.R:

library(shiny)

#defining a function
rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }

shinyServer(function(input, output){

  #Don't know how run the function and save the output into the variables 
  age <- 
  cholestrol <- 
  bp <-  


  })

})

这是我的第一天闪亮和所有的服务器。互联网上的代码示例有点过头了。但我今天真的需要扭转局面。请帮忙!!!

1 个答案:

答案 0 :(得分:2)

我添加的唯一依赖是library(DT),这是一个非常有用的包。

您会注意到您需要在ui.R中使输入ID唯一,并使用eventReactive指示闪亮等待输入按钮。

<强> ui.R

library(shiny)
library(DT)
shinyUI(pageWithSidebar(
  headerPanel("function1"),

  sidebarPanel( 
    #Age input
    numericInput(inputId = "n", 
                 label = h4("Enter n:"), 
                 value = ""),

    numericInput(inputId = "mean1", 
                 label = h4("Enter mean for Age:"), 
                 value = ""),

    numericInput(inputId = "sd1", 
                 label = h4("Enter sd for Age:"), 
                 value = ""),

    #Cholestrol input        
    numericInput(inputId = "mean2", 
                 label = h4("Enter mean for Cholestrol:"), 
                 value = ""),

    numericInput(inputId = "sd2", 
                 label = h4("Enter sd for Cholestrol:"), 
                 value = ""),


    #Blood Pressure input
    numericInput(inputId = "mean3", 
                 label = h4("Enter mean for Blood Pressure:"), 
                 value = ""),

    numericInput(inputId = "sd3", 
                 label = h4("Enter sd for Blood Pressure:"), 
                 value = ""),


    actionButton(inputId = "input_action", label = "Show Inputs")),



  mainPanel( 
    h2("Input Elements"), # title with HTML helper
    dataTableOutput("inputs"),
    h2("Results"),
    dataTableOutput("results")
  )))

<强> server.R

library(shiny)
library(DT)

rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }

shinyServer(function(input, output){

  data <- eventReactive(input$input_action, {
    matrix(
      c(rnorm2(input$n, input$mean1, input$sd1),
        rnorm2(input$n, input$mean2, input$sd2),
        rnorm2(input$n, input$mean3, input$sd3)), byrow = FALSE,
      ncol = 3)
  })

  inpts <- eventReactive(input$input_action, {
    data.frame(Type = c("Age", "Cholestorol", "BP"), 
               N = c(input$n, input$n, input$n),
               Mean = c(input$mean1, input$mean2, input$mean3),
               SD = c(input$sd1, input$sd2, input$sd3))
  }) 

  output$inputs <- renderDataTable({
    inpts()
  })
  output$results <- renderDataTable({
    set <- as.data.frame(data())
    colnames(set) <- c("Age", "BP", "Cholestorol")
    set
  })

})