2015-04-22 26 views
3

我的問題如下:使用在光澤頁可變數量輸入字段

我有一個發亮應用程序,顯示可變數目基於用戶輸入輸出元件(如,例如,在詳述: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 
    ) 
)) 

回答

1

雖然我不覺得它是一個理想的解決方案,分配:

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

解決了這個問題,但我很樂意得到一個更好的解決方案(特別是一個,每一次重新避免輸入對象的構造)。

相關問題