如何改变闪亮的任意输入变量
How to change arbitrary input variable in shiny
我正在尝试在 shiny renderplot 模块中使用的绘图(通常是 ggplot2 和 base R plot)中实现放大功能。这是代码。
ui <- basicPage(
plotOutput("plot1",
brush = "plot_brush"
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = c(input$plot_brush$xmin, input$plot_brush$xmax), ylim=c(input$plot_brush$ymin, input$plot_brush$ymax))
})
}
shinyApp(ui, server)
这里我想在应用缩放后将输入设置为:- input$plot_brush
到 NULL
。我知道这需要在代码的 renderPlot
块中进行修改,但除此之外我如何将 input$plot_brush
设置为 NULL
[这将使选择从图中消失]。我知道更新输入函数是为了更新 UI [比如 shiny::updateSelectInput()
等],但不能在这里用于此目的。
目前解决这个问题的唯一方法是拥有一个基本图和一个缩放图,其中在选择基本图上的区域后将应用缩放 [如 https://gallery.shinyapps.io/105-plot-interaction-zoom/] 中实现的那样。
请帮我解决这个问题。
如果我理解你正确地将画笔设置为 NULL 只是为了删除蓝色画笔选择?
如果是这样你可以这样做:
ui <- basicPage(
plotOutput("plot1",
brush = brushOpts("plot_brush",resetOnNew=T),
dblclick = 'dblclick'
)
)
#
# Zoom in on plot brush, restore zoom on dbl click
#
server <- function(input, output) {
ranges <- reactiveValues(x=NULL, y=NULL)
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = ranges$x, ylim=ranges$y)
})
# Set limits for zoom
observe({
brush <- input$plot_brush
if (!is.null(brush)) { ranges$x <- c(brush$xmin, brush$xmax); ranges$y <- c(brush$ymin, brush$ymax); }
})
# Zoom out on doubleclick
observeEvent(input$dblclick,{
ranges$x <- NULL; ranges$y <- NULL;
})
}
#
# Zoom in on dbl click, restore zoom on dbl click
#
server2 <- function(input, output) {
ranges <- reactiveValues(x=NULL, y=NULL)
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = ranges$x, ylim=ranges$y)
})
# Set ranges for plot zoom
observeEvent(input$dblclick,{
brush <- input$plot_brush
if (!is.null(brush)) { ranges$x <- c(brush$xmin, brush$xmax); ranges$y <- c(brush$ymin, brush$ymax); }
else{ ranges$x <- NULL; ranges$y <- NULL; }
})
}
shinyApp(ui, server)
这里我在缩放后使用 reset on new 删除画笔选择。
Server
展示了如何通过简单的刷牙(双击缩小)和
来放大
Server2
展示了如何通过双击刷过的区域来放大,双击没有刷过的区域来缩小。
希望对您有所帮助!
我正在尝试在 shiny renderplot 模块中使用的绘图(通常是 ggplot2 和 base R plot)中实现放大功能。这是代码。
ui <- basicPage(
plotOutput("plot1",
brush = "plot_brush"
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = c(input$plot_brush$xmin, input$plot_brush$xmax), ylim=c(input$plot_brush$ymin, input$plot_brush$ymax))
})
}
shinyApp(ui, server)
这里我想在应用缩放后将输入设置为:- input$plot_brush
到 NULL
。我知道这需要在代码的 renderPlot
块中进行修改,但除此之外我如何将 input$plot_brush
设置为 NULL
[这将使选择从图中消失]。我知道更新输入函数是为了更新 UI [比如 shiny::updateSelectInput()
等],但不能在这里用于此目的。
目前解决这个问题的唯一方法是拥有一个基本图和一个缩放图,其中在选择基本图上的区域后将应用缩放 [如 https://gallery.shinyapps.io/105-plot-interaction-zoom/] 中实现的那样。
请帮我解决这个问题。
如果我理解你正确地将画笔设置为 NULL 只是为了删除蓝色画笔选择?
如果是这样你可以这样做:
ui <- basicPage(
plotOutput("plot1",
brush = brushOpts("plot_brush",resetOnNew=T),
dblclick = 'dblclick'
)
)
#
# Zoom in on plot brush, restore zoom on dbl click
#
server <- function(input, output) {
ranges <- reactiveValues(x=NULL, y=NULL)
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = ranges$x, ylim=ranges$y)
})
# Set limits for zoom
observe({
brush <- input$plot_brush
if (!is.null(brush)) { ranges$x <- c(brush$xmin, brush$xmax); ranges$y <- c(brush$ymin, brush$ymax); }
})
# Zoom out on doubleclick
observeEvent(input$dblclick,{
ranges$x <- NULL; ranges$y <- NULL;
})
}
#
# Zoom in on dbl click, restore zoom on dbl click
#
server2 <- function(input, output) {
ranges <- reactiveValues(x=NULL, y=NULL)
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = ranges$x, ylim=ranges$y)
})
# Set ranges for plot zoom
observeEvent(input$dblclick,{
brush <- input$plot_brush
if (!is.null(brush)) { ranges$x <- c(brush$xmin, brush$xmax); ranges$y <- c(brush$ymin, brush$ymax); }
else{ ranges$x <- NULL; ranges$y <- NULL; }
})
}
shinyApp(ui, server)
这里我在缩放后使用 reset on new 删除画笔选择。
Server
展示了如何通过简单的刷牙(双击缩小)和
Server2
展示了如何通过双击刷过的区域来放大,双击没有刷过的区域来缩小。
希望对您有所帮助!