【问题标题】:use rhandsontable package to edit multiple data frame on shiny使用 rhandsontable 包在闪亮上编辑多个数据框
【发布时间】:2018-06-19 17:46:44
【问题描述】:

我是闪亮的新手,我想通过单选按钮编辑不同的多个数据帧或使用 rhandsontable 包选择输入。但是,我的脚本不能显示其他数据框,只能显示第一个,我不知道是什么问题。

ui.R:
library(rhandsontable)
fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("select2", label = h3("Choose to edit"), 
              choices = list("003.csv", "004.csv", "005.csv", 
                             "006.csv", "007.csv"), 
              selected = "003.csv"),
  actionButton("saveBtn", "Save changes")
),
mainPanel(
  rHandsontableOutput("hot")
)))

server.R
values <- reactiveValues() 
setHot <- function(x) values[["hot"]] <<- x 
function(input, output, session) {

 fname <- reactive({
   x <- input$select2
   return(x)
 })

 observe({ 
   input$saveBtn # update csv file each time the button is pressed
   if (!is.null(values[["hot"]])) { 
  write.csv(x = values[["hot"]], file = fname(), row.names = FALSE)
}
})

 output$hot <- renderRHandsontable({ 
   if (!is.null(input$hot)) { # if there is an rhot user input...
  DF <- hot_to_r(input$hot) # convert rhandsontable data to R object 
  and store in data frame
  setHot(DF) # set the rhandsontable values

} else {
  DF <- read.csv(fname(), stringsAsFactors = FALSE) # else pull table from the csv (default)
  setHot(DF) # set the rhandsontable values
}

rhandsontable(DF) %>% # actual rhandsontable object
  hot_table(highlightCol = TRUE, highlightRow = TRUE, readOnly = TRUE) %>%
  hot_col("Status", readOnly = FALSE)
 })}

我可以编辑和保存它显示第一个 003.csv 的数据框,但是当我使用 004.csv 的下拉列表时,它没有显示数据框。请指教。

【问题讨论】:

  • 什么是setHot
  • @Aurèle setHot
  • 它不会按您期望的方式工作,因为一旦导入了第一个表,input$hot 在 Shiny 会话期间不再为空,并且包含 @ 的 else 分支987654325@ 不再访问
  • @Aurèle 谢谢!!所以有什么建议可以纠正那部分吗??

标签: r shiny shiny-server shinyjs rhandsontable


【解决方案1】:

这将写入(并可能覆盖 ⚠ 任何现有文件)虚拟数据:

for (i in c("003.csv", "004.csv", "005.csv", "006.csv", "007.csv")) {
  write.csv(cbind(V1 = rep(i, 3), Status = "foo"), i, row.names = FALSE)
}

我对@9​​87654322@进行了一番大修:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        "select2", label = h3("Choose to edit"), selected = "003.csv",
        choices = list("003.csv", "004.csv", "005.csv", "006.csv", "007.csv")
      ),
      actionButton("saveBtn", "Save changes")
    ),
    mainPanel(
      rHandsontableOutput("hot")
    )
  )
)

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

  DF <- reactiveVal()

  observe({
    DF(read.csv(input$select2, stringsAsFactors = FALSE))
  })

  observe({
    if (!is.null(input$hot)) DF(hot_to_r(input$hot))
  })

  observeEvent(input$saveBtn, {
    if (!is.null(DF())) write.csv(DF(), input$select2, row.names = FALSE)
  })

  output$hot <- renderRHandsontable({
    rhandsontable(DF()) %>% 
      hot_table(highlightCol = TRUE, highlightRow = TRUE, readOnly = TRUE) %>%
      hot_col("Status", readOnly = FALSE)
  })

}

shinyApp(ui, server)

【讨论】:

  • 在观察中添加这两个命令时出现错误。观察({ DF(read.csv(input$select2, stringsAsFactors = FALSE), y
  • 这在语法上可能不是您想要的(没有定义x)。报告错误时,它还有助于完整地报告它(当它发生时,以及完整的错误消息、堆栈跟踪、调试尝试......)。也许试试:observe({ x &lt;- read.csv(input$select2, stringsAsFactors = FALSE) ; y &lt;- names(x)[grepl(pattern = "_Date", names(x))] ; x[, y] &lt;- as.Date(x[, y], format = "%d-%b-%y") ; DF(x) })
  • (为了可读性,最好用新行替换;s。我在这里放了分号,因为SO cmets中不允许新行)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2019-08-01
  • 2019-10-20
  • 2020-02-23
  • 2019-01-29
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多