Flexdashboard - 模块化rCharts代码

Akh*_*air 5 r shiny rcharts flexdashboard

我试图将仪表板的一些代码分解为模块.我在使用此rCharts代码时遇到问题.我可以将它作为一个应用程序运行,但理想情况下我想将它分成UIserver函数,以便我可以将它们保存在一个包中.

下面的代码显示了应用程序中的工作代码以及作为模块的损坏代码.任何人都可以指出我做错了吗?

谢谢

---
title: "Example"
output: 
  flexdashboard::flex_dashboard:
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(rCharts)
X <- data.frame(Var1 = rep(1:10, 3),
                Var2 = rep(c("control", "treatment1", "treatment2"), each = 10),
                Freq = abs(rnorm(30, 0, 1))
)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Broken Code as Module

```{r}
ui2 = function(id) {
  ns = NS(id)

  mainPanel(plotOutput("plot1", height = "100%"),
            showOutput(ns("histogram"), "nvd3"))
}

server2 = function(input, output, session) {
      output$histogram <- renderChart2({
        n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
        n2$set(width = session$clientData$output_plot1_width)
        n2
      })
}

ui2("example")
callModule(server2, "example")
```

Column {data-width=350}
-----------------------------------------------------------------------

### Working Code as App

```{r}
shinyApp(
  ui = mainPanel(
        plotOutput("plot2", height = "100%"),
        showOutput("histogram", "nvd3")
      ),

  server = function(input, output, session) {
      output$histogram <- renderChart2({
        n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
        n2$set(width = session$clientData$output_plot2_width)
        n2
      })
    }
)
```

### Template

```{r}

```
Run Code Online (Sandbox Code Playgroud)

更新以回应@ HubertL的回复

下面的代码,当附加到上面的主体时,说明了flexdashboard可以将shiny应用程序作为独立函数运行的方式(请注意runtime: shiny标题中的内容).

这里的独立功能可以存储在一个库中,随意调用,不需要包装shinyApp.

为了澄清,我问的问题是我如何完成将rCharts绘图称为一系列独立的UI和服务器功能,正如我可以正常使用plots,或者如果这不可能,为什么不呢?

Stand Alone Example
======================================================================

Inputs{.sidebar}
-----------------------------------------------------------------------

```{r}
## UI for choosing species
iris_plotUI = function(id) {
  ns = NS(id)

  # User choices for line
  list_species = list(
    "Setosa" = "setosa",
    "Versicolor" = "versicolor",
    "Virginica" = "virginica"
  )

  flowLayout(
    # input: Tube line selection
    selectInput(
      ns("type"),
      label = h3("Select Species:"),
      choices = list_species,
      selected = "setosa"
    )
  )
}

# server module
iris_plot = function(input, output, session) {
  library(MASS)
  library(data.table)

  dt = data.table::copy(iris)
  data.table::setDT(dt)

  iris_filtered = reactive({
    dt[Species == input$type]
  })

  output$scatter = renderPlot({
    plot(iris_filtered()$Petal.Width, 
       iris_filtered()$Petal.Length)
  })

}

# plotting module
iris_plotOutput = function(id) {
  ns = NS(id)

  plotOutput(ns("scatter"))
}

## call inputs in the sidebar
iris_plotUI("ns_iris")
```

Column
-----------------------------------------------------------------------

```{r}
## all server module and plot (plot in main panel) 
callModule(iris_plot, "ns_iris")
iris_plotOutput("ns_iris")
```
Run Code Online (Sandbox Code Playgroud)

WRT命名约定,这已在博客文章的评论中澄清.消息:

这有点不清楚,后缀UI,输入,输出影响行为还是建议的命名约定?

其中一位作者回答:

只是一个建议的命名约定,它们不会影响任何东西.

Akh*_*air 0

我用 anhtmlwidget而不是rCharts来解决这个问题。仍然不确定为什么rCharts不让我在htmlwigdets需要时将这些函数作为独立的函数调用,但正如htmlwidgets似乎概括的那样rCharts,这对我来说解决了这个问题。