无法推断如何使用R脚本连接ui.R和server.R以在后台运行 [英] Unable to deduce how to connect ui.R and server.R with an R script to run in the background

查看:7
本文介绍了无法推断如何使用R脚本连接ui.R和server.R以在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着按照一个评论者(名为:warmoverflow)在我昨天发布的一个问题中给出的建议:How to pass values from input boxes of shiny UI back into the variables in an R script and run it?

我决定先在一个简短的示例中尝试他提出的方法。因此,我创建了一个R脚本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))
}

我开发了一个具有两个文本框的UI,上面提到的变量ab将从这两个文本框中接受输入,并且还有一个操作按钮来显示输出。具体如下:

ui.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上输入框的输入,并将这些值传递到上面所示的R脚本中的mathops()函数的变量ab中。

我还是个新手。我在这里遗漏或误解了什么?如何解决这种情况?

请帮帮忙!

谢谢。

推荐答案

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

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

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

ui.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")


))

server.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

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)

这篇关于无法推断如何使用R脚本连接ui.R和server.R以在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆