无法推断如何使用要在后台运行的R脚本连接ui.R和server.R
我试图按照一位评论者(名为:warmoverflow)在我昨天发布的一个问题中给出的建议来做: 我决定尝试他首先在一个简短的例子中提出的方法。因此,我创建了一个R脚本无法推断如何使用要在后台运行的R脚本连接ui.R和server.R,r,shiny,R,Shiny,我试图按照一位评论者(名为:warmoverflow)在我昨天发布的一个问题中给出的建议来做: 我决定尝试他首先在一个简短的例子中提出的方法。因此,我创建了一个R脚本mathops.R,其中包含一个带有次要数学操作的函数。详情如下: mathops.R: mathops <- function(a,b) { print(paste0("Addition of two number is: ", a+b)) print(paste0("Multiplication of two nu
mathops.R
,其中包含一个带有次要数学操作的函数。详情如下:
mathops.R:
mathops <- function(a,b) {
print(paste0("Addition of two number is: ", a+b))
print(paste0("Multiplication of two number is: ", a*b))
}
现在,正如他所建议的,我正在尝试为server.R
文件开发代码:
服务器.R:
library(shiny)
source(mathops.R)
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderDataTable({
mathops()
})
}
库(闪亮)
资料来源(mathops.R)
shinyServer(功能(输入、输出){
a你必须做几件事:
在ui.R
未正确分析您的mainPanel
参数
如果要使用打印文本,则需要使用renderPrint
而不是renderDataTable
您可以调用a
和b
作为反应对象,但mathops
只是一个常规函数:
注意:为了简单起见,我将mathops
函数定义移到了server.R
中
ui.R
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
numericInput(inputId = "a",
label = h4("Enter a:"),
value = ""),
numericInput(inputId = "b",
label = h4("Enter b:"),
value = ""),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"),
textOutput("td")
)
))
library(shiny)
mathops <- function(a, b) {
print(paste0("Addition of two number is: ", a + b))
print(paste0("Multiplication of two number is: ", a * b))
}
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderPrint({
mathops(a(), b())
})
})
server.R
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
numericInput(inputId = "a",
label = h4("Enter a:"),
value = ""),
numericInput(inputId = "b",
label = h4("Enter b:"),
value = ""),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"),
textOutput("td")
)
))
library(shiny)
mathops <- function(a, b) {
print(paste0("Addition of two number is: ", a + b))
print(paste0("Multiplication of two number is: ", a * b))
}
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderPrint({
mathops(a(), b())
})
})
库(闪亮)
mathops你必须做几件事:
在ui.R
未正确分析您的mainPanel
参数
如果要使用打印文本,则需要使用renderPrint
而不是renderDataTable
您可以调用a
和b
作为反应对象,但mathops
只是一个常规函数:
注意:为了简单起见,我将mathops
函数定义移到了server.R
中
ui.R
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
numericInput(inputId = "a",
label = h4("Enter a:"),
value = ""),
numericInput(inputId = "b",
label = h4("Enter b:"),
value = ""),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"),
textOutput("td")
)
))
library(shiny)
mathops <- function(a, b) {
print(paste0("Addition of two number is: ", a + b))
print(paste0("Multiplication of two number is: ", a * b))
}
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderPrint({
mathops(a(), b())
})
})
server.R
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
numericInput(inputId = "a",
label = h4("Enter a:"),
value = ""),
numericInput(inputId = "b",
label = h4("Enter b:"),
value = ""),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"),
textOutput("td")
)
))
library(shiny)
mathops <- function(a, b) {
print(paste0("Addition of two number is: ", a + b))
print(paste0("Multiplication of two number is: ", a * b))
}
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderPrint({
mathops(a(), b())
})
})
库(闪亮)
mathops这是一个工作版本,对原始代码做了一些更改。正如在另一个答案中指出的,您的代码中有一些错误。此外,我将textOutput更改为htmlOutput。在server.R中,我将所有代码放在observeEvent
环境中
一般来说,R脚本中的方法需要返回server.R
中适合处理的内容。例如,在本例中,它返回一个字符串,该字符串server.R
呈现为文本,ui.R
依次呈现为HTML
可以在ui中显示在server.R
中可访问的任何变量/数据(包括其源
中的任何文件)。您需要的是(1)在server.R
中呈现此数据的适当呈现方法(2)在ui.R
中容纳输出显示的适当输出容器
用户界面
服务器.R
library(shiny)
source("mathops.R")
shinyServer(function(input, output) {
observeEvent(input$input_action, {
a = input$a
b = input$b
output$td <- renderText({
mathops(a,b)
})
})
# Use renderDataTable to convert mat2 to a displayable object. There are many other render methods for different kind of data and output, you can find them in Shiny documentation
output$table <- renderDataTable(data.frame(mat2))
})
库(闪亮)
资料来源(“mathops.R”)
shinyServer(函数(输入、输出){
ObserveeEvent(输入$input_动作{
a=输入$a
b=输入$b
output$td这是一个工作版本,对原始代码做了一些更改。正如在另一个答案中指出的,您的代码中有一些错误。此外,我将textOutput更改为htmlOutput。在server.R中,我将所有代码放在observeEvent
环境中
一般来说,R脚本中的方法需要返回server.R
中适合处理的内容。例如,在本例中,它返回一个字符串,该字符串server.R
呈现为文本,ui.R
依次呈现为HTML
可以在ui中显示在server.R
中可访问的任何变量/数据(包括其源
中的任何文件)。您需要的是(1)在server.R
中呈现此数据的适当呈现方法(2)在ui.R
中容纳输出显示的适当输出容器
用户界面
服务器.R
library(shiny)
source("mathops.R")
shinyServer(function(input, output) {
observeEvent(input$input_action, {
a = input$a
b = input$b
output$td <- renderText({
mathops(a,b)
})
})
# Use renderDataTable to convert mat2 to a displayable object. There are many other render methods for different kind of data and output, you can find them in Shiny documentation
output$table <- renderDataTable(data.frame(mat2))
})
库(闪亮)
资料来源(“mathops.R”)
shinyServer(函数(输入、输出){
ObserveeEvent(输入$input_动作{
a=输入$a
b=输入$b
output$td非常感谢您详细说明了我的错误所在。这让我意识到我完全忽略了解析的细微差别。从现在起,我将关注它们。还感谢您展示了如何在服务器.R
本身中定义和使用函数。非常感谢您详细说明了我的错误所在。这让我意识到我完成了我错过了解析的细微差别。从现在开始,我会记住它们。同时感谢您演示如何在server.R
本身中定义和使用函数。我只是想澄清一下。在我试图实现的实际任务中,在source.R
脚本中,除了函数之外,我还有许多其他命令,例如for
循环,数据帧
操作,应用线性回归模型,生成模型结果的绘图,并使用write.csv()将这些结果数据帧写入csv
和其他各种类似命令..因此,当我在服务器.R
文件中链接我的源.R
时,包含所有这些命令的整个脚本将正确执行??如果您将所有这些命令都作为函数实现,那么只会导入函数,但在调用这些函数之前不会执行它们,如上面的例子中,您调用mathops(a,b)
Ok。比方说,我刚刚在mathops.R
脚本中的mathops()
函数下创建了两个矩阵。mat1在server.R
中可以访问的任何变量/数据都可以显示在ui中。您需要的是(1)在服务器.R
中呈现此数据的合适呈现方法(2)在前端保存输出显示的合适输出容器。我已更新了答案以包含矩阵。只是为了澄清..在我试图实现的实际任务中,在source.R
sc中