R闪亮的可编辑DT数据表应用程序可在Chrome上运行,但不能在Safari上运行

时间:2019-07-19 10:28:40

标签: r datatable shiny safari

我正在编写一个简单的闪亮应用程序,以计算棋盘游戏7 Wonders的得分。它使用可编辑的DT数据表,并部署在Shinyapps.io上。它在Chrome上运行良好,但在Safari上我无法编辑表格。如何在Safari上启用编辑?谢谢!

这是Shinyapps.io上的链接

https://woodward.shinyapps.io/7wonders/

library(shiny)
library(shinythemes)
library(dplyr)
library(DT)
library(magrittr)

rnames <- c("Gears", "Tablets", "Compasses", "Sets", "Total Score")

initdata <- data.frame(
  Number = rep("", length(rnames)), 
  Wilds = rep("", length(rnames)),
  Total = rep("", length(rnames)),
  Score = rep("", length(rnames)),
  row.names=rnames,
  stringsAsFactors=FALSE
) 

ui <- fluidPage(

  theme=shinytheme("united"),
  dataTableOutput("table")

)

server <- function(input, output, session){

  my <- reactiveValues(
    data=initdata
    )

  output$table <- renderDataTable(
    datatable(isolate(my$data), 
              options = list(searching=FALSE, scrollX=FALSE, scrollY=FALSE, info=FALSE,
                             paging=FALSE, ordering=FALSE, lengthChange=FALSE), 
              class = "stripe nowrap cell-border",
              editable = TRUE)
  )

  proxy <- dataTableProxy("table")

  n2s <- function(n){
    if_else(n==0, "", as.character(n))
  }

  s2n <- function(s){
    if_else(s %in% c("", "0"), 0, floor(abs(as.numeric(s))))
  }

  observeEvent(input$table_cell_edit, {
    info = input$table_cell_edit
    i = info$row
    j = info$col
    k = s2n(info$value)
    cat("edited", i, j, k, "\n")
    d <- my$data
    if (i<=3 && j==1){ # simple symbol
      d[i, j] <- n2s(k)
    } else if (i<=3 && j==2){ # add a wild
      d[i, j] <- n2s(k)
    }

    # assign wilds
    w <- sum(s2n(d[1:3, 2]), na.rm=TRUE)
    cat("wilds", w, "\n")

    i <- 0
    comb <- vector("list", (w+1)^2)
    for (b1 in 0:w){
      for (b2 in 0:(w-b1)){
        b3 <- w - b1 - b2
        i <- i + 1
        comb[[i]] <- c(b1, b2, b3)
      }
    }
    ntries <- i
    cat("combos", ntries, "\n")
    tbest <- 0
    xbest <- comb[[1]]
    for (i in seq(ntries+1)){
      if (i<=ntries){
        x <- comb[[i]]
      } else {
        cat("best", xbest, "gives", tbest, "\n")
        x <- xbest
      }
      d[1:3, 2] <- n2s(x)
      d[1:3, 3] <- n2s(s2n(d[1:3, 1]) + s2n(d[1:3, 2])) # Number + Wilds
      d[4, 3] <- n2s(min(s2n(d[1:3, 3]))) # Sets
      d[1:3, 4] <- n2s(s2n(d[1:3, 3])^2) # Score for each type
      d[4, 4] <- n2s(s2n(d[4, 3])*7) # Score for Sets
      d[5, 4] <- n2s(sum(s2n(d[1:4, 4]))) # Total Score
      if (s2n(d[5, 4])>tbest){
        tbest <- s2n(d[5, 4])
        xbest <- x
      }
    }
    my$data <- d
    replaceData(proxy, my$data, resetPaging = FALSE)
    })

}

shinyApp(ui, server)

0 个答案:

没有答案