studio script ejecutar desde correr como codigos r shiny shinyapps

desde - No se puede deducir cómo conectar ui.R y server.R con un script R para ejecutar en segundo plano



ejecutar script r linux (2)

Tuviste que hacer un par de cosas:

  1. Cambia de la entrada de numericInput a la entrada numericInput en tu ui.R
  2. Sus argumentos mainPanel no se estaban mainPanel correctamente
  3. Necesitabas usar renderPrint lugar de renderDataTable si vas a usar texto impreso
  4. Puedes llamar b como objetos reactivos, pero mathops es solo una función regular:

Nota: mathops definición de la función mathops al server.R para simplificar

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

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

Estaba tratando de seguir las sugerencias dadas por un comentarista (llamado: warmoverflow) en una pregunta que publiqué ayer: ¿Cómo pasar los valores de los cuadros de entrada de UI brillante de nuevo a las variables en un script R y ejecutarlo?

Decidí probar el enfoque que propuso en primer lugar en un breve ejemplo. Entonces, creé un script R mathops.R contiene una función con operaciones matemáticas menores. Es como sigue:

mathops.R:

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

Desarrollé una interfaz de usuario con dos cuadros de texto desde los cuales se tomará entrada para los varaibles a y b mencionados anteriormente y también un botón de acción para mostrar la salida. Es como sigue:

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

y ahora, como sugirió, estaba intentando desarrollar el código para el archivo server.R :

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

Pero aquí es donde estoy frente a un callejón sin salida. Simplemente no podía pensar cómo conectar ese script server.R archivo server.R para que tome la entrada proveniente de los cuadros de entrada en la UI y pase esos valores a las variables a y b de mathops() función en el guión R que se muestra arriba.

Soy nuevo en brillante. ¿Qué estoy perdiendo o entendiendo mal aquí? ¿Cómo resolver esta situación?

¡Por favor ayuda!

Gracias.


Aquí hay una versión de trabajo con algunos cambios de su código original. Como se señaló en la otra respuesta, hay algunos errores en su código. Además, cambié textOutput a htmlOutput. En server.R, puse todo el código dentro del entorno observeEvent .

En general, su método en su script R necesita devolver algo adecuado para el proceso en server.R . Por ejemplo, en este caso, devuelve una cadena, cuyo server.R representa como texto, y ui.R a su vez se representa como HTML.

Cualquier variable / datos a las que se server.R acceder en server.R (incluidos los archivos que se source ) se pueden mostrar en la interfaz de usuario. Lo que necesita es (1) un método de renderizado adecuado para representar estos datos en el server.R . server.R (2) un contenedor de salida adecuado para mantener la visualización de salida en 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") ))

servidor.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)