问题
I want the user to be able to edit the already-loaded DataTable, click a button and then have the edited version used as input to do stuff. So in this example, how could I make the new user-edited versions appear in the "New" tabs upon clicking of the "Change Dataframes" button?
User Interface
shinyUI(fluidPage(
titlePanel(),
sidebarLayout(
sidebarPanel(
actionButton("runButton","Change Dataframes")
),
mainPanel(
tabsetPanel(
tabPanel("OldIrisTab",
DT::dataTableOutput("OldIris")),
tabPanel("OldPetrolTab",
DT::dataTableOutput("OldPetrol")),
tabPanel("NewIrisTab",
DT::dataTableOutput("NewIris")),
tabPanel("NewPetrolTab",
DT::dataTableOutput("NewPetrol"))
)
)
)
))
Server file
shinyServer(function(input,output){
output$OldIris <- DT::renderDataTable({
datatable(iris,editable=T)
})
output$OldPetrol <- DT::renderDataTable({
datatable(petrol,editable=T)
})
######
# HERES WHERE I'M NOT REALLY SURE WHAT TO DO
change_data1 <- eventReactive(input$runButton, {
withProgress(message="Generating new dataframes",{
newdf1 <- datatable(output$OldIris)
newdf1
})
})
change_data2 <- eventReactive(input$runButton, {
withProgress(message="Generating new dataframes",{
newdf2 <- datatable(output$OldPetrol)
newdf1
})
})
output$NewIris <- DT::renderDataTable({
datatable(change_data1())
})
output$NewPetrol <- DT::renderDataTable({
datatable(change_data2())
})
#######
######
})
回答1:
Using rhandsontable package:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
titlePanel("Ttile"),
sidebarLayout(
sidebarPanel(
actionButton("runButton","Change Dataframes")
),
mainPanel(
tabsetPanel(
tabPanel("OldIrisTab", rHandsontableOutput('OldIris')),
tabPanel("NewIrisTab", DT::dataTableOutput("NewIris"))
))))
server <- function(input,output,session)({
values <- reactiveValues()
output$OldIris <- renderRHandsontable({
rhandsontable(iris)
})
observeEvent(input$runButton, {
values$data <- hot_to_r(input$OldIris)
})
output$NewIris <- DT::renderDataTable({
values$data
})
})
shinyApp(ui, server)
I hope it helps. You can also check my answer at Rstudio community here.
来源:https://stackoverflow.com/questions/49956418/how-to-use-an-editable-datatable-in-shiny-as-input-for-another-datatable