- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将仪表板的一些代码分解为模块。我遇到了这个问题 rCharts
代码。我可以将其作为应用程序运行,但理想情况下我想将其拆分为 UI
和 server
功能,以便我可以将它们保存在一个包中。
下面的代码显示了应用程序中的工作代码和作为模块的损坏代码。谁能指出我做错了什么?
谢谢
---
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}
```
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")
```
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
, orUI
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
})
}
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/
每个人(希望)都在努力实现代码模块化。我想要做的是有 1 个主要的 Sass 文件,它导入我的所有模块,这些模块是局部的,如果需要,这些局部可以调用它们自己的局部组。我想要的是,不是在我的代码库中调用
如何在 xslt 转换中模块化一组重复的输出?例如,我有如下内容(伪代码)。 并
假设我有几个简单的模型驻留在 food.py 中: import peewee as pw db = pw.SqliteDatabase('food.db') class BaseModel(pw.M
我正在开始一个新的 Angular 项目并尝试模块化我的所有代码——我厌倦了拥有大量的 app.js 文件,而且因为我正在为一家公司开发一个平台,所以我的代码整洁且模块化以便于测试、清洁和易于过渡到
所以,有人告诉我,在 nodeJS 中传递 request 和或 response 变量是“不好的做法”。但这意味着你的大部分代码都必须在 server.js 文件中,这使得它变得困惑而且有点难看。
有一个想法:函数(在 FP 中)可以以与 OOP 中的组件类似的方式组成。对于 OOP 中的组件,我们使用接口(interface)。对于函数,我们可以使用委托(delegate)。目标是实现分解、模
有没有办法将 SQL 代码模块化,使其更具可读性和可测试性? 我的 SQL 代码经常变成一长串复杂的嵌套连接、内连接等,难以编写和调试。相比之下,在像 Javascript 或 Java 这样的过程语
我想知道大公司如何倾向于在他们的页面上模块化组件。 Facebook 就是一个很好的例子: There's a team working on Search that has its own CSS,
我正在寻找在 WPF 中构建模块化应用程序模型的解决方案。目前,我使用 Devexpress POCO MVVM 来构建我的 WPF 应用程序,但缺乏模块化的可扩展性,我正在寻找适合我当前设计并允许构
我制作了一个 Gradle 项目,它使用类加载器从子目录资源/文本中加载文本文件。此时它可以工作,但是当我将项目转换为模块化 JavaFX 程序时,相同的类加载器函数会给出 NullPointerEx
假设我有一个通用类模块: export class MyCalc { data = {} ... } 并说我想扩展更多功能: export class MyCalcLoader {
我的模板文件变得越来越大并且过于复杂(大约 200 行(长)代码,9 级缩进),因此它也变得容易出错。我正在寻找一个简单的解决方案,它可以让我轻松访问 $scope 变量和函数。 我的第一个想法是使用
许多人说要将外部 CSS 和 JavaScript 文件的数量保持在最低限度以减少往返时间。例如,Google 建议每个网站最多分别使用两个 CSS 和 JavaScript 文件。 问题是,作为“模
我试图找出为什么我的 Promise 链执行无序,尽管编写了一个非嵌套的 then 链。我的函数已经模块化,以减少链中发生的代码膨胀(我期望有五个 then 方法),并且我不确定这些模块中的某些内容是
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我使用 create-react-app 创建了一个样板 React 应用程序。 现在,在我的 App.js 文件中 import classes from './App.css'; 我做到了
Java 模块系统是否应该阻止模块通过反射访问其他模块,而不声明正确的模块依赖关系? 例如,在编译这个 hello world Java 11 类时,它从另一个模块调用类,正如预期的那样,它不会编译,
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我的应用程序上有许多不同的“控制元素”:下拉菜单、选项卡、菜单等。在同一页面上,有许多相同的控件。当编写 JavaScript 来处理与每个控件关联的不同事件时,我试图使我的代码尽可能干燥。挑战之一是
处理以下场景的模块化方式是什么:应用程序具有所有标题标签(h1、h2、h3 等)的通用样式。特定组件 Widget.jsx 可以使用这些标题中的任何一个,但 h1 标签具有特殊样式。在 CSS 的“旧
我是一名优秀的程序员,十分优秀!