我试图在用户按下Shiny中的按钮后简单地更新数据框的列。对于当前显示的数据帧如何传递到服务器端功能,我有些困惑。
按下按钮后,列cyl应增加10。如果再次按下按钮,则列应获取重新计算的值并再乘以10,依此类推。
到目前为止,我已经做到了,但是当按下按钮时似乎什么也没发生。
---
title: "My dataframe refresh"
output: html_document
runtime: shiny
---
```{r, echo=FALSE}
library(EndoMineR)
shinyApp(
ui <- fluidPage(
DT::dataTableOutput("mytable"),
actionButton("do", "Click Me")
),
server = function(input, output,session) {
#Load the mtcars table into a dataTable
output$mytable = DT::renderDataTable({
mtcars
})
#A test action button
observeEvent(input$do, {
renderDataTable(mtcars$cyl*10)
})
},
options = list(height = 800)
)
```
Run Code Online (Sandbox Code Playgroud)
尝试这个:
library(shiny)
library(DT)
RV <- reactiveValues(data = mtcars)
app <- shinyApp(
ui <- fluidPage(
DT::dataTableOutput("mytable"),
actionButton("do", "Click Me")
),
server = function(input, output,session) {
#Load the mtcars table into a dataTable
output$mytable = DT::renderDataTable({
RV$data
})
#A test action button
observeEvent(input$do, {
RV$data$cyl <- RV$data$cyl * 10
})
}
)
runApp(app)
Run Code Online (Sandbox Code Playgroud)
我总是存储我的数据帧,尤其是如果它们应该在reactiveValues列表中处于响应状态。之后,您只需渲染数据,然后在观察步骤中覆盖原始数据框。您必须显式覆盖数据以存储结果,mtcars$cyl * 10而不会影响mtcars数据框。