gpt4 book ai didi

r - 使用 Shiny 的动态向网页添加图

转载 作者:行者123 更新时间:2023-12-03 10:01:45 24 4
gpt4 key购买 nike

我想使用 Shiny 创建一个应用程序,动态地向页面添加绘图。它可能是 10 个地块,也可能只有一个。我正在使用 this tutorial在动态 UI 的 Shiny 主页中。

这是一个简化的例子。
函数showme正在绘制图形

服务器文件

shinyServer(function(input, output) {
# Create an environment for storing data
symbol_env <- new.env()
# Make a chart for a symbol, with the settings from the inputs
make_chart <- function(symbol) {
showme(symbol)
}

display <- c("1083484" , "1101732")

output$MyList <- renderUi({
for (i in i:nrow(display))
renderPlot({make_chart(display[i])})
})
})

用户界面
shinyUI(pageWithSidebar(
headerPanel("My Plots !"),
sidebarPanel(
wellPanel(
p(strong("Scan1"))))
,mainPanel(
uiOutput("MyList")
)))

我收到以下错误
Listening on port 8100
Error in .subset2(x, "impl")$defineOutput(name, value, deparse(substitute(value))) :
Unexpected character output for display

如果这不是方法 - 我将不胜感激任何指导。
谢谢。
> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: i386-w64-mingw32/i386 (32-bit)

最佳答案

也许这个由 Winston Chang 提供的例子很有帮助:

Shiny example app with dynamic number of plots

这是一个完整的例子,以防链接腐烂:

server.R

max_plots <- 5

shinyServer(function(input, output) {

# Insert the right number of plot output objects into the web page
output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})

# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, plot_output_list)
})

# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max_plots) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")

output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i, xlim = c(1, max_plots), ylim = c(1, max_plots), main = paste("1:", my_i, ". n is ", input$n, sep = ""))
})
})
}
})

ui.R
shinyUI(pageWithSidebar(

headerPanel("Dynamic number of plots"),

sidebarPanel(
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),

mainPanel(
uiOutput("plots") # This is the dynamic UI for the plots
)
))

关于r - 使用 Shiny 的动态向网页添加图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com