有没有办法在闪亮的应用程序中插入(并评估)RMarkdown脚本.(注意,我不是在寻找在此处解释的RMarkdown中的闪亮应用程序,也不是在寻找闪亮的Markdown脚本(请参阅Shiny Gallery Markdown))
我正在构建一个包含文本,方程,代码块,图和交互元素的应用程序.为方便起见,我使用Markdown文件作为文本和方程式,并希望有时在两者之间(即在RMarkdown中写入大部分内容).由于闪亮的应用程序更复杂(我使用它shinydashboard包括许多独特的功能),我宁愿选择不使用第一个链接中描述的方法.
最小的工作示例是:
R-文件:
library(shiny)
ui <- shinyUI(
fluidPage(
includeMarkdown("RMarkdownFile.rmd")
)
)
server <- function(input, output) {}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
和"RMarkdownFile.rmd"在同一个文件夹中:
This is a text
$$ E(x) = 0 $$
```{r, eval = T}
plot(rnorm(100))
```
Run Code Online (Sandbox Code Playgroud)
具体来说,我想得到代码块的评估(绘制一些东西......),我想得到渲染的数学方程式.
有任何想法吗?
感谢@Bunk的输入,我选择使用该命令将所有rmd文件渲染到md文件,knit然后将md文件包含在闪亮的应用程序中(我使用markdown而不是html,因为后者产生了方程式的一些问题).最后,将includeMarkdown其包裹起来withMathJax以确保正确显示方程式.
最终代码如下所示:
library(shiny)
library(knitr)
rmdfiles <- c("RMarkdownFile.rmd")
sapply(rmdfiles, knit, quiet = T)
ui <- …Run Code Online (Sandbox Code Playgroud) 如何在 64 位向量中将 NA 值从 Rcpp 传递给 R?
我的第一种方法是:
// [[Rcpp::export]]
Rcpp::NumericVector foo() {
Rcpp::NumericVector res(2);
int64_t val = 1234567890123456789;
std::memcpy(&(res[0]), &(val), sizeof(double));
res[1] = NA_REAL;
res.attr("class") = "integer64";
return res;
}
Run Code Online (Sandbox Code Playgroud)
但它产生
#> foo()
integer64
[1] 1234567890123456789 9218868437227407266
Run Code Online (Sandbox Code Playgroud)
我需要得到
#> foo()
integer64
[1] 1234567890123456789 <NA>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用传单映射 ~8000 个多边形并遇到性能问题。当我在一个闪亮的应用程序中使用地图时,我想知道是否可以以某种方式缓存或预渲染地图。
请注意,在我的情况下,我有不同的多边形层,它们按照这种方法进行交换。
一个小的 MWE 是这样的:
数据可以从这里下载
library(shiny)
library(leaflet)
library(sf)
## Download Shapefile
file <- "plz-gebiete.shp"
if (!file.exists(file)) {
url <- "https://www.suche-postleitzahl.org/download_files/public/plz-gebiete.shp.zip"
zipfile <- paste0(file, ".zip")
download.file(url, zipfile)
unzip(zipfile)
}
df <- st_read(file, options = "ENCODING=UTF-8")
# If possible: pre-render the map here!
library(shiny)
ui <- fluidPage(
leafletOutput("mymap", width = "700px", height = "700px")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = df, weight = 1, color …Run Code Online (Sandbox Code Playgroud) 我最近升级到R版本3.2.3,也升级到ggplot版本2.0.0.
尝试将一些旧代码升级到较新版本我遇到了ggplot2及其透明度设置的奇怪行为.
现在我的问题是,这是一个错误还是一个功能(如果是这样,有人可以告诉我为什么这样做有好处)?我想要的结果是(显然)情节2.
假设我绘制一条线并在其上放置一个透明的矩形,如下所示:
library(ggplot2)
plot_data <- data.frame(x = 1:100, y = rnorm(100))
# Plot 1
ggplot(data = plot_data, aes(x = x, y = y)) +
geom_line() +
geom_rect(aes(xmin = 20, xmax = 50, ymin = -Inf, ymax = Inf), fill = "red",
alpha = 0.1) + ggtitle("Plot 1")
# Plot 2
ggplot() +
geom_line(data = plot_data, aes(x = x, y = y)) +
geom_rect(aes(xmin = 20, xmax = 50, ymin = -Inf, ymax = Inf), …Run Code Online (Sandbox Code Playgroud) 我有一个使用DT-package 显示数据表的闪亮应用程序.我想要的是能够以自定义方式格式化列.例如,我想要一个货币值显示如下:1,234.50€而不是-way DT,它显示它像$ 1,234.5(注意符号的变化,货币符号的位置以及小数点后的数字-点).
MWE看起来像这样:
library(shiny)
library(DT)
shinyApp(
# UI
ui = fluidPage(DT::dataTableOutput('tbl')),
# SERVER
server = function(input, output) {
dat <- data.frame(cur = 1234.5, # supposed to be displayed as: 1,234.50€ | Bad!
# displayed as $1,234.5
perc = 0.123456, # 12.34% | Good!
num = 1000) # 1,000 | Bad! displayed as 1000
# render DT
output$tbl = DT::renderDataTable(
datatable(dat) %>%
formatCurrency(c('cur'), "$") %>%
formatPercentage('perc', 2) %>%
formatRound('num', digits = 0)
)
}
)
Run Code Online (Sandbox Code Playgroud)
但它做得相当不错,当将货币符号更改为时 …
我正在尝试创建一个为多个发行版生成随机数的类,同时保持它们的可重现性(通过设置初始种子).
代码似乎工作,直到我开始使用正态分布和奇怪的错误表面.这些主要是:
double a = rnd.rnorm(0.0, 1.0);-line(第40行)(即如果我rnorm在设置种子之前调用),则正态分布的第一个随机数不再匹配,随后的随机数再次匹配int n = 3;)现在我的问题是,是什么导致了这种奇怪的行为?我RNG是以错误的方式实施的吗?最重要的是,我该如何解决?
如果您想自己测试结果,可以使用此http://cpp.sh/9phre
或这个
#include <stdio.h>
#include <random>
// Class to create random numbers
// Main functions to set the seed: setseed()
// create uniformly distributed values: runif()
// and normally distributed values: rnorm()
class RNG {
public:
RNG(int seed = (int) time(0)) {
setseed(seed);
};
~RNG() {};
void setseed(int newSeed) {
re.seed(newSeed);
};
double runif(double minNum, double maxNum) …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来操作R中的data.table中的多个列.由于我必须动态地处理列以及第二个输入,所以我无法找到答案.
这个想法是通过将所有值除以日期值来索引某个日期的两个或更多系列,例如:
set.seed(132)
# simulate some data
dt <- data.table(date = seq(from = as.Date("2000-01-01"), by = "days", length.out = 10),
X1 = cumsum(rnorm(10)),
X2 = cumsum(rnorm(10)))
# set a date for the index
indexDate <- as.Date("2000-01-05")
# get the column names to be able to select the columns dynamically
cols <- colnames(dt)
cols <- cols[substr(cols, 1, 1) == "X"]
Run Code Online (Sandbox Code Playgroud)
第1部分:Easy data.frame/apply方法
df <- as.data.frame(dt)
# get the right rownumber for the indexDate
rownum <- max((1:nrow(df))*(df$date==indexDate))
# use apply to iterate …Run Code Online (Sandbox Code Playgroud) 我想抑制zip命令输出的消息,r但是我找不到正确的命令.
背景,因为我在函数中使用zip函数,我不希望用户看到有关所有文件的所有信息(大约5.000,这会使控制台变得混乱).
这是我到目前为止所尝试的,但所有功能都foo显示adding: hw.txt (stored 0%)或updating: hw.txt (stored 0%)
# create a small file
writeLines("hello world", "hw.txt")
# use the original command
zip("zip.zip", "hw.txt")
# try different options of capturing/suppressing output!
# assignment
foo1 <- function() a <- zip("zip.zip", "hw.txt")
foo1()
# capture.output
foo2 <- function() a <- capture.output(zip("zip.zip", "hw.txt"))
foo2()
# suppressMessages
foo3 <- function() suppressMessages(zip("zip.zip", "hw.txt"))
foo3()
# invisible
foo4 <- function() invisible(zip("zip.zip", "hw.txt"))
foo4()
# sink
foo5 <- …Run Code Online (Sandbox Code Playgroud) 我编写了一个将字符值读入std::vector<std::string>向量的解析器,它可以在亚秒内解析 100 万条记录。然后我想将向量转换为 a Rcpp::DataFrame,这需要超过 80 秒...
有没有办法Rcpp::DataFrame“有效地”从大字符向量创建一个?
使用数值时,我会尝试std::memcpy()使用std::vectorto Rcpp::NumericVector(有关更多信息,请参阅此int64 示例或此data.table 示例),但这似乎不适用于字符向量,因为它们的大小不同。
该函数的基本思想是解析数独字符串数据(每个数独字符串正好是81个字符长),每行有两个数独(数据保存为.csv文件,你可以在这里找到数据)。
$ head sudoku.csv
quizzes,solutions
004300209005009001070060043006002087190007400050083000600000105003508690042910300,864371259325849761971265843436192587198657432257483916689734125713528694542916378
040100050107003960520008000000000017000906800803050620090060543600080700250097100,346179258187523964529648371965832417472916835813754629798261543631485792254397186
600120384008459072000006005000264030070080006940003000310000050089700000502000190,695127384138459672724836915851264739273981546946573821317692458489715263562348197
497200000100400005000016098620300040300900000001072600002005870000600004530097061,497258316186439725253716498629381547375964182841572639962145873718623954534897261
005910308009403060027500100030000201000820007006007004000080000640150700890000420,465912378189473562327568149738645291954821637216397854573284916642159783891736425
100005007380900000600000480820001075040760020069002001005039004000020100000046352,194685237382974516657213489823491675541768923769352841215839764436527198978146352
009065430007000800600108020003090002501403960804000100030509007056080000070240090,289765431317924856645138729763891542521473968894652173432519687956387214178246395
000000657702400100350006000500020009210300500047109008008760090900502030030018206,894231657762495183351876942583624719219387564647159328128763495976542831435918276
503070190000006750047190600400038000950200300000010072000804001300001860086720005,563472198219386754847195623472638519951247386638519472795864231324951867186723945
Run Code Online (Sandbox Code Playgroud)
的CPP的内部读功能,我fread()的文件,填充缓冲液(buffer)并解析数据到所述std::vector<std::string>载体(a和b在本例中)
请注意,包括我迄今为止所做的实验在内的完整代码可以在这个 gist 中找到。
const int BUFFERSIZE = 1e8;
const int n_lines = count_lines(filename); // 1 million in this case
FILE* infile;
infile …Run Code Online (Sandbox Code Playgroud) 我有同一个包的多个版本foo(全部具有一个函数bar),我都想在同一个脚本中使用它们。
在这个问题之后,我可以v1使用 加载包library("foo", lib.loc = "pkgs/v1")。但这会加载包中的所有函数。
现在我想foo::bar从版本 v1 分配到版本v2bar_v1到foo::bar版本 v2 来bar_v2独立调用它们。但我没有看到在给定 lib 位置的情况下仅加载库的一个函数的选项(例如,解决方案是lib.loc在函数调用中指定 a bar_v1 <- foo::bar)。
这在 R 中可能吗?
我在这里创建了一个测试包github.com/DavZim/testPkgfoo ,它有一个打印包版本(硬编码)的函数。该软件包有两个版本,每个版本一个。
要获取包的 tar.gz 文件,您可以使用此
# Download Files from https://github.com/DavZim/testPkg
download.file("https://github.com/DavZim/testPkg/releases/download/v0.1.0/testPkg_0.1.0.tar.gz", "testPkg_0.1.0.tar.gz")
download.file("https://github.com/DavZim/testPkg/releases/download/v0.2.0/testPkg_0.2.0.tar.gz", "testPkg_0.2.0.tar.gz")
Run Code Online (Sandbox Code Playgroud)
然后以以下形式设置文件夹结构
pkgs/
0.1.0/
testPkg/
0.2.0/
testPkg/
Run Code Online (Sandbox Code Playgroud)
我用
if (dir.exists("pkgs")) unlink("pkgs", recursive = TRUE)
dir.create("pkgs")
dir.create("pkgs/0.1.0")
dir.create("pkgs/0.2.0")
# install the packages locally …Run Code Online (Sandbox Code Playgroud)