假设我在查看器窗格中查看了以下HTML
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, "index.html")
write('<h1> Content</h1>', htmlFile, append = TRUE)
write('<h2> Content</h2>', htmlFile, append = TRUE)
write('lorem ipsum...', htmlFile, append = TRUE)
viewer <- getOption("viewer")
viewer(htmlFile)
Run Code Online (Sandbox Code Playgroud)
当我在查看器窗格中有这个html时,我可以点击"另存为图像"按钮:
我将html内容作为png,例如:
有没有办法用命令行执行此操作?我知道rstudioapi::savePlotAsImage(),所以我正在寻找一种saveViewerAsImage.
编辑:我知道我们可以使用{webshot}包来做到这一点,但我正在寻找能够做到这一点的RStudio函数.
我正在尝试在 R 中使用 PostgreSQL 的大对象(https://www.postgresql.org/docs/10/largeobjects.html{DBI} )功能,但使用/编写和读取时遇到一些问题{RPostgres}。
这是我到目前为止所尝试过的:
# Getting the db
docker run --rm --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5433:5432 postgres
Run Code Online (Sandbox Code Playgroud)
library(DBI)
con <- dbConnect(
RPostgres::Postgres(),
dbname = "postgres",
host = "localhost",
port = 5433,
user = "postgres",
password = "mysecretpassword"
)
Run Code Online (Sandbox Code Playgroud)
创作作品:
> dbGetQuery(con, "SELECT lo_create(1234);")
lo_create
1 1234
Run Code Online (Sandbox Code Playgroud)
但后来我很难弄清楚如何将 R 对象写入这个大对象。例如,我如何在 Postgres 中使用and编写mtcars一个大对象?{DBI}{RPostgres}
然后,我如何在 R 中再次读回它?
这是一个简单的闪亮应用程序:
library(shiny)
library(leaflet)
nc = sf::st_read(system.file("shape/nc.shp", package="sf"))
ui <- function(request){
tagList(
selectInput("color", "color", c("blue", "red", "green")),
leafletOutput("map")
)
}
server <- function(
input,
output,
session
){
output$map <- renderLeaflet({
leaflet(nc) %>%
addPolygons(color = input$color)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我想在每个多边形上的单击时绑定一个事件,但我希望它仅从客户端发生,我不希望它通过 R。例如,我想在用户单击时发送警报在多边形上。
我有一些黑客代码可以做到这一点,但我希望有一种干净的方法来做到这一点。我正在寻找一种从 R 定义看起来像的东西的方法addPolygon(onClick = "alert('hello there')")。
需要明确的是,我不希望这通过服务器,我希望一切都在浏览器中发生。
它适用于以下 JS 代码(在 ext/script.js 中)
$(document).ready(function() {
Shiny.addCustomMessageHandler('bindleaflet', function(arg) {
$("#" + arg).find("path").remove();
wait_for_path(arg);
})
});
var wait_for_path = function(id) {
if ($("#" + id).find("path").length !== 0) {
$("#" …Run Code Online (Sandbox Code Playgroud) 我想在我的 Shiny 应用程序中插入非预定义数量的图表。我使用了一个for循环和一系列的insertUI。
问题是当我运行它时,文本元素的行为符合预期,但图表都呈现相同的图像。我怎样才能防止这种情况发生?
这是一个代表:
library(shiny)
ui <- fluidPage(
tags$div(
class = "this",
actionButton("go", "go")
)
)
server <- function(input, output, session) {
observeEvent( input$go , {
x <- reactiveValues(x = list(iris, mtcars, airquality))
for (i in 1:3){
insertUI(
".this",
ui = tagList(
p(paste("Number", i)),
renderPlot({
plot(x$x[[i]])
})
))
}
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我正在尝试在{ggimage}图层中使用非平方图像,但我似乎无法使比例适合原始图像比例。文档指向了这个asp论点,但这似乎不起作用。
这是一个明显的非方形图像:https : //ps.w.org/insert-headers-and-footers/assets/banner-772x250.png
以下是结果{ggimage}:
library(ggimage)
library(ggplot2)
d <- data.frame(
x = 1,
y = 1,
image = "https://ps.w.org/insert-headers-and-footers/assets/banner-772x250.png"
)
Run Code Online (Sandbox Code Playgroud)
ggplot(d, aes(x, y)) +
geom_image(aes(image=image), size = 1)
ggplot(d, aes(x, y)) +
geom_image(aes(image=image), size = 1, asp = 0.1)
ggplot(d, aes(x, y)) +
geom_image(aes(image=image), size = 1, asp = 0.5)
ggplot(d, aes(x, y)) +
geom_image(aes(image=image), size = 1, asp = 0.9)
Run Code Online (Sandbox Code Playgroud)
如何让图片保持原来的宽高比?
r ×5
shiny ×2
ggimage ×1
javascript ×1
leaflet ×1
postgresql ×1
r-leaflet ×1
rpostgresql ×1
rstudio ×1