我在 renderPlot 中運行的閃亮應用程式中有五個 R 腳本。每個腳本中都包含一系列獨特的突變以及 ggplot 代碼,以生成具有特定分位數值的箱線圖。每個都是接受我的 input$variable 的獨特函式。我想在 Shiny 中獲取腳本,并根據用戶滑塊選擇的分位數值回圈遍歷繪圖。我沒有任何問題單獨生成每個情節,問題是一起運行它們。“名稱”的每個變數在每個函式中都有唯一的值。所以理想情況下,我想選擇一個變數并使用滑塊查看它的每個圖。是否有捷徑可尋?我試圖將滑塊輸入與每個特定的情節聯系起來,但沒有任何運氣。
source("s_75.R")
source("s_80.R")
source("s_85.R")
source("s_90.R")
source("s_95.R")
# Define UI for pair interaction app ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Set"),
# Sidebar panel for inputs ----
sidebarPanel(# Input: Selector for variable to plot against ----
selectInput("variable", "Name:", qc$S),
sliderInput("quantile", "Quantile Range:",
min = 75, max = 95, value = c(85), step = 5)),
# Main panel for displaying outputs ----
mainPanel(
h1("Header 1"),
plotOutput("plot", width = "100%")
))
# Define server logic to plot various variables against
server <- function(input, output, session) {
output$plot <- renderPlot(s_75(input$variable),
height = 600, width = 800)
output$plot2 <- renderPlot(s_80(input$variable),
height = 600, width = 800)
output$plot3 <- renderPlot(s_85(input$variable),
height = 600, width = 800)
output$plot4 <- renderPlot(s_90(input$variable),
height = 600, width = 800)
output$plot5 <- renderPlot(s_95(input$variable),
height = 600, width = 800)
shinyApp(ui, server)
uj5u.com熱心網友回復:
有幾種方法可以做到這一點。我認為我能想到的最簡潔的方法是使用get()命令。這將讓您在作為字串 ( input$variable)呼叫時在您的環境中找到一個物件。唯一的新專案是頂部的代表和服務器主體。如果我誤解了提示,請告訴我,我會嘗試更新我的答案。
library(shiny)
library(tidyverse)
# reprex ----
qc <-
mtcars |>
pivot_longer(everything()) |>
print()
s_75 <- function(var) ggplot(mtcars, aes(get(var))) geom_bar()
s_80 <- function(var) ggplot(mtcars, aes(get(var))) geom_dotplot()
s_85 <- function(var) ggplot(mtcars, aes(get(var))) geom_histogram()
s_90 <- function(var) ggplot(mtcars, aes(get(var), 1)) geom_count()
s_95 <- function(var) ggplot(mtcars, aes(get(var))) geom_density()
# ui ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Set"),
# Sidebar panel for inputs ----
sidebarPanel( # Input: Selector for variable to plot against ----
selectInput("variable", "Name:", unique(qc$name)),
sliderInput("quantile", "Quantile Range:",
min = 75, max = 95, value = c(85), step = 5
)
),
# Main panel for displaying outputs ----
mainPanel(
h1("Header 1"),
plotOutput("plot", width = "100%")
)
)
# server ----
# Define server logic to plot various variables against
server <- function(input, output, session) {
fn <- reactive(get(paste0("s_", input$quantile)))
output$plot <- renderPlot(fn()(input$variable), height = 600, width = 800)
# ^^^ note that the reactive value goes fn()(var)
}
shinyApp(ui, server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420119.html
標籤:
上一篇:用r中特定列欄位的插值替換NA
