本文介绍了如何在 RStudio 中将 Shiny 应用程序代码拆分为多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个闪亮的应用程序的代码拆分到不同的文件中,但无法让它在 Shiny 中正常工作.我的尝试可以在在这个演示

I tried to split code for a shiny application over different files, but can't get this to work decently in Shiny. My attempt can be found in this demo

如何将我的代码拆分到不同的文件中,但仍保留运行应用程序按钮"并在 RStudio 中恢复代码完成"?

How can I split up my code over different files, but still keep the 'Run App button ' and have 'Code Completion' back in RStudio?

如果没有!我可以将 Shiny 与 Visual Studio 集成吗?

if not ! can i integrate shiny with Visual Studio ?

推荐答案

是的,您可以使用与 RStudio 中的每个其他项目相同的方式轻松实现这一目标:通过使用提供的 R 机制来实现:

Yes, you can achieve that very easily in the same way you do that with every other project in RStudio: by using the R mechanisms provided to achieve that:

  • 在单独的文件中定义函数和/或对象.
  • 在主文件中使用 source() 来加载它们的定义

闪亮函数的代码完成仅在使用 library(shiny) 加载闪亮包时在 RStudio 中发生.应用程序中的主文件将显示运行应用程序"按钮.在下面的示例中,这将是 app.R 文件.因此,如果您想从 RStudio 中运行您的应用程序,您必须始终返回主文件.

Code completion for shiny functions only occurs in RStudio when the shiny package is loaded using library(shiny). The 'Run App' button will be visible for the main file in the application. In the example below, that would be the app.R file. So if you want to run your app from within RStudio, you always have to go back to the main file.

一个例子:

在文件 app.R 中:

library(shiny)
source('myUI.R', local = TRUE)
source('myServer.R')


shinyApp(
  ui = myUI,
  server = myserver
)

这段代码除了启动对象 myUImyserver 并调用应用程序外,什么都不做.

This code does nothing else but initiate the objects myUI and myserver and call the app.

文件 myUI.R 包含

source('Tabs.R')
myUI <- shinyUI({
  fluidPage(
    tabsetPanel(
      Tab1,
      Tab2
    )
  )
})

此文件定义了 app.R 中使用的 UI 对象.函数 tabsetPanel 将多个 tabPanel 作为参数.这些 tabPanel 是在以下文件 (Tabs.R) 中创建的,因此必须在构建 UI 之前获取它们.

This file defines the UI object used in app.R. The function tabsetPanel takes a number of tabPanels as arguments. These tabPanels are created in the following file (Tabs.R), so that one has to be sourced before the UI is constructed.

文件 Tabs.R 包含:

Tab1 <- tabPanel("First Tab",
                 selectInput("select",
                             "Choose one",
                             choices = letters[1:3],
                             selected = 'a'))

Tab2 <- tabPanel("Second Tab",
                 textOutput('mychoice'))

此文件创建要添加到 tabsetPanel 的 tabPanel 对象.在我自己的代码中,我将每个 tabPanel 定义存储在一个单独的文件中.

This file creates the tabPanel objects to be added to the tabsetPanel. In my own code, I store every tabPanel definition in a separate file.

文件 myServer.R 包含:

myserver <- function(input,output,session){
  output$mychoice <- renderText(
    input$select
  )
}

如果需要,您可以再次创建单独的文件,其中包含可在服务器功能内部使用的功能.但是您始终必须遵循经典的 R 逻辑:将事物分配给一个对象并在您想要插入它的位置引用该对象.

And if you want, you can again create separate files with functions that can be used inside the server function. But you always have to follow the classic R logic: assign things to an object and refer to that object at the position you want to insert it.

您也可以直接在 server() 函数中获取源代码.在这种情况下,您应该使用 source(..., local = TRUE) 在本地获取源代码,因此创建的对象包含在 server 函数中.另请参阅:https://shiny.rstudio.com/articles/scoping.html

You can also source code directly inside the server() function. In that case you should source locally using source(..., local = TRUE), so the created objects are contained inside the server function. See also : https://shiny.rstudio.com/articles/scoping.html

如果你想把它提升一个档次并重用特定的逻辑和布局(例如应该附加到一些绘图的绘图选项控制面板),你应该去模块.(另见 http://shiny.rstudio.com/articles/modules.html )

If you want to take it up a notch and reuse a certain logic and layout (eg a plot option control panel that should be attached to some plots), you should go to modules. (see also http://shiny.rstudio.com/articles/modules.html )

模块可以再次存储在单独的文件中,该文件来源于您拥有的 app.R 文件.

Modules can again be stored in a separate file, and that file sourced in the app.R file you have.

这篇关于如何在 RStudio 中将 Shiny 应用程序代码拆分为多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:06