问题描述
我的目标是创建一个脚本,在该脚本中最初显示一个空白的可打印图形.如果用户单击表示添加点"的Shiny actionButton,则将通过htmlWidgets的onRender()函数将点添加到绘图中.这将是有效的,因为当用户选择"Shiny" actionButton时,不需要重新绘制背景空白图.
My goal is to create a script where initially a blank plotly graph is displayed. If the user clicks on a Shiny actionButton saying "Add points", then points will be added to the plotly graph via the onRender() function of htmlWidgets. This would be efficient because the background blank plotly graph would not need to be replotted when the user selects the Shiny actionButton.
但是,为了实现此目的,我需要找出一种方法来将Shiny actionButton中的更改直接指示给onRender()函数,以使空白的可打印图形(在下面的代码中称为"pP )完全不会被更改.我在下面的onRender()代码中添加了两条注释行作为if语句,以显示我的目标.
However, in order for me to achieve this, I would need to figure out a way to indicate the change in the Shiny actionButton directly into the onRender() function so that the blank plotly graph (in the code below called "pP") would not be altered at all. I put two commented lines as an if statement in the onRender() code below to show what I am aiming for.
我知道有一个称为Shiny.onInputChange('variable',variable)的函数,可以在onRender()函数内部调用以保存在onRender()内部创建的变量,以便像Shiny一样使用在onRender()函数外部输入.因此,我想我正在寻找相反的方法(将闪亮的输入值直接传输到onRender()函数中.)
I know there is a function called Shiny.onInputChange('variable', variable) that can be called inside the onRender() function to save a variable that was created inside onRender() so that it can be used like a Shiny input outside of the onRender() function. So, I guess I am looking for something that does the reverse (transfers a shiny input value directly into the onRender() function).
ui <- shinyUI(fluidPage(
uiOutput("add"),
plotlyOutput("myPlot", height = 700)
))
server <- shinyServer(function(input, output) {
output$add <- renderUI({
actionButton("addButton", "Add points")
})
output$myPlot <- renderPlotly({
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_blank()
pP <- ggplotly(p)
pP %>% onRender("
function(el, x, data) {
//if (input$addButton selected){
var Traces = [];
var trace = {
x: data.x,
y: data.y,
mode: 'markers',
marker: {
color: 'green',
size: 6
},
hoverinfo: 'none'
};
Traces.push(trace);
Plotly.addTraces(el.id, Traces);
//}
}", data = list(x = mtcars$wt, y = mtcars$mpg))
})
})
shinyApp(ui, server)
注意:这类似于我之前提出的问题(在htmlWidgets的onRender()函数中使用Shiny actionButton()函数).但是,我简化了问题,希望确定是否有一个简单的答案.
Note: This is similar to a question I asked earlier (Using Shiny actionButton() function in onRender() function of htmlWidgets). However, I simplified the question and am hoping to determine if there is a simple answer that might be available.
推荐答案
您可以尝试直接使用一些jQuery,以响应用户单击按钮的情况. onRender
为:
You can try using some jQuery directly in order to respond to the user clicking on the button. The onRender
would be:
function(el, x, data) {
$('#addButton').on('click',function() {
var Traces = [];
var trace = {
x: data.x,
y: data.y,
mode: 'markers',
marker: {
color: 'green',
size: 6
},
hoverinfo: 'none'
};
Traces.push(trace);
Plotly.addTraces(el.id, Traces);
})
}
要使其正常工作,首先需要创建按钮,因此我将您的ui.R
更改为:
For this to work the button needs to be created first, so I changed your ui.R
to:
ui <- shinyUI(fluidPage(
actionButton("addButton", "Add points"),
plotlyOutput("myPlot", height = 700)
))
如果要保留renderUI
,则可以使用事件委托将功能按钮绑定在#add
div中:
If you want to keep the renderUI
, you can use event-delegation to bind your function buttons in the #add
div:
function(el, x, data) {
$('#add').on('click','button',function() {
var Traces = [];
var trace = {
x: data.x,
y: data.y,
mode: 'markers',
marker: {
color: 'green',
size: 6
},
hoverinfo: 'none'
};
Traces.push(trace);
Plotly.addTraces(el.id, Traces);
})
}
这篇关于htmlWidgets的onRender()函数中的闪亮的actionButton()输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!