Van*_*pez 11
这是一个使用shinyjs和签名板的想法,使演示适应"绘制图像".
shinyjs在页面加载时运行JavaScript函数.阅读关于使用shinyjs::extendShinyjs 这里.请注意V8应该安装包的插图.CSS
.signature-pad {
position: absolute;
left: 0;
top: 0;
width: 600px;
height: 400px;
}
.wrapper {
position: relative;
width: 600px;
height: 400px;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
Run Code Online (Sandbox Code Playgroud)
应用
library(shiny)
library(dplyr)
library(ggplot2)
library(shinyjs)
jscode <- "shinyjs.init = function() {
var signaturePad = new SignaturePad(document.getElementById('signature-pad'), {
backgroundColor: 'rgba(255, 255, 255, 0)',
penColor: 'rgb(0, 0, 0)'
});
var saveButton = document.getElementById('save');
var cancelButton = document.getElementById('clear');
saveButton.addEventListener('click', function (event) {
var data = signaturePad.toDataURL('image/png');
// Send data to server instead...
window.open(data);
});
cancelButton.addEventListener('click', function (event) {
signaturePad.clear();
});
}"
server <- function(input, output, session){
output$plot1 <- renderPlot({
df <- sample_frac(diamonds, 0.1)
ggplot(df, aes(x = carat, y = price, color = color)) +
geom_point()
})
}
ui <- fluidPage(
includeCSS("custom.css"),
tags$head(tags$script(src = "signature_pad.js")),
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = jscode),
h1("Draw on plot"),
div(class="wrapper",
plotOutput("plot1"),
HTML("<canvas id='signature-pad' class='signature-pad' width=600 height=400></canvas>"),
HTML("<div>
<button id='save'>Save</button>
<button id='clear'>Clear</button>
</div>")
)
)
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
仅使用基本shiny功能,您可以构建一个应用程序,您可以在简单的绘图上绘制手动形状.我在plot这里使用基本功能,因此它反应更快.它使用的是click和hover参数plotOutput.如果你想在更复杂的预先存在的情节中进行,你可能更喜欢ggplot来更好地管理不同的图层?您还可以考虑为点添加样条平滑器.视觉:
应用程序代码(可在此处访问实时版本):
library(shiny)
ui <- fluidPage(
h4("Click on plot to start drawing, click again to pause"),
sliderInput("mywidth", "width of the pencil", min=1, max=30, step=1, value=10),
actionButton("reset", "reset"),
plotOutput("plot", width = "500px", height = "500px",
hover=hoverOpts(id = "hover", delay = 100, delayType = "throttle", clip = TRUE, nullOutside = TRUE),
click="click"))
server <- function(input, output, session) {
vals = reactiveValues(x=NULL, y=NULL)
draw = reactiveVal(FALSE)
observeEvent(input$click, handlerExpr = {
temp <- draw(); draw(!temp)
if(!draw()) {
vals$x <- c(vals$x, NA)
vals$y <- c(vals$y, NA)
}})
observeEvent(input$reset, handlerExpr = {
vals$x <- NULL; vals$y <- NULL
})
observeEvent(input$hover, {
if (draw()) {
vals$x <- c(vals$x, input$hover$x)
vals$y <- c(vals$y, input$hover$y)
}})
output$plot= renderPlot({
plot(x=vals$x, y=vals$y, xlim=c(0, 28), ylim=c(0, 28), ylab="y", xlab="x", type="l", lwd=input$mywidth)
})}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你..
iframe使用draw.io在线工具的小例子
#rm(list = ls())
library(shiny)
ui <- fluidPage(titlePanel("Getting Started with draw.io"), htmlOutput("frame"))
server <- function(input, output) {
output$frame <- renderUI({
tags$iframe(src="https://www.draw.io", height=1000, width=1400)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)