无法推断如何连接 ui.R 和服务器.R 与 R 脚本在后台运行



我试图遵循评论者(命名为:warmoverflow)在我昨天发布的一个问题中给出的建议:如何将值从闪亮 UI 的输入框传递回 R 脚本中的变量并运行它?

我决定尝试他首先在一个小例子中提出的方法。因此,我创建了一个 R 脚本mathops.R其中包含一个具有少量数学运算的函数。具体如下:

马托普斯。R:

mathops <- function(a,b) {
  print(paste0("Addition of two number is: ", a+b))
  print(paste0("Multiplication of two number is: ", a*b))
}

我开发了一个带有两个文本框的 UI,从中获取上述变量ab的输入,还有一个用于显示输出的操作按钮。具体如下:

用户界面。R:

library(shiny)
shinyUI(fluidPage(
  headerPanel("Inputting from Interface to Script"),
  sidebarPanel( 
    #'a' input
    textInput(inputId = "a", 
              label = h4("Enter a:"), 
              value = ""),
    textInput(inputId = "b", 
              label = h4("Enter b:"), 
              value = ""),
    actionButton(inputId = "input_action", label = "Show Inputs")),

  mainPanel( 
    h2("Input Elements"))
    textOutput("td"))

))

现在正如他的建议,我正在尝试为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脚本连接到server.R文件中,以便它将获取来自 UI 上的输入框的输入,并将这些值传递到变量中,ab上面所示的 R 脚本中 mathops() 函数。

我是闪亮的新手。我在这里错过了什么或误解了什么?如何解决这种情况?

请帮忙!

谢谢。

这是一个工作版本,对原始代码进行了一些更改。正如另一个答案所指出的,您的代码中存在一些错误。此外,我将textOutput更改为htmlOutput。在服务器中。R,我把所有代码都放在observeEvent环境中。

通常,R 脚本中的方法需要返回适合 server.R 中进程的内容。例如,在本例中,它返回一个字符串,该字符串server.R呈现为文本,ui.R又呈现为 HTML。

server.R中可访问的任何变量/数据(包括它source的任何文件)都可以显示在 UI 中。您需要的是 (1) 一个合适的渲染方法来server.R渲染此数据 (2) 一个合适的输出容器来保存输出显示ui.R

用户界面。R

library(shiny)
shinyUI(fluidPage(
    headerPanel("Inputting from Interface to Script"),
    sidebarPanel( 
        #'a' input
        numericInput(inputId = "a", 
                  label = h4("Enter a:"), 
                  value = 3),
        numericInput(inputId = "b", 
                  label = h4("Enter b:"), 
                  value = 4),
        actionButton(inputId = "input_action", label = "Show Inputs")),
    mainPanel( 
        h2("Input Elements"),
        htmlOutput("td")),
        # use dataTableOuput to display the result of renderDataTable. Just like the render methods, there are many output methods available for different kind of outputs. See Shiny documentation.
        dataTableOutput("table")

))

服务器。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))
})

马托普斯。R

mathops <- function(a,b) {
    return(paste0("Addition of two number is: ", a+b, br(), "Multiplication of two number is: ", a*b))
}
mat1 <- matrix(sample(1:16), ncol=4)
mat2 <- matrix(sample(1:25), ncol=5)

你必须做几件事:

  1. ui.R中从使用textInput切换到numericInput
  2. 您的mainPanel参数未正确解析
  3. 如果您要使用
  4. 打印文本,则需要使用renderPrint而不是renderDataTable
  5. 您可以将 ab 作为响应对象调用,但mathops只是一个常规函数:

注意:为简单起见,我将mathops函数定义移到了server.R

用户界面。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")
    )
))

服务器。R

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())
  })
})

最新更新