【问题标题】:Is there a way to list the objects(dataframes, functions, args(funtions), inputs, outputs of a shiny app有没有办法列出闪亮应用程序的对象(数据框、函数、参数(函数)、输入、输出
【发布时间】:2020-08-01 02:01:19
【问题描述】:

有没有办法列出闪亮应用程序的对象(数据帧、函数、args(函数)、输入、输出。在下面的应用程序中,有 actionButton()、numericInput() 等输入和输出renderText()。有没有办法将这些与 ID 一起列出(例如,动作按钮有“goButton”)等等。

此外,这里声明了一个名为“asd”的函数,带有参数 (3,4)。我们也可以列出这些信息吗?请指导

ui.R

source("fun.R")

pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText")
  )
)

服务器.R

function(input, output) {

  # builds a reactive expression that only invalidates 
  # when the value of input$goButton becomes out of date 
  # (i.e., when the button is pressed)
  ntext <- eventReactive(input$goButton, {
    input$n
  })

  output$nText <- renderText({
    asd(3,4)
  })
}

fun.R

asd <- function(a,b)
{ 
c <- a + b
return(c)
}

【问题讨论】:

    标签: shiny


    【解决方案1】:

    您可以使用操作按钮、显示、隐藏 Shinyjs 和逐字文本输出来显示应用程序中特定输出的代码。像这样:

    library(shiny)
    library(shinyjs)
    
    asd <- function(a,b)
    { 
      c <- a + b
      return(c)
    }
    
    ui <- pageWithSidebar(
    
    
      headerPanel("actionButton test"),
      sidebarPanel(
        numericInput("n", "N:", min = 0, max = 100, value = 50),
        br(),
        actionButton("goButton", "Go!"),
        p("Click the button to update the value displayed in the main panel.")
      ),
      mainPanel(
    
        useShinyjs(),
    
        tags$head(tags$style("
    
        #nText {
          color: #333;
          background-color: #f5f5f5;
          border-radius: 4px;');
    
        }
    
        pre {
            font-size: 90%;
            color: #c7254e;
            border-radius: 4px;
    
        }")),
    
        verbatimTextOutput("nText"),
        actionButton("showtextcode", "Show Code"),
        verbatimTextOutput("textoutputcode"),
        )
    )
    
    server <- function(input, output) {
    
      # builds a reactive expression that only invalidates 
      # when the value of input$goButton becomes out of date 
      # (i.e., when the button is pressed)
    
      shinyjs::hide("textoutputcode")
    
      ntext <- eventReactive(input$goButton, {
        input$n
      })
    
      output$nText <- renderText({
        asd(2,3)
      })
    
      output$textoutputcode <- renderText({
        "output$nText <- renderText({
            asd(2,3)
    })"
      })
    
      observeEvent(input$showtextcode, {
    
        if(input$showtextcode %% 2) {
          shinyjs::show("textoutputcode")
        } else {
          shinyjs::hide("textoutputcode")
        }
      }) 
    
    
    }
    
    shinyApp(ui, server)
    

    结果:

    如果您想显示应用程序的所有代码,您可以创建一个单独的选项卡来显示完整代码,并将所有内容放在逐字文本输出中。然而,仅仅因为你可以并不意味着你应该。例如,如果您的闪亮应用程序跨越 5000 行,那么这有点愚蠢!

    最好简单地包含一个指向应用程序源代码的 github 链接,如果用户有兴趣,他们可以直接点击该链接。请记住,shiny 旨在共享交互式输出,而不是完整的源代码。

    【讨论】:

    • 感谢您的宝贵时间。但基本上我的问题有点棘手。我的想法是,假设我们有闪亮的应用程序(任何应用程序)。文件名是“ABC.R”(ui.R 和 server.R) 好吗?现在,无论应用程序中的任何函数、数据帧、矩阵、输入、输出如何。用户应该知道细节。在您的代码中,您自己已将asd(2,3) 写为对象。但是您是否建议我需要为所有应用程序编写此代码(其中包含 100 个函数)。
    • Contd... 基本上我的想法是我需要在我的应用程序含义中创建一个工作流,所有输入都在那里(我的意思是标签),所有输出都在那里(例如 output$table ),所有数据帧都有哪些?所有的功能都在那里(还有参数)等等。所以如果我准备好了。对于任何应用程序(即使是 2000 行或 20 行),
    • Contd .. 我可以详细了解它的结构。说得通?其实坦率地说,这很难解释,很多人不明白我在做什么。但我相信这是非常有用的。
    • 恐怕我不明白你想要什么。你想显示代码吗?或者你想要一个按钮,一旦你点击它就可以检索 R 代码,而不必像我一样手动输入它?
    • 我们可以进入聊天框吗?我可以详细解释一下
    猜你喜欢
    • 2020-11-05
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多