我有一个 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 个输入(age
、cholestrol
和 bp
各 3 个),我创建了 9 个输入框,用户在其中输入输入,然后单击按钮“我想运行我创建的 rnorm2
函数”。
我的 Shiny 应用程序代码如下:
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
文件中。我可以从简单地定义函数开始吗?然后如何对从用户处获取的输入运行该函数,然后将输出保存到每个变量中:age
、cholestrol
和 bp
???
服务器.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 <-
})
})
这是我使用 Shiny 的第一天,互联网上的所有 server.R 代码示例都让我难以理解。但我今天确实需要扭转局面。请帮忙!!!
最佳答案
我添加的唯一依赖项是library(DT)
,这是一个非常有用的包。
您会注意到,您需要使输入 ID 在 ui.R
中唯一,并使用 eventReactive
指示 Shiny 等待输入按钮。
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")
)))
服务器.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
})
})
关于r - 无法弄清楚如何使用输入文本框在 Shiny 中创建和运行用户定义的 R 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725156/