我正在尝试构建一个有光泽的Web应用程序,我想在弹出窗口中显示R函数的结果图 窗口而不是mainPanel。 例如,对于以下示例(来自http://shiny.rstudio.com/articles/action-buttons.html),请单击" Go"按键 会在弹出窗口中显示相同的情节。
我试图添加一些javascript,但我还没有成功......有人可以帮忙吗?
提前谢谢!
library(shiny)
ui <- fluidPage(
actionButton("go", "Go"),
numericInput("n", "n", 50),
plotOutput("plot")
)
server <- function(input, output) {
randomVals <- eventReactive(input$go, {
runif(input$n)
})
output$plot <- renderPlot({
hist(randomVals())
})
}
shinyApp(ui, server)
答案 0 :(得分:10)
查看提供shinyBS
弹出窗口的modal
包。下面的示例显示了按钮单击时的图表。
编辑 - 在模态中添加了下载按钮
rm(list = ls())
library(shiny)
library(shinyBS)
shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(numericInput("n", "n", 50),actionButton("go", "Go")),
mainPanel(
bsModal("modalExample", "Your plot", "go", size = "large",plotOutput("plot"),downloadButton('downloadPlot', 'Download'))
)
)
),
server =
function(input, output, session) {
randomVals <- eventReactive(input$go, {
runif(input$n)
})
plotInput <- function(){hist(randomVals())}
output$plot <- renderPlot({
hist(randomVals())
})
output$downloadPlot <- downloadHandler(
filename = "Shinyplot.png",
content = function(file) {
png(file)
plotInput()
dev.off()
})
}
)
答案 1 :(得分:2)
您可以使用conditional panel来显示/隐藏包含您的情节的absolute panel,将条件设置为某个js变量,该变量由连接到按钮的函数切换。
e.g。
conditionalPanel("popupActive==1",
absolutePanel(id = "popup", class = "modal",
fixed = FALSE, draggable = TRUE,
top = 200, right = "auto", left = 400,
bottom = "auto",
width = 500, height = 500,
plotOutput(#output plot stuff#)
)
)
然后在js中切换popupActive的值以显示/隐藏
HTML('<head><script>popupActive = 0; function myFunction(){popupActive=!popupActive;} </script></head>'), HTML('<button id="go" type="button" class="btn btn-default action-button" onclick="myFunction()">Go</button>'),
答案 2 :(得分:2)
使用本机的Shiny功能
library(shiny)
ui <- fluidPage(
actionButton("go", "Go"),
numericInput("n", "n", 50)
)
server <- function(input, output) {
randomVals <- eventReactive(input$go, {
runif(input$n)
})
output$plot <- renderPlot({
hist(randomVals())
})
observeEvent(input$go, {
showModal(modalDialog(
plotOutput("plot"),
footer = NULL,
easyClose = TRUE
))
})
}
shinyApp(ui, server)