外面的人真棒!在 Shiny 上需要快速幫助eventReactive()。
我有兩個資料框(df1、df2),它們是我 Shiny 應用程式中先前計算的輸出。我需要在我的 Shiny 應用程式中對這兩個資料幀運行一個 for 回圈eventReactive()。當我actionButton("simulate")從我的用戶界面單擊/觸發時。我需要運行下面的 for 回圈并在 Shiny 應用程式中創建一個資料表。我遇到的問題是我沒有看到 UI 上發生任何事情,UI 是空白的(雖然我希望看到一個資料表)并且沒有要除錯的錯誤訊息。
該for回圈在控制臺中運行良好,但在 Shiny 應用程式中運行良好。我試過lapply了,但效果不佳。這也是一個沒有任何輸出的空螢屏。
任何幫助深表感謝!
控制臺中的for回圈,運行良好
library(tidyverse)
df1 <- data.frame(Year = rep(c(2018:2020), each = 12),
Treatment = rep(c("AA", "BB", "CC"), each = 4, times = 3),
Source = rep(c("Ran", "Ban", "Dam", "Sam"), times = 9),
Value = sample(36))
df2 <- data.frame(Year = rep(c(2018:2020), each = 3),
Treatment = rep(c("AA", "BB", "CC"), times = 3),
Value2 = sample(9))
tnames <- unique(df1$Treatment)
simdata <- data.frame()
for(i in 1:length(tnames)){
vcmp <- df1 %>% filter(Treatment == tnames[i]) %>%
group_by(Source) %>%
summarise(median = median(Value)) %>%
mutate(Trait = tnames[i])
preSD <- df2 %>% filter(Treatment == tnames[i]) %>%
summarize(Value2 = median(Value2))
simuS <- tibble(nLo = rep(5:15, times = 3),
nRe = rep(1:3, each = 11),
nPlo = nLo * nRe,
Vg = vcmp[[3,2]],
Ve = vcmp[[4,2]],
ycept = preSD[[1,1]],
sm = sqrt(Vg / nLo Ve / nPlo),
sd = sqrt(2 * (Vg / nLo Ve / nPlo)),
ld = 2 * sd,
Rp = factor(nRe),
Treatment = tnames[i])
simdata <- rbind(simdata, simuS)
}
MRE,Shiny implementation,我解決了一半,我現在可以看到輸出,但它只生成一次迭代。不是整個 for 回圈
library(shiny)
library(tidyverse)
library(DT)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 2,
selectInput("Parameter", "Select Parameter","", ""),
actionButton("generate", "Generate Data"),
hr(),
hr(),
actionButton("simulate", "Simulate Data")),
mainPanel(width = 7,
DT::dataTableOutput("table1"),
DT::dataTableOutput("table2"),
DT::dataTableOutput("table3"))
)
)
server <- function(input, output, session){
table1 <- eventReactive(input$generate,{
df1 <- data.frame(Year = rep(c(2018:2020), each = 12),
Treatment = rep(c("AA", "BB", "CC"), each = 4, times = 3),
Source = rep(c("Ran", "Ban", "Dam", "Sam"), times = 9),
Value = sample(36))
})
table2 <- eventReactive(input$generate, {
df2 <- data.frame(Year = rep(c(2018:2020), each = 3),
Treatment = rep(c("AA", "BB", "CC"), times = 3),
Value2 = sample(9))
})
simudata <- eventReactive(input$simulate,{
tnames <- unique(table1()$Treatment)
simudata <- tibble()
for(i in 1:length(tnames)){
vcmp <- table1() %>%
filter(Treatment == tnames[i]) %>%
group_by(Source) %>%
summarise(median = median(Value)) %>%
mutate(Trait = tnames[i])
preSD <- table2() %>%
filter(Treatment == tnames[i]) %>%
summarize(Value2 = median(Value2))
new_Row <- tibble(nLo = rep(5:15, times = 3),
nRe = rep(1:3, each = 11),
nPlo = nLo * nRe,
Vg = vcmp[[3,2]],
Ve = vcmp[[4,2]],
ycept = preSD[[1,1]],
sm = sqrt(Vg / nLo Ve / nPlo),
sd = sqrt(2 * (Vg / nLo Ve / nPlo)),
ld = 2 * sd,
Rp = factor(nRe),
Treatment = tnames[i])
simudata <- rbind(simudata, new_Row)
return(simudata)
}
})
output$table1 <- DT::renderDataTable(table1(), options = list(paging = t, pageLength = 5))
output$table2 <- DT::renderDataTable(table2(), options = list(paging = t, pageLength = 5))
output$table3 <- DT::renderDataTable(simudata(), options = list(paging = t, pageLength = 5))
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
您可以使用local()如下所示的方法解決您的問題。
simudata <- eventReactive(input$simulate,{
tnames <- unique(table1()$Treatment)
simudata <- tibble()
for(i in 1:length(tnames)){
local({
i <- i
vcmp <- table1() %>%
dplyr::filter(Treatment == tnames[i]) %>%
group_by(Source) %>%
dplyr::summarise(median = median(Value)) %>%
dplyr::mutate(Trait = tnames[i])
preSD <- table2() %>%
dplyr::filter(Treatment == tnames[i]) %>%
dplyr::summarize(Value2 = median(Value2))
new_Row <- tibble(nLo = rep(5:15, times = 3),
nRe = rep(1:3, each = 11),
nPlo = nLo * nRe,
Vg = vcmp[[3,2]],
Ve = vcmp[[4,2]],
ycept = preSD[[1,1]],
sm = sqrt(Vg / nLo Ve / nPlo),
sd = sqrt(2 * (Vg / nLo Ve / nPlo)),
ld = 2 * sd,
Rp = factor(nRe),
Treatment = tnames[i])
simudata <<- rbind(simudata, new_Row)
})
}
return(simudata)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488474.html
