gpt4 book ai didi

r - Flexdashboard - 模块化 rCharts 代码

转载 作者:行者123 更新时间:2023-12-04 09:07:49 25 4
gpt4 key购买 nike

我正在尝试将仪表板的一些代码分解为模块。我遇到了这个问题 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}

```

更新以响应@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")
```

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

It's a bit unclear to me, do the suffixes UI, Input, Output affect behavior or is that a suggested naming convention?



其中一位作者已回答:

Just a suggested naming convention, they don't affect anything.

最佳答案

您的代码有两个小问题:

1.模块功能命名不符合the documentation you are referring to

A module’s UI function should be given a name that is suffixed with Input, Output, or UI





Module server functions should be named like their corresponding module UI functions, but without the Input/Output/UI suffix



在这里,我将它们重命名为 myUI()my() :
myUI = function(id) {
ns = NS(id)
mainPanel(plotOutput("plot1", height = "100%"),
showOutput(ns("histogram"), "nvd3"))
}

my = 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
})
}

2.调用模块服务器功能的方式就像它们是独立的一样

相反,它们必须由 ui() 调用。和 server()它们本身由 shinyApp() 调用.
ui <- fluidPage(
myUI("example")
)

server <- function(input, output, session) {
callModule(my, "example")
}

shinyApp(ui, server)

关于r - Flexdashboard - 模块化 rCharts 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com