Shiny 允许用户选择要显示的绘图输出
Shiny allow users to choose which plot outputs to display
我有一个闪亮的应用程序,我的服务器功能如下所示:
shinyServer(function(input, output, session) {
filedata <- reactive({
infile <- input$file1
if (is.null(infile)) {
return(NULL)
}
myDF <- fread(infile$datapath)
return(myDF)
# Return the requested graph
graphInput <- reactive({
switch(input$graph,
"Plot1" = plot1,
"Plot2" = plot2)
})
output$selected_graph <- renderPlot({
paste(input$graph)
})
output$plot1 <- renderPlot({
#fill in code to create a plot1
})
output$plot2 <- renderPlot({
#fill in code to create plot2
})
UI 函数如下所示:
shinyUI(pageWithSidebar(
headerPanel("CSV Viewer"),
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
selectInput("graph", "Choose a graph to view:",
choices = c("Plot1", "Plot2"))
submitButton("Update View")
),#end of sidebar panel
mainPanel(
tabsetPanel(
tabPanel("Graph Viewer", plotOutput("selected_graph"))
)
我无法在屏幕上显示所选的情节。当我从下拉菜单中进行选择并单击 "Update View" 按钮时,应用程序不显示绘图。它不显示错误消息。它什么都不显示。
我该如何解决这个问题?
如评论中所述,鉴于您问题中的示例不完整,很难确保任何答案都有效。然而,基于提供的框架服务器,这种选择图形的模式应该有效:
shinyServer(function(input, output, session) {
filedata <- reactive({
# Haven't tested that this will read in data correctly;
# assuming it does
infile <- input$file1
if (is.null(infile)) {
return(NULL)
}
myDF <- fread(infile$datapath)
return(myDF)
})
plot1 <- reactive({
# this should be a complete plot image,
# e.g. ggplot(data, aes(x=x, y=y)) + geom_line()
})
plot2 <- reactive({
# this should be a complete plot image,
# e.g. ggplot(data, aes(x=x, y=y)) + geom_line()
})
# Return the requested graph
graphInput <- reactive({
switch(input$graph,
"Plot1" = plot1(),
"Plot2" = plot2()
)
})
output$selected_graph <- renderPlot({
graphInput()
})
}
发生了什么变化:
plot1
和 plot2
是可以从 graphInput
反应函数 返回的 reactive
函数(而不是输出)
graphInput
returns 将 plot1
或 plot2
的值(即绘图)转换为 output$selected_graph
我有一个闪亮的应用程序,我的服务器功能如下所示:
shinyServer(function(input, output, session) {
filedata <- reactive({
infile <- input$file1
if (is.null(infile)) {
return(NULL)
}
myDF <- fread(infile$datapath)
return(myDF)
# Return the requested graph
graphInput <- reactive({
switch(input$graph,
"Plot1" = plot1,
"Plot2" = plot2)
})
output$selected_graph <- renderPlot({
paste(input$graph)
})
output$plot1 <- renderPlot({
#fill in code to create a plot1
})
output$plot2 <- renderPlot({
#fill in code to create plot2
})
UI 函数如下所示:
shinyUI(pageWithSidebar(
headerPanel("CSV Viewer"),
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
selectInput("graph", "Choose a graph to view:",
choices = c("Plot1", "Plot2"))
submitButton("Update View")
),#end of sidebar panel
mainPanel(
tabsetPanel(
tabPanel("Graph Viewer", plotOutput("selected_graph"))
)
我无法在屏幕上显示所选的情节。当我从下拉菜单中进行选择并单击 "Update View" 按钮时,应用程序不显示绘图。它不显示错误消息。它什么都不显示。
我该如何解决这个问题?
如评论中所述,鉴于您问题中的示例不完整,很难确保任何答案都有效。然而,基于提供的框架服务器,这种选择图形的模式应该有效:
shinyServer(function(input, output, session) {
filedata <- reactive({
# Haven't tested that this will read in data correctly;
# assuming it does
infile <- input$file1
if (is.null(infile)) {
return(NULL)
}
myDF <- fread(infile$datapath)
return(myDF)
})
plot1 <- reactive({
# this should be a complete plot image,
# e.g. ggplot(data, aes(x=x, y=y)) + geom_line()
})
plot2 <- reactive({
# this should be a complete plot image,
# e.g. ggplot(data, aes(x=x, y=y)) + geom_line()
})
# Return the requested graph
graphInput <- reactive({
switch(input$graph,
"Plot1" = plot1(),
"Plot2" = plot2()
)
})
output$selected_graph <- renderPlot({
graphInput()
})
}
发生了什么变化:
plot1
和plot2
是可以从graphInput
反应函数 返回的 graphInput
returns 将plot1
或plot2
的值(即绘图)转换为output$selected_graph
reactive
函数(而不是输出)