2017-06-08 96 views
1

我正在構建一個由多個重複輸入和輸出填充的Shiny應用程序。我不是一遍又一遍地複製和粘貼代碼,而是按照this example on the Shiny website的順序生成了輸入和輸出,其輸入爲lapply()。這很好地工作到某一點,但我想根據用戶輸入進行預測,然後將這些預測存儲爲反應對象,以便通過多個輸出(例如繪圖,打印和組合預測)進行調用。在這裏,lapply()函數中的被動對象的賦值似乎有問題。我有一種感覺,assign()函數不喜歡reactive()對象!R Shiny將反應值分配給粘貼的對象

我已經使用mtcars編寫了一個我的問題的簡單工作示例。此代碼工作正常,但我想刪除明確的作業pred1pred2並替換爲lapply()函數。我知道在這個簡單的例子中,在output$out對象內部進行預測會更容易,但在我的實際應用程序中,我需要將預測對象調用到多個輸出中。任何幫助將不勝感激。

library(shiny) 
ui = fluidPage(
     # slider input for horse power to predict with... 
     column(3,lapply(1:2, function(i) { 
     sliderInput(paste0('hp', i), paste0('Select HP', i), min = 0, max = 300, value = 50) 
     }) 
    ), 

     # output display 
     column(3,lapply(1:2, function(i) { 
     uiOutput(paste0('out', i)) 
     }) 
    ) 

) 

server = function(input, output, session) { 

# # I can work pred out separately like this... 
# pred1 <- reactive({ 
#  predict(lm(mpg ~ hp, data = mtcars), 
#    newdata = data.frame(hp = input$hp1), se.fit = TRUE) 
# 
# }) 
# 
# pred2 <- reactive({ 
# predict(lm(mpg ~ hp, data = mtcars), 
#   newdata = data.frame(hp = input$hp2), se.fit = TRUE) 
# 
#}) 

    # but I want to create pred1 and pred2 in one go...something like this: 
    lapply(1:2, function(i){ 
    assign(paste0("pred",i), reactive({ 
     predict(lm(mpg ~ hp, data = mtcars), 
       newdata = data.frame(hp = input[[paste0("hp",i)]]), se.fit = TRUE) 
     })) 
    }) 

    # output 
    lapply(1:2, function(i){ 
    output[[paste0("out",i)]] <- renderText({ 
     paste0("MPG with HP",i," = ", round(get(paste0("pred",i))()$fit,0), " (", 
        round(get(paste0("pred",i))()$fit - 1.96 * get(paste0("pred",i))()$se.fit,0), ", ", 
        round(get(paste0("pred",i))()$fit + 1.96 * get(paste0("pred",i))()$se.fit,0), ")") 

     }) 
     }) 

} 


# Compile 
shinyApp(
    ui = ui, 
    server = server 
) 
+1

我認爲最好的辦法是讓一個'名單()'內'reactiveValues()',並將它們分配到列表中,你會在外面閃閃發光,... – BigDataScientist

+0

你不會提及任何錯誤或不良結果。 – Parfait

+0

謝謝,我編輯了一個錯誤,並將工作代碼註釋掉了。這篇文章是關於效率,而不是一個錯誤修復,抱歉的混淆。 – andsim

回答

0

這裏是使用的解決方案reactiveValues():

library(shiny) 
ui = fluidPage(
    # slider input for horse power to predict with... 
    column(3,lapply(1:2, function(i) { 
    sliderInput(paste0('hp', i), paste0('Select HP', i), min = 0, max = 300, value = 50) 
    }) 
), 

    #output display 
    column(3,lapply(1:2, function(i) { 
    uiOutput(paste0('out', i)) 
    }) 
    ) 
) 

server = function(input, output, session) { 
    predReactive <- reactiveValues() 

    outOjbects <- paste("pred", paste(1:2), sep = "") 

    lapply(1:2, function(i){ 
    predReactive[[outOjbects[i]]] <- reactive({ 
     predict(lm(mpg ~ hp, data = mtcars), 
       newdata = data.frame(hp = input[[paste0("hp",i)]]), se.fit = TRUE) 
    }) 
    }) 

    # output 
    lapply(1:2, function(i){ 
    output[[paste0("out",i)]] <- renderText({ 
     paste0("MPG with HP",i," = ", round(predReactive[[outOjbects[i]]]()$fit,0), " (", 
       round(predReactive[[outOjbects[i]]]()$fit - 1.96 * predReactive[[outOjbects[i]]]()$se.fit,0), ", ", 
       round(predReactive[[outOjbects[i]]]()$fit + 1.96 * predReactive[[outOjbects[i]]]()$se.fit,0), ")") 

    }) 
    }) 

} 


shinyApp(ui = ui, server = server)