【发布时间】:2020-11-16 07:15:41
【问题描述】:
我对 Shiny 很陌生,我正在尝试制作一个 Shiny 应用程序,它允许用户通过一个简单的下载按钮下载特定数据的所有可能组合。
但是,闪亮似乎不允许我在闪亮之外使用的标准for loop
所以基本上我想用这些可重现的数据做的是允许用户下载所有 9 个可能的散点图(1,1),(1,2),(1,3) 等
我尝试过使用lapply, isolate, local 等等,但很简单,要么制作 3 个相同的图,要么不像当前代码那样工作
我们将非常非常感谢您的帮助。
提前谢谢你
这是我当前的用户界面
#UI
library(shiny)
library(datasets)
library(ggplot2)
Test1<- c(3,5,7)
Test2<- c(21000, 23400, 26800)
Test3<- c(600, 700, 800)
df <- data.frame(Test1, Test2, Test3)
rownames(df)<-colnames(df)
*ui <- shinyUI(fluidPage(
titlePanel("Column Plot"),
tabsetPanel(
tabPanel("First Type",
pageWithSidebar(
headerPanel('My First Plot'),
sidebarPanel(
selectInput('xcol', 'X Variable', ""),
selectInput('ycol', 'Y Variable', "", selected = ""),
actionButton(inputId = "clicks_3",label = "Real"),
),
mainPanel(
plotOutput('MyPlot')
)
)
)
)
)
)
这是我的服务器
server <- shinyServer(function(input, output, session) {
data <- reactive({
updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
choices = names(df), selected = names(df)[2])
return(df)
})
output$contents <- renderTable({
data()
})
output$MyPlot <- renderPlot({
x <- data()[, c(input$xcol, input$ycol)]
plot(x)
})
observeEvent(input$clicks_3,{
for (i in 1:3){
assign(paste("Plotinput_",i,sep = ""),({
x <- data()[, c(colnames(data())[i], input$ycol)]
plot(x)
}))
lapply(1:3, function(k){local({
ggsave(plot = eval(parse(text=paste("Plotinput_",k,sep = ""))),filename = paste(k,"barplot.png",collapse = ""))
})
})
}
})
})
【问题讨论】:
标签: r loops for-loop ggplot2 shiny