首页 > 解决方案 > How can I call my shinyapp through functions?

问题描述

I deployed a shiny app as a library. I want to know how to open a file in my shiny app but through an R function.

I mean; I want to have two different functions:

  1. open(fileName) This function will open the shiny app with the file that the user will send as a parameter.

  2. create() This function will open the shiny app with a new file

I am confused about how can I organize my application. For example, if the user types open(Test1), then deploy my app with the file Test1 loaded or what happened if the user types create().

Do I need to create different scripts of app.R and then inside each function call the app.R that will help me with the behavior expected?

标签: rshiny

解决方案


一个可重复的示例将有助于理解您的确切需求(例如,“打开文件”究竟是什么意思),但我认为这里有一些内容可以帮助您入门。

这是基于此处提供的第一个 Shiny 示例。基本上,整个闪亮的应用程序是在一个名为 的函数内定义的gadget_runner(),然后应用程序在函数的末尾运行shiny::runGadget(ui, server)。您可以根据传递的参数定义函数以不同方式操作。然后您可以定义您的函数open(fileName)create()根据需要调用该函数。

gadget_runner <- function(fileName) {
  if (missing(fileName)) {
    x <- tempfile()
  }

  ui <- shiny::fluidPage(
    shiny::titlePanel(x),
    shiny::sidebarLayout(
      shiny::sidebarPanel(
        shiny::sliderInput(
          inputId = "bins",
          label = "Number of bins:",
          min = 1,
          max = 50,
          value = 30
        )
      ),
      shiny::mainPanel(
        shiny::plotOutput(outputId = "distPlot")
      )
    )
  )

  server <- function(input, output) {
    output$distPlot <- shiny::renderPlot({
      x <- faithful$waiting
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x,
        breaks = bins, col = "#75AADB", border = "white",
        xlab = "Waiting time to next eruption (in mins)",
        main = "Histogram of waiting times"
      )
    })
  }

  shiny::runGadget(ui, server)
}

open <- function(fileName) {
  gadget_runner(fileName)
}

create <- function() {
  gadget_runner()
}


推荐阅读