复选框不会更新闪亮应用程序中的从属值

时间:2018-10-24 20:00:47

标签: r shiny

我有一个简单的闪亮应用程序,在其中选择数据集,然后选择是否在字符值两边加上引号,然后下载csv文件。由于某种原因,该应用仅对我对引号的第一个默认选择做出反应(无引号),并且当我单击复选框以添加引号时不响应。如果您使用文本编辑器打开csv文件,则每次都会看到: enter image description here

代替

enter image description here

选择引号时

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .destructive, title: "-") { [weak self] (action, indexPath) in

        // Use a weak reference to self to avoid any reference cycles.
        guard let weakSelf = self else {
            return
        }

        tableView.beginUpdates()

        let cmd: cmdline
        if weakSelf.groupedData[indexPath.section].1.count > 1 {
            cmd = weakSelf.groupedData[indexPath.section].1.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .right)
        } else {
            // Remove the section when the `items` are empty or when the last item is to be removed.
            cmd = weakSelf.groupedData.remove(at: indexPath.section).1[indexPath.row]
            tableView.deleteSections([indexPath.section], with: .right)
        }
        weakSelf.delegate?.didDeleteCmdLine(cmd)

        tableView.endUpdates()
    }

    return [delete]
}

1 个答案:

答案 0 :(得分:2)

将您的output$downloadData代修改为以下作品:

 # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = paste(input$dataset, ".csv", sep = ""),
    content = function(file) write.csv(datasetInput(), file, row.names = FALSE, quote = input$quotes)
  )

这也更容易阅读和理解。

我不确定您的if-else“ content”参数是否正确解释。我本来希望有一个错误,但我不明白为什么它不会失败。如果content =if之前,则可能有效。无论如何,在这种情况下,您应该使用ifelse函数,或者做一些我上面做过的事情。

此外,filename参数也不需要成为(lambda)函数。

最后,最好使用TRUEFALSE而不是TF,因为后者可以分配变量。

完整脚本:

library(shiny)

# Define UI for data download app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Downloading Data"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Choose dataset ----
      selectInput("dataset", "Choose a dataset:",
                  choices = c("rock", "pressure", "cars")),
      checkboxInput("quotes",
                    strong("Include quotes around values"), 
                    value = FALSE),

      # Button
      downloadButton("downloadData", "Download")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      tableOutput("table")

    )

  )
)

# Define server logic to display and download selected file ----
server <- function(input, output) {

  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  # Table of selected dataset ----
  output$table <- renderTable({
    datasetInput()
  })

  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = paste(input$dataset, ".csv", sep = ""),
    content = function(file) write.csv(datasetInput(), file, row.names = FALSE, quote = input$quotes)
  )

}

# Create Shiny app ----
shinyApp(ui, server)