![](/img/trans.png)
[英]How to reactively plot using radioButton and actionButton values in Shiny R
[英]How to display default plot in www/ before actionButton is clicked R shiny
這是在單擊 actionButton 時生成繪圖的示例代碼。
shinyApp(
shinyUI(fluidPage(
inputPanel(
numericInput("n", "n", 10),
actionButton("update", "Update")
),
plotOutput("plot")
)),
shinyServer(function(input, output) {
values <- reactiveValues()
values$data <- c()
obs <- observe({
input$update
isolate({ values$data <- c(values$data, runif(as.numeric(input$n), -10, 10)) })
}, suspended=TRUE)
obs2 <- observe({
if (input$update > 0) obs$resume()
})
output$plot <- renderPlot({
dat <- values$data
hist(dat)
})
})
)
我想顯示一個在 www/test.png 中的默認圖,以在應用程序啟動時出現。 然后根據用戶輸入單擊 actionButton 后更改繪圖。
首先,我創建一個簡單的繪圖,將其導出為圖像(手動,而不是在代碼中)並將其命名為Rplot.png
(將其保存在您想要的位置):
plot(mtcars$mpg)
然后,在閃亮的應用程序中,我們必須區分兩種情況:
當應用程序啟動時,還沒有點擊按鈕,我們使用renderImage
渲染圖像
當我們點擊按鈕時,我們用renderImage
替換renderPlot
並渲染一個交互式繪圖
這意味着我們必須在ui
部分使用uiOutput
,以便我們可以根據情況選擇輸出是圖像還是繪圖。
這是一個示例(我沒有修改您的代碼,但應該不會太難):
library(shiny)
# determine your path to image here (you should use the package "here" to do so)
ui <- fluidPage(
selectInput("choice", "Choose", choices = names(mtcars)),
actionButton("run", "Run"),
uiOutput("some_ui")
)
server <- function(input, output, session) {
### "Static" part: no click on actionButton yet
output$some_ui <- renderUI({
imageOutput("image_plot")
})
output$image_plot <- renderImage({
list(src = "Rplot.png",
contentType = 'image/png')
}, deleteFile = FALSE) # Do not forget this option
### Click on actionButton
observeEvent(input$run, {
output$some_ui <- renderUI({
plotOutput("dynamic_plot")
})
output$dynamic_plot <- renderPlot({
plot(mtcars[[input$choice]])
})
})
}
shinyApp(ui, server)
關鍵是使用renderUI,因此您可以顯示圖像或R圖。 這應該做你想要的:
shinyApp(
shinyUI(fluidPage(
inputPanel(
numericInput("n", "n", 10),
actionButton("update", "Update")
),
uiOutput("out")
)),
shinyServer(function(session, input, output) {
values <- reactiveValues()
# check if plot has been already rendered
check <- reactiveVal(FALSE)
values$data <- c()
observeEvent(input$update, {
# set check to TRUE
check(TRUE)
input$update
values$data <- c(values$data, runif(as.numeric(input$n), -10, 10))
dat <- values$data
output$plot <- renderPlot({
hist(dat)
})
})
# initial picture.
output$picture <- renderImage({
list(src = "temp.png")
}, deleteFile = FALSE)
output$out <- renderUI({
# in the beginning, check is FALSE and the picture is shown
if (!check()) {
imageOutput("picture")
} else {
# as soon as the button has been pressed the first time,
# the plot is shown
plotOutput("plot")
}
})
})
)
我知道,這已經解決了一段時間,但我需要一個沒有 uiOutput 的解決方案。 另外,我發現這要簡單得多。
library(shiny)
if (interactive()) {
shinyApp(
ui = fluidPage(
actionButton("btn", "Click me"),
br(),
plotOutput('some_plot', width = '100%')
),
server = function(input, output) {
output$some_plot <- renderPlot({
if (!input$btn) {
# default plot
plot(1, 1, col = 'red')
} else{
# updated plot
plot(1000, 1000, col = 'green')
}
})
}
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.