闪亮的DT:使单元格可编辑并更新图

问题描述 投票:0回答:1

我正在尝试使DT数据表(以下称为datatable)中的单元格可编辑。此后,图形也应更新。

我尝试过类似editable DT inside shiny module的操作,但似乎无法解决。

任何人都能对此有所了解吗?

请在下面找到我的完整代码-数据集是经典的《泰坦尼克号》。

#========================+== LOADING LIBRARIES AND DATASET ======================
#load libraries to be used. Pacman ensures library reproducibility 
#rm(list=ls(all=TRUE))
if (!require("pacman")) install.package("pacman")
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, shinythemes, plotly, DT)


#load Titanic dataset 
dataset<-read.csv("...titanic.csv")

#=========================== DATA PREPARATION ====================================
#change the data type of "survived", "Pclass", and "sex" to factor 
dataset<-dataset%>%mutate (
  Survived=factor(Survived),
  Pclass= factor(Pclass),
  Sex= factor(Sex))

#imputation of "embarked" for passengers 62 and 830 to "C" 
dataset$Embarked[c(62,830)]<- 'C'


#imputation through mean of missing ages - this could be improved with recursive partioning, for example 
dataset$Age[is.na(dataset$Age)] <- mean(dataset$Age,na.rm=T)


#=========================== OTHERS (SUPPORT) ==========================================
pal <-c("dark gray", "steelblue")

#=========================== UI ==========================================
ui <- dashboardPage(

  #header
  dashboardHeader(
    title = "Titanic demo",
    titleWidth = 300
  ),

  #sidebar
  dashboardSidebar(
    sidebarMenu(
      sidebarMenu(
        menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
        menuItem("Data Table", tabName = "datatable", icon = icon("th"))
      )
    )
  ),

  #body
  dashboardBody(
    tabItems(
      # first tab
      tabItem(tabName = "visualization",
              h2("Is there a relationship amongst age, fare paid, and having survived in the Titanic disaster?"),
              fluidRow(
                box(plotlyOutput("Trialplot"),
                    title= "Age vs. Fare visualization",
                    status="primary",
                    solidHeader=TRUE,
                    collapsible=TRUE,
                    width=8
                ),

                box(
                  title = "Age range",
                  status="primary",
                  solidHeader=TRUE,
                  collapsible=TRUE,
                  width=4,
                  sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age,na.rm = T), c(0,max(dataset$Age,na.rm = T)/2), ticks=FALSE)
                ), 


                box(
                  title = "Fare range",
                  status="primary",
                  solidHeader=TRUE,
                  collapsible=TRUE,
                  width=4,
                  sliderInput("slider2", "Please select the fare range to be seen:", 0, max(dataset$Fare,na.rm = T), c(0,max(dataset$Fare,na.rm = T)/2), ticks=FALSE)
                ), 


              )
      ),

      #second tab
      tabItem(tabName = "datatable",
              h2("Data table"),
              h3("Filtered by the sliders in 'visualization' tab"),
              fluidRow(DTOutput("datatable"),
              class='cell-border stripe'
              )
      )
    )
  )
)


#========================== SERVER ==========================================
server = shinyServer(function(input, output,session) { 
  observe({
    print(input$slider)
  })
  output$Trialplot<-renderPlotly({
    dataset_filtered<-dataset[dataset$Age >= input$slider[1] & dataset$Age <= input$slider[2] & dataset$Fare >= input$slider2 & dataset$Fare <= input$slider2[2],]
    plot_ly(data = dataset_filtered, x = ~Fare, y = ~Age , 
            color=~Survived, 
            colors=pal, 
            type='scatter',
            marker=list(size=5),
            hoverinfo='text',
            text=~paste("Name:",Name,", Age:",Age,", Fare:",Fare, "pounds")
    )%>%
      layout(title="Age vs.Fare",
             xaxis=list(title="Fare in pounds"),
             yaxis=list(title="Age in years"))
  }) 

  output$datatable = renderDT(
    dataset[dataset$Age >= input$slider[1] & dataset$Age <= input$slider[2] & dataset$Fare >= input$slider2[1] & dataset$Fare <= input$slider2[2] ,], 
    filter="bottom",
    options = list(lengthChange = FALSE))

}
)

#========================== APP ==========================================
shinyApp(ui,server)  
r datatable shiny
1个回答
0
投票

要使表格可编辑,您只需添加editable参数。选项可以是cellrowcolumnall。一个很好的参考是here,它可以帮助您解决有关样式和功能的许多DT问题。

output$datatable = renderDT(
        dataset[dataset$Age >= input$slider[1] & dataset$Age <= input$slider[2] & dataset$Fare >= input$slider2[1] & dataset$Fare <= input$slider2[2] ,],
        editable = 'cell',
        filter="bottom",
        options = list(lengthChange = FALSE))
© www.soinside.com 2019 - 2024. All rights reserved.