【问题标题】:Using variable number of input fields in shiny page在闪亮页面中使用可变数量的输入字段
【发布时间】:2015-06-29 17:14:34
【问题描述】:

我的问题如下:

我有一个闪亮的应用程序,它根据用户输入显示可变数量的输出元素(例如,详细信息见:dynamically add plots to web page using shiny)。

但是,我还想为每个输出元素添加一个输入元素,允许用户为输出指定一些修饰符(例如,让用户在将每个元素查看为绘图或表格之间进行选择,但是一个可以概括为对可变数量的输出进行任何类型的修改)。

我认为最简单的方法是向每个元素添加一个 selectInput 元素,其中包含我希望用户拥有的选项。 我的问题是,每次呈现页面时, selectInput 元素似乎都会恢复到它们的初始值,因此更改它们没有任何效果(实际上,它们确实有效果,但随后它们被重置并且效果是已恢复)。

以下代码重现了问题(根据@skasch 对上述问题的回答进行了修改):

server.R

library(shiny)
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) {
     modifier <- paste("select",i,sep="")
     plotname <- paste("plot", i, sep="")
     plottitle <- paste("plottitle", i, sep="")
     tablename <- paste("tablename", i, sep="")

     # By default I display a plot
     disp <- plotOutput(plotname, height = 280, width = 250)  
     # modifier may not be in the input if this is the first time the element is displayed
     if(modifier %in% names(input) && input[[modifier]] == "table") {
       disp = tableOutput(tablename)
     }
     # Make the output element display properly:
     tagList(
       textOutput(plottitle, container = h3),
       uiOutput(modifier),
       disp)
   })    
   # 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
        modifier <- paste("select",i,sep="")
        plotname <- paste("plot", my_i, sep="")
        plottitle <- paste("plottitle", my_i, sep="")
        tablename <- paste("tablename", 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 = ""))
        })
        output[[plottitle]] <- renderText({paste("1:", my_i, ".  n is ", input$n, sep = "")
        })
        output[[tablename]] <- renderTable({table(x = 1:my_i, y = 1:my_i)
        })
        # I suspect the problem is the re-evaluation of this part that resets the "selected" field
        output[[modifier]] <- renderUI({selectInput(
                                          inputId = modifier,
                                          label="select how to display",
                                          choices = c("plot","table"),
                                          selected = 1)})
    })
}
})

ui.r

library(shiny)
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 rstudio


    【解决方案1】:

    虽然我认为这不是一个理想的解决方案,但分配:

    output[[modifier]] <- renderUI({selectInput(
                                          inputId = modifier,
                                          label="select how to display",
                                          choices = c("plot","table"),
                                          selected = input[[modifier]])})
    

    解决了这个问题,虽然我很乐意得到一个更好的解决方案(特别是避免每次都重新构建输入对象的解决方案)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      相关资源
      最近更新 更多