如何使用R Shiny声明全局变量,以便您不需要多次运行相同的代码片段?作为一个非常简单的例子,我有两个使用相同精确数据的图,但我只想计算数据ONCE.
这是ui.R文件:
library(shiny)
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("Hello Shiny!"),
# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("obs",
"Number of observations:",
min = 1,
max = 1000,
value = 500)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot1"),
plotOutput("distPlot2")
)
))
Run Code Online (Sandbox Code Playgroud)
这是server.R文件:
library(shiny)
shinyServer(function(input, output) {
output$distPlot1 <- renderPlot({
dist <- rnorm(input$obs)
hist(dist)
})
output$distPlot2 <- renderPlot({
dist <- rnorm(input$obs) …Run Code Online (Sandbox Code Playgroud) 我正在学习为R构建一个包.现在要设置它,我需要在加载包时运行一些代码require(myPackage).
我阅读的文档help(".onLoad")只是让我感到困惑,因为没有例子.我如何实际使用.onLoad?
有人能告诉我一个简单的例子吗?例如,我知道export(myfun)在NAMESPACE文件中将导出myfun以供使用,我需要rnorm(10)在包加载时运行的代码是什么?
我正在尝试运行此代码(Ubuntu 12.04,R 3.1.1)
# Load requisite packages
library(tm)
library(ggplot2)
library(lsa)
# Place Enron email snippets into a single vector.
text <- c(
"To Mr. Ken Lay, I’m writing to urge you to donate the millions of dollars you made from selling Enron stock before the company declared bankruptcy.",
"while you netted well over a $100 million, many of Enron's employees were financially devastated when the company declared bankruptcy and their retirement plans were wiped out",
"you sold $101 million worth …Run Code Online (Sandbox Code Playgroud) 对于每个X值,我计算了每个Y值的平均值Y和标准偏差(sd)
x = 1:5
y = c(1.1, 1.5, 2.9, 3.8, 5.2)
sd = c(0.1, 0.3, 0.2, 0.2, 0.4)
plot (x, y)
Run Code Online (Sandbox Code Playgroud)
如何使用标准偏差为我的绘图的每个数据点添加误差线?
我一直在使用R中并行技术的项目近期开工,并有我的程序使用Linux系统的工作mclapply从并行包.但是,我parLapply对Windows的理解遇到了障碍.
使用mclapply我可以设置核心数,迭代次数,并将其传递给我的工作区中的现有函数.
mclapply(1:8, function(z) adder(z, 100), mc.cores=4)
Run Code Online (Sandbox Code Playgroud)
我似乎无法在Windows中实现相同的功能parLapply.据我了解,我需要通过使用传递所有变量clusterExport()并将我想要应用的实际函数传递给参数.
这是正确的还是有类似于mclapply适用于Windows 的功能?
我有以下数据框:
test2 <- data.frame(groups = c(rep("group1",4), rep("group2",4)),
X2 = c(rnorm(4), rnorm(4)) ,
label = c(rep(1,2),rep(2,2),rep(1,2),rep(2,2)))
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法绘制每组每个标签的条形图:
ggplot(test2, aes(label, X2, fill=as.factor(groups))) +
geom_bar(position="dodge", stat="identity")
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法找到一个stat="mean"所以我可以在每个条形图而不是身份上绘制手段.
谢谢你的帮助.
在R中转置data.frame并将其中一列设置为新转置表的标头的最佳方法是什么?我编写了一种方法来执行此操作.因为我还是R的新手.我想建议改进我的代码以及更像R的替代品.不幸的是,我的解决方案也有点硬编码(即新的列标题位于某个位置).
# Assume a data.frame called fooData
# Assume the column is the first column before transposing
# Transpose table
fooData.T <- t(fooData)
# Set the column headings
colnames(fooData.T) <- test[1,]
# Get rid of the column heading row
fooData.T <- fooData.T[2:nrow(fooData.T), ]
#fooData.T now contains a transposed table with the first column as headings
Run Code Online (Sandbox Code Playgroud) 基函数的文档c()显示其默认参数为
c(..., recursive = FALSE)
Run Code Online (Sandbox Code Playgroud)
现在,如果我们定义
lst <- list(x = 1:5, y = 6:10)
Run Code Online (Sandbox Code Playgroud)
然后将列表与
c(lst, recursive = TRUE)
# x1 x2 x3 x4 x5 y1 y2 y3 y4 y5
# 1 2 3 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)
列表已折叠,并保留名称.
但我们也可以使用另一个未记录的参数use.names来删除名称.
c(lst, recursive = TRUE, use.names = FALSE)
# [1] 1 2 3 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)
为什么没有 use.names 记录为其中一个参数 c()?
当我调用rnorm单个值作为均值时,很明显会发生什么:从Normal(10,1)生成一个值.
y <- rnorm(20, mean=10, sd=1)
Run Code Online (Sandbox Code Playgroud)
但是,我看到传递给整个矢量的例子rnorm(或rcauchy等等); 在这种情况下,我不确定R机器到底做了什么.例如:
a = c(10,22,33,44,5,10,30,22,100,45,97)
y <- rnorm(a, mean=a, sd=1)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
可以为Tableau编写插件吗?Tableau是否配备了任何通用脚本语言?
例如,用于生成无法使用默认Tableau工具创建的可视化,或者使用各种指标等对数据集进行k均值聚类...