【问题标题】:writing click xy coordinates to csv将单击 xy 坐标写入 csv
【发布时间】:2017-05-31 12:51:32
【问题描述】:

我有一个表单应用程序,可以将键入的信息以及在绘图上单击的位置保存到 CSV。我无法让它输出此点击输入的 x/y 坐标。我可以获得完整的 plot_click 列表输出 但这包含逗号,因此在 CSV 中造成困难。我已经尝试了每种类型的反应式表达,但除了 NULL 之外,input$plot_click$x 什么都没有

代码如下: 界面

shinyUI(
  fluidPage(
    titlePanel("click on location"),
div(
  textInput("name", "Name", ""),
  actionButton("submit", "Submit", class = "btn-primary")
),
div(plotOutput("plot1", click = "plot_click"),
    verbatimTextOutput("info")
)))

服务器

fieldsAll <- c("name", "plot_click")
plotx <- c(0,100)
ploty <- c(0,70)
responsesDir <- file.path("responses")
epochTime <- function() {
  as.integer(Sys.time())
}
humanTime <- function() format(Sys.time(), "%Y%m%d-%H%M%OS")

shinyServer (function(input, output, session) {


   formData <- reactive({
   data <- sapply(fieldsAll, function(x) input[[x]])
data <- c(data, timestamp = epochTime())
data <- t(data)
data
  })
  saveData <- function(data) {
fileName <- sprintf("%s_%s.csv",
                    humanTime(),
                    digest::digest(data))

    write.csv(x = data, file = file.path(responsesDir, fileName),
          row.names = FALSE, quote = TRUE)
  }

  # action to take when submit button is pressed
  observeEvent(input$submit, {
   saveData(formData())
 })
  output$plot1 <- renderPlot({
plot(plotx, ploty)
  })

 output$info <- renderText({
   paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
 })

  })

任何帮助将不胜感激! 康纳

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    你快到了。稍微修改您的反应式表达式即可获得 x 坐标。

     formData <- reactive({
            data <- sapply(fieldsAll, function(x) unlist(input[[x]][1]))
            data <- c(data, timestamp = epochTime())
            data <- t(data)
            data
          }) 
    

    【讨论】:

    • 谢谢,这正是我们所需要的。
    • 如果您的问题已经解决,可以接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 2017-11-18
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多