如何通过plumber/REST API将shiny的输出呈现给其他程序员?

g0czyy6m  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(177)

嘿,闪亮和水管Maven,
相关链接:https://abndistro.com/post/2019/07/06/deploying-a-plumber-api-on-aws-ec2-instance/
https://abndistro.com/post/2019/07/06/deploying-a-shiny-app-with-shiny-server-on-an-aws-ec2-instance/
基本上,我们试图通过Plumber将shiny的输出传递给其他程序员,以集成到网页中。
从文档中,在本地计算机上运行,以下工作分别:

管道工重复

#* Plot a histogram
#* @png
#* @get /plot
function(){
  random_num <- rnorm(10) * 5
  hist(random_num)
}
saving the file as plumber.R
# Running the below in console 
r <- plumb("plumber.R")  # Where 'plumber.R' is the location of the file shown above
r$run(port=8000)

# Now, loading the browser: http://127.0.0.1:8000/__swagger__/ 
[![plumber output][1]][1]

同样闪亮的reprex:

library(shiny)
# Define UI for application that plots random distributions 
ui = shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for number of observations
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", 
                  "Number of observations:", 
                  min = 1, 
                  max = 1000, 
                  value = 500)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

# Define server logic required to generate and plot a random distribution
server = shinyServer(function(input, output) {

  # Expression that generates a plot of the distribution. The expression
  # is wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 

  output$distPlot <- renderPlot({

    # generate an rnorm distribution and plot it
    dist <- rnorm(input$obs)
    hist(dist)
  })

})

# Run the app
shinyApp(ui, server)

如何将shiny的输出,即直方图渲染为plumber的swagger?

即如何显示用户

尝试:在Shiny的renderFunction中嵌入plumber函数

library(shiny)
# Define UI for application that plots random distributions 
ui = shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for number of observations
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", 
                  "Number of observations:", 
                  min = 1, 
                  max = 1000, 
                  value = 500)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

# Define server logic required to generate and plot a random distribution
server = shinyServer(function(input, output) {

  # Expression that generates a plot of the distribution. The expression
  # is wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 

  #### Embedding inside the shiny
  output$distPlot <- renderPlot({
    #* Plot a histogram
    #* @png
    #* @get /plot
    function(){
    # generate an rnorm distribution and plot it
    dist <- rnorm(input$obs)
    hist(dist)
    }
  })

})

shinyApp(ui, server)
7kqas0il

7kqas0il1#

您需要在ui/server定义之外干净地定义plotting函数。下面的代码可以作为shiny app使用,并且也可以被plumber正确解析:

library(shiny)
# Define UI for application that plots random distributions 

ui = fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for number of observations
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", 
                  "Number of observations:", 
                  min = 1, 
                  max = 1000, 
                  value = 500)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

#* Plot a histogram
#* @param n Number or observations
#* @get /plothist
#* @serializer png
plothist <- function(n){
  # generate an rnorm distribution and plot it
  dist <- rnorm(as.integer(n))
  hist(dist, breaks=100)
}

server = function(input, output) {
  output$distPlot <- renderPlot({
    plothist(input$obs)
  })
}

shinyApp(ui, server)

相关问题