我在用R语言写一张在线表格,但有些东西让我感到困惑。
我需要的是:
如果您在输入块中键入数字并提交,只需显示表automatically.的前n行即可。
我看到的是:
DFflt (表后面的变量)在提交后已经更改,但是在手动刷新页面之前不能重新加载网页上的表。
我能做些什么来解决这个问题?谢谢!
library(shiny)
library(tidyverse)
DF <- mpg
DFflt <- mpg
ui <- fluidPage(
numericInput("nrow", "Slice the top n rows of DF", value = 0),
actionButton("submit", "Submit"),
actionButton("p_DFflt", "print DFflt in console"),
div('render DFflt:'),
tableOutput("table")
)
server <- function(input, output, session) {
observeEvent(input$submit, {
nrow <- input$nrow
if (nrow > 10) {
showNotification(str_c("Invalid", type="error")) }
else { DFflt <<- DF %>% head(nrow) }
})
observeEvent(input$p_DFflt, { print(DFflt)} )
output$table <- renderTable({DFflt})
}
shinyApp(ui, server)发布于 2021-11-24 15:20:47
这应该如您所预期的那样起作用:
library(shiny)
library(tidyverse)
DF <- mpg
ui <- fluidPage(
numericInput("nrow", "Slice the top n rows of DF", value = 0),
actionButton("submit", "Submit"),
actionButton("p_DFflt", "print DFflt in console"),
div('render DFflt:'),
tableOutput("table")
)
server <- function(input, output, session) {
n_row <- eventReactive(input$submit, {
input$nrow
})
observe({
if (n_row() > 10) {
showNotification(str_c("Invalid ", type="error"))
}
})
output$table <- renderTable({
if(input$submit == 0){ # when the submit button is not clicked
DF
}else{
if (n_row() >= 0 & n_row() <= 10) {
DF %>% head(n_row())
}
}
})
observeEvent(input$p_DFflt, { print(DF)} )
}
shinyApp(ui, server)https://stackoverflow.com/questions/70097619
复制相似问题