在 R Shiny 中更新 table
Update table in R Shiny
我刚开始学习 R。我有一个小项目,其中显示时间table,用户可以输入主题。
我的问题:我不知道如何在时间table(数据框)中输入主题(例如“数学”)。用户按下操作按钮后,应在 table 中的 ["1", "monday"] 位置输入主题。
我在这里试过:
output$my_table <- renderDataTable(df())
df <- eventReactive(input$button, {
timetable["1", "monday"] <- input$select1
})
不幸的是,这不起作用。非常感谢任何有关如何将内容输入 table 的提示和建议!
这是我的代码:
library(shiny)
ui <- fluidPage(
theme = bs_theme(version = 4, bootswatch = "minty"),
titlePanel(h1("My timetable", align = "center" )),
sidebarLayout(
position = c("left"),
sidebarPanel(
width = 4,
selectInput("select1", label = h5("Event:"),
choices = c("math" , "sience", "sport") ,
selected = 1,
width = 400),
actionButton("action", label = "Add")),
mainPanel(
width = 8,
tableOutput('my_table')),
),
)
和服务器:
server <- function(input, output, session) {
output$my_table = renderTable({timetable<- data.frame(monday <- c("","","","",""),
tuesday <- c("","","","",""),
wednesday <- c("","","","",""),
thursday <- c("","","","",""),
friday <- c("","","","",""))},
bordered = TRUE,
spacing = c('l'),
width = "100%",
striped = TRUE,
align = 'c',
rownames = TRUE)
output$timetable <- renderDataTable(df())
df <- eventReactive(input$action, { timetable["1","monday"] <- input$select1 })
}
shinyApp(ui, server)
这是一个完整的工作示例,可能会有帮助。
首先,我可能会定义一个单独的 reactiveVal
来存储您的 data.frame。这将在您的 table 输出以及 observeEvent
或 eventReactive
方法中访问。
当您引用 reactiveVal
时,请在末尾使用带括号的 timetable()
。当你想替换存储在timetable
中的data.frame时,你可以做timetable(new_data_frame_here)
。在 observeEvent
中,我创建了一个临时的 tmp
data.frame 可以用来进一步编辑方便。
library(shiny)
library(bslib)
ui <- fluidPage(
theme = bs_theme(version = 4, bootswatch = "minty"),
titlePanel(h1("My timetable", align = "center" )),
sidebarLayout(
position = c("left"),
sidebarPanel(
width = 4,
selectInput("select1", label = h5("Event:"),
choices = c("math" , "sience", "sport") ,
selected = 1,
width = 400),
actionButton("action", label = "Add")),
mainPanel(
width = 8,
tableOutput('my_table')
)
)
)
server <- function(input, output, session) {
timetable <- reactiveVal(
data.frame(monday = c("","","","",""),
tuesday = c("","","","",""),
wednesday = c("","","","",""),
thursday = c("","","","",""),
friday = c("","","","",""))
)
output$my_table = renderTable(timetable(),
bordered = TRUE,
spacing = c('l'),
width = "100%",
striped = TRUE,
align = 'c',
rownames = TRUE)
observeEvent(input$action, {
tmp <- timetable()
tmp[1, "monday"] <- input$select1
timetable(tmp)
})
}
shinyApp(ui, server)
我刚开始学习 R。我有一个小项目,其中显示时间table,用户可以输入主题。
我的问题:我不知道如何在时间table(数据框)中输入主题(例如“数学”)。用户按下操作按钮后,应在 table 中的 ["1", "monday"] 位置输入主题。
我在这里试过:
output$my_table <- renderDataTable(df())
df <- eventReactive(input$button, {
timetable["1", "monday"] <- input$select1
})
不幸的是,这不起作用。非常感谢任何有关如何将内容输入 table 的提示和建议!
这是我的代码:
library(shiny)
ui <- fluidPage(
theme = bs_theme(version = 4, bootswatch = "minty"),
titlePanel(h1("My timetable", align = "center" )),
sidebarLayout(
position = c("left"),
sidebarPanel(
width = 4,
selectInput("select1", label = h5("Event:"),
choices = c("math" , "sience", "sport") ,
selected = 1,
width = 400),
actionButton("action", label = "Add")),
mainPanel(
width = 8,
tableOutput('my_table')),
),
)
和服务器:
server <- function(input, output, session) {
output$my_table = renderTable({timetable<- data.frame(monday <- c("","","","",""),
tuesday <- c("","","","",""),
wednesday <- c("","","","",""),
thursday <- c("","","","",""),
friday <- c("","","","",""))},
bordered = TRUE,
spacing = c('l'),
width = "100%",
striped = TRUE,
align = 'c',
rownames = TRUE)
output$timetable <- renderDataTable(df())
df <- eventReactive(input$action, { timetable["1","monday"] <- input$select1 })
}
shinyApp(ui, server)
这是一个完整的工作示例,可能会有帮助。
首先,我可能会定义一个单独的 reactiveVal
来存储您的 data.frame。这将在您的 table 输出以及 observeEvent
或 eventReactive
方法中访问。
当您引用 reactiveVal
时,请在末尾使用带括号的 timetable()
。当你想替换存储在timetable
中的data.frame时,你可以做timetable(new_data_frame_here)
。在 observeEvent
中,我创建了一个临时的 tmp
data.frame 可以用来进一步编辑方便。
library(shiny)
library(bslib)
ui <- fluidPage(
theme = bs_theme(version = 4, bootswatch = "minty"),
titlePanel(h1("My timetable", align = "center" )),
sidebarLayout(
position = c("left"),
sidebarPanel(
width = 4,
selectInput("select1", label = h5("Event:"),
choices = c("math" , "sience", "sport") ,
selected = 1,
width = 400),
actionButton("action", label = "Add")),
mainPanel(
width = 8,
tableOutput('my_table')
)
)
)
server <- function(input, output, session) {
timetable <- reactiveVal(
data.frame(monday = c("","","","",""),
tuesday = c("","","","",""),
wednesday = c("","","","",""),
thursday = c("","","","",""),
friday = c("","","","",""))
)
output$my_table = renderTable(timetable(),
bordered = TRUE,
spacing = c('l'),
width = "100%",
striped = TRUE,
align = 'c',
rownames = TRUE)
observeEvent(input$action, {
tmp <- timetable()
tmp[1, "monday"] <- input$select1
timetable(tmp)
})
}
shinyApp(ui, server)